python文件操作 seek(),tell()


 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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM