Read specific bytes of file in python

后端 未结 3 1536
一整个雨季
一整个雨季 2021-02-08 09:02

I want to specify an offset and then read the bytes of a file like

offset = 5
read(5) 

and then read the next 6-10 etc. I read about seek but

3条回答
  •  野的像风
    2021-02-08 09:18

    Just play with Python's REPL to see for yourself:

    [...]:/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'
    

提交回复
热议问题