学习python难题练习20帮助
问题描述:
练习附加题20我被告知要评论我认为每一行的含义。寻找我发现混乱。如果有人愿意查看我对源代码的评论,看看我是否理解正确。我可以跳过它,但我觉得它很重要,我明白这一点。学习python难题练习20帮助
感谢
from sys import argv #imports argv from sys moduel
script, input_file = argv #unpacks the startup arguments to script and input_file variables
def print_all(f): #defines a function that uses the read() function on whatever is in the parameter(current_file in this case)
print f.read()
def rewind(f): #not a clue what this does really...moves to byte 0 of the file??
f.seek(0)
def print_a_line(line_count, f): #sets a function that reads a line from the current_file
print line_count, f.readline()
current_file = open(input_file)
print 'first of all, lets read the wole file:\n'
print_all(current_file)
print 'now lets rewind, kind of like a tape'
rewind(current_file)
print 'lets print 3 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)
答
是的,它设置文件的当前位置到第一个字节。这可以看出,在documentation for file.seek:
file.seek(offset[, whence])
设置文件的当前位置,如标准输入输出的FSEEK()。 whence参数是可选的,默认为os.SEEK_SET或0(绝对文件定位);其他值为os.SEEK_CUR或1(相对于当前位置查找)和os.SEEK_END或2(相对于文件末尾查找)。没有返回值。
注意,因为你没有为whence
参数提供一个值,默认值os.SEEK_SET
使用。这意味着绝对文件定位(即相对于文件的开始)。
谢谢你们,剩下的是准确的? – neil 2011-03-07 20:53:59