此方沒有返回值。請注意,如果檔案被開啟使用的是'a'或'A+'追加,任何seek()操作將在下次寫時撤消。
如果該檔案只開啟使用 'A' 追加模式寫入,這種方法本質上是一個無操作,但是讀取啟用(模式'A+'),它在追加模式開啟的檔案非常有用。
如果檔案在文字模式下使用「t」,只有 tell() 返回偏移開是合法的。其他偏移時會導致不確定的行為。
fileObject.seek(offset[, whence])
offset -- 這是在檔案內的讀/寫指標的位置。
whence -- 這是可選的,預設為0表示絕對的檔案定位;值是1時這意味著尋找相對於當前位置;以及值是2時尋找相對於檔案的末尾。
Assuming that 'foo.txt' file contains following text: This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line
#!/usr/bin/python3 # Open a file fo = open("foo.txt", "rw+") print ("Name of the file: ", fo.name) line = fo.readlines() print ("Read Line: %s" % (line)) # Again set the pointer to the beginning fo.seek(0, 0) line = fo.readline() print ("Read Line: %s" % (line)) # Close opened file fo.close()
Name of the file: foo.txt Read Line: ['This is 1st line\n', 'This is 2nd line\n', 'This is 3rd line\n', 'This is 4th line\n', 'This is 5th line'] Read Line: This is 1st line