博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python tell和seek操作
阅读量:4160 次
发布时间:2019-05-26

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

tell

1. 作用:获取当前文件读取指针的位置
2. 语法格式: file.tell() 注: 此方法没有参数

seek

1. 作用:用于移动文件读写指针到指定的位置
2. 语法格式:file.seek(offset, whence=0):
--> offset: 偏移量,需要向前或者是向后移动的字节数
--> whence: 可选值,默认为0, 可选值为1或者2,表示从何处开始计算偏移,具体来说,
--> 0表示从当前位置开始计算偏移
--> 1表示从文件头位置开始计算偏移
--> 2表示从文件尾开始计算偏移
一个例子:
>>> x = file('my.log', 'r')    #读取一个文件
>>> x.tell()            #获得当前文件读取指针
0L                #当前文件指针在文件头处
>>> x.seek(3)            #将文件指针向前移动3个字节
>>> x.tell()
3L                #指针已经移动到了第3个字节处
>>> x.seek(5,1)            #表示从文件头处开始移动指针,向前移动5个字节
>>> x.tell()                
5L                #当前文件读取指针已经移动到第5个字节处
>>> x.seek(0,0)            #表示将文件指针移动到文件头处
>>> x.tell()
0L
>>> x.seek(0,2)            #表示将文件读取指针移动到文件尾部
>>> x.tell()                
214L                #可以得到文件大小为214B
>>> x.seek(-2,2)        #表示从文件尾部开始移动指针,向后移动2个字节
>>> x.tell()
212L
 

转载地址:http://ywnxi.baihongyu.com/

你可能感兴趣的文章
高性能服务器设计
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>