尝试在我的test.txt文件中使用seek时出错

时间:2019-05-06 19:27:14

标签: python-2.7 printing

现在让我们倒带,就像磁带一样。 让我们打印三行: 1个 追溯(最近一次通话):   在第27行的文件“ ex20.py”中     print_a_line(当前行,当前行)   文件“ ex20.py”,第12行,在print_a_line中     打印line_count,f.readline() AttributeError:“ int”对象没有属性“ readline” 是我得到的错误,我的代码是:

from sys import argv

script, input_file = argv

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

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

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

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_line)

current_line = current_line + 1
print_a_line(current_line, current_line)

current_line = current_line + 1
print_a_line(current_line, current_line)

文本文件的内容是:

“然后” ,'谁' ,'是'

为什么会这样?

0 个答案:

没有答案