Python tell 和 seek用法

tell

1.作用:获取当前文件读取指针的位置

2.语法:file.tell()

seek

1.作用:用于移动文件读写指针到指定位置

2.语法:file.seek(offset,whence=0)

              -->offset:偏移量,需要向前或向后移动的字节数,正往结束方向移动,负往开始方向移动。

              -->whence:可选值,默认为0,这意味着绝对的文件定位,

                                               1这意味着寻求相对于当前位置,

                                                2表示相对于文件的末尾。

   

#test.txt
#first line
#second line
#third line

f=open('test.txt','r')
print(f.readline())
print(f.readline())
f.seek(0,0)
print(f.readline())
f.seek(1,0)
print(f.readline())
first line

second line

first line

irst line

                              

原文地址:https://www.cnblogs.com/zhanghr0728/p/7890445.html