python readline,seek

本文详细介绍了Python中文件操作中的seek()和tell()函数,包括它们的三种移动方式以及如何使用这些函数来定位文件读写指针。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 seek():移动文件读取指针到指定位置

tell():返回文件读取指针的位置

seek()的三种模式:

    (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置

    (2)f.seek(p,1)  移动到相对于当前位置之后的p个字节

    (3)f.seek(p,2)  移动到相对文章尾之后的p个字节

code:

        f = open('d:/hello.txt','w')
        f.write('hello my friend python!\nsecond line.')

        f = open('d:\hello.txt','r')
        (1)print(f.readlines())   #result:  ['hello my friend python!\n', 'second line.'],f.tell()=37  文件读到的位置

        (2)print(f.readline())       #result: 
               print 'f.tell(): ',f.tell()  

               print(f.readline())        
               print 'f.tell(): ',f.tell()  

          #result:

               hello my friend python!

               f.tell():  25
               second line.
               f.tell():  37

         (3)print(f.read())
             print 'f.tell(): ',f.tell()

             #result

                 hello my friend python!
                 second line.
                 f.tell():  37

 

 

转载于:https://www.cnblogs.com/lovemo1314/archive/2012/10/12/2721578.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值