python中seek函数将文件指针设置到文件的起始位置

 

001、

>>> os.listdir()                ## 当前路径下的文件
['a.txt']
>>> input_file = open("a.txt")  ## 打开文件
>>> for i in input_file:
...     i = i.strip()
...     print(i)
...
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16                     ## 执行循环后,文件指针移动到最后
>>> for i in input_file:        
...     i = i.strip()
...     print(i)
...
>>> input_file.seek(0,0)        ## 利用seek函数将文件的指针移动到文件的起始位置
0
>>> for i in input_file:        ## 循环文件
...     i = i.strip()
...     print(i)
...
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16

 。

 

posted @ 2025-05-19 22:43  小鲨鱼2018  阅读(14)  评论(0)    收藏  举报