本文共 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/