Fork me on GitHub

一 概念
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())

 

posted on 2024-04-18 14:40  虚生  阅读(73)  评论(0)    收藏  举报