艰苦地学习Python(练习20)在运行时有奇怪的符号

艰苦地学习Python(练习20)在运行时有奇怪的符号,python,Python,这是密码 from sys import argv script, input_file = argv def print_all(f): print f.read() def rewind(f): print f.seek(0) def print_a_line(line_count, f): print line_count, f.readline() current_file = open(input_file) print "First, we'll

这是密码

from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    print f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First, we'll print the whole file.\n"

print_all(current_file)

print "Now let's rewind."

rewind(current_file)

print "Let's print three lines."

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)
下面是通过终端(OSX)运行时的代码


这里发生了什么事?我甚至从中复制并粘贴代码,以查看我是否做错了什么,但它做了同样的事情。

您使用文字处理器创建了输入文件。使用文本编辑器(Windows中的记事本,我不知道在OS X中会有什么)创建输入文件。

您使用文字处理器创建了输入文件。使用文本编辑器(Windows中的记事本,我不知道在OS X中会有什么)来创建输入文件。

您在TextEdit中编辑了文件-python正在吐出反映文件RTF格式的字节。在从TextEdit保存之前,请选择“格式>>生成文本文件”

您在TextEdit中编辑了文件-python正在吐出反映文件RTF格式的字节。在从文本编辑保存之前,请选择“格式>>生成文本文件”

您实际上正在读取纯文本文件吗?这看起来像是某种文字处理器文档格式之类的。另外,我可以看到,考虑到你的版本中有一个额外的
print
,你没有像你说的那样复制粘贴原始代码。直到你指出它,我才知道这一点。非常感谢。如果你想知道为什么有些印刷品的措辞不同,那是因为我喜欢在不改变太多文件的情况下保留自己的个性。我不知道为什么人们反对你。你真的在读纯文本文件吗?这看起来像是某种文字处理器文档格式之类的。另外,我可以看到,考虑到你的版本中有一个额外的
print
,你没有像你说的那样复制粘贴原始代码。直到你指出它,我才知道这一点。非常感谢。如果你想知道为什么有些印刷品的措词不同,那是因为我喜欢在不改变太多文件的情况下在它们中有我自己的个性。我不知道为什么人们会反对你。
First, we'll print the whole file.

{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0

\f0\fs24 \cf0 example 1\
example 2\
example 3}
Now let's rewind.
None
Let's print three lines.
1 {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470

2 {\fonttbl\f0\fswiss\fcharset0 Helvetica;}

3 {\colortbl;\red255\green255\blue255;}