Python 文件操作seek()函数

时间:2018-10-29 11:15:17   收藏:0   阅读:1830

函数语法

seek(offset, whence=0, /)

函数说明

Change the stream position to the given byte offset.The offset is interpreted relative to the position indicated by whence.

参数说明

    * 0 -- start of stream (the default); offset should be zero or positive
    * 1 -- current stream position; offset may be negative
    * 2 -- end of stream; offset is usually negative

注:当未使用二进制格式打开文件时,从当前位置或文件尾进行偏移会报“io.UnsupportedOperation: can‘t do nonzero cur-relative seeks”错误,把文件打开方式加上“b”即可。

示例:

f.seek(4,0)   #从开头位置向后偏移4字节
f.seek(3,1)   #从当前位置向后偏移3字节
f.seek(-3,2)  #从结尾位置向前偏移3字节

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!