基於python的文件seek和tell實例解析

一 概念
A F.seek(偏移量, whence=相對位置)
偏移量
大於0的數代表向文件末尾方向移動的字節數
小於0的數代表向文件頭方向中移動的字節數
相對位置
0 代表從文件頭開始偏移
1 代表從文件當前讀寫位置開始偏移
2 代表從文件尾開始偏移
B tell函數能夠返回指針在文件中的位置。
 
二 實例解析
file_name = "test1.txt"
fp = open(file_name, "r",encoding='utf8')
print("point is ", fp.tell())
str = fp.read(18)  # 見說明1
print("read data is ", str)
print("now position is ", fp.tell())
fp.seek(9,0)
print("fp.seek(9,0) ow position is: ", fp.tell())
str=fp.readline()  # 見說明4
print("fp.readline() read data is ", str)
print("now point is", fp.tell())

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章