博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python文件操作 seek(),tell()
阅读量:4568 次
发布时间:2019-06-08

本文共 695 字,大约阅读时间需要 2 分钟。

 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/paisen/p/3492768.html

你可能感兴趣的文章
android开发时gen和bin目录的SVN管理(转)
查看>>
Jsonp post 跨域方案
查看>>
30分钟 让你成为一个更好的程序员
查看>>
使用HTML5开发离线应用 - cache manifest
查看>>
深入理解C语言 - 指针使用的常见错误
查看>>
JAVA Excel API学习案例
查看>>
网页打开速度慢的原因及N种解决方法
查看>>
监控线上服务的小脚本
查看>>
ultraedit 实际应用技巧
查看>>
mysql_select 单表查询
查看>>
磁盘配额
查看>>
dns视图搭建
查看>>
WPF 中动态改变控件模板
查看>>
ubuntu Ctrl+Alt+F1 进入终端字符界面 登录出现login incorrect解决办法
查看>>
珍爱生命,远离JS=>JS避坑记
查看>>
Bundle对象实现不同Activity之间数据传递
查看>>
TeamViewer 远程应用不显示,空白解决方案
查看>>
人机交互第五次作业
查看>>
linux下MySQL停止和重启
查看>>
Unity Particle System Sorting Order
查看>>