How to read specific part of large file in Python

后端 未结 2 1838
一整个雨季
一整个雨季 2020-11-27 21:59

Given a large file (hundreds of MB) how would I use Python to quickly read the content between a specific start and end index within the file?

Essentially, I\'m look

2条回答
  •  借酒劲吻你
    2020-11-27 22:05

    You can seek into the file the file and then read a certain amount from there. Seek allows you to get to a specific offset within a file, and then you can limit your read to only the number of bytes in that range.

    with open(filename) as fin:
        fin.seek(start_index)
        data = fin.read(end_index - start_index)
    

    That will only read that data that you're looking for.

提交回复
热议问题