5

I have a thread in which I am reading a zip file with zipfile.ZipFile().read(), where I am getting a memory error.

I am aware that read() loads the entire file into memory. The size of file after unzipping is more than 100MB. I also tried with zipfile.ZipFile().open().readlines(), but it takes too much time.

Is there any way that I can read the file with speed without getting memory error?

1
  • 1
    readlines() with no sizehint argument also reads the entire file into memory and builds a list of the lines. So it wouldn't reduce memory requirements, but rather increase them slightly. See Aya's answer. Commented Jun 20, 2013 at 19:29

1 Answer 1

7

Assuming you're trying to read a zipped text file, you can treat the file-like object returned by ZipFile.open() as an iterator, and process it line-by-line...

from zipfile import ZipFile

zip = ZipFile('myzip.zip')
stream = zip.open('myfile.txt')
for line in stream:
    do_something_with(line)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.