This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author minstrelofc
Recipients minstrelofc
Date 2021-10-14.21:12:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634245921.58.0.043840382812.issue45475@roundup.psfhosted.org>
In-reply-to
Content
Attempting to iterate over an opened gzip file raises a ValueError: readline of closed file

Behavior in Python 3.9.7:
Python 3.9.7 (default, Oct 13 2021, 09:08:19) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> ll = [l for l in gzip.GzipFile(filename='data/UTF-8-test_for_gzip.txt.gz')]
>>> len(ll)
300


Behavior in Python 3.10.0 (and 3.11.0a1 is the same):
Python 3.10.0 (default, Oct 13 2021, 08:53:15) [GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> ll = [l for l in gzip.GzipFile(filename='data/UTF-8-test_for_gzip.txt.gz')]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
ValueError: readline of closed file


This only happens when iterating directly over the GzipFile object. Using a with: statement has the correct behaviour in both 3.10 and 3.11:
>>> with gzip.GzipFile(filename='UTF-8-test_for_gzip.txt.gz') as input_file:
...     len(list(input_file))
... 
300
History
Date User Action Args
2021-10-14 21:12:01minstrelofcsetrecipients: + minstrelofc
2021-10-14 21:12:01minstrelofcsetmessageid: <1634245921.58.0.043840382812.issue45475@roundup.psfhosted.org>
2021-10-14 21:12:01minstrelofclinkissue45475 messages
2021-10-14 21:12:01minstrelofccreate