2011-03-07 215 views
0

練習附加題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) 
+0

謝謝你們,剩下的是準確的? – neil 2011-03-07 20:53:59

回答

3

是的,它設置文件的當前位置到第一個字節。這可以看出,在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使用。這意味着絕對文件定位(即相對於文件的開始)。