我想指定一个偏移量,然后读取文件的字节,如下所示
offset = 5
read(5)
然后阅读下一篇6-10等等。我读到了关于seek的内容,但我不能理解它是如何工作的,例子也不够描述性。
seek(offset,1)
返回什么?
谢谢
发布于 2015-04-01 03:09:44
只需使用Python的REPL即可亲身体验:
[...]:/tmp$ cat hello.txt
hello world
[...]:/tmp$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('hello.txt', 'rb')
>>> f.seek(6, 1) # move the file pointer forward 6 bytes (i.e. to the 'w')
>>> f.read() # read the rest of the file from the current file pointer
'world\n'
发布于 2015-04-01 03:08:15
seek
不会返回任何有用的内容。它只是将内部文件指针移动到给定的偏移量。下一次读取将从该指针开始向上读取。
https://stackoverflow.com/questions/29376498
复制相似问题