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

 

                              

 

相关文章:

  • 2021-07-22
  • 2021-12-01
  • 2021-09-28
  • 2021-09-16
  • 2021-06-25
  • 2022-12-23
  • 2021-09-15
  • 2021-10-01
猜你喜欢
  • 2021-07-12
  • 2021-04-16
  • 2022-12-23
  • 2023-02-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案