You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gzip always returns byte strings, no text mode #58197
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2012-05-06.13:11:08.078>created_at=<Date2012-02-10.17:14:53.631>labels= []
title='gzip always returns byte strings, no text mode'updated_at=<Date2012-05-06.13:11:08.077>user='https://github.com/peterjc'
Consider the following example where I have a gzipped text file,
$ python3
Python 3.2 (r32:88445, Feb 28 2011, 17:04:33)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> with gzip.open("ex1.sam.gz") as handle:
... line = handle.readline()
...
>>> line
b'EAS56_57:6:190:289:82\t69\tchr1\t100\t0\t*\t=\t100\t0\tCTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA\t<<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<;\tMF:i:192\n'
Notice the file was opened in binary mode ("rb" is the default for gzip.open which is surprising given "t" is the default for open on Python 3), and a byte string is returned.
Now try explicitly using non-binary reading "r", and again you get bytes rather than a (unicode) string as I would expect:
>>> with gzip.open("ex1.sam.gz", "r") as handle:
... line = handle.readline()
...
>>> line
b'EAS56_57:6:190:289:82\t69\tchr1\t100\t0\t*\t=\t100\t0\tCTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA\t<<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<;\tMF:i:192\n'
Now try and use "t" or "rt" to be even more explicit that text mode is desired,
>>> with gzip.open("ex1.sam.gz", "t") as handle:
... line = handle.readline()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/pjcock/lib/python3.2/gzip.py", line 46, in open
return GzipFile(filename, mode, compresslevel)
File "/Users/pjcock/lib/python3.2/gzip.py", line 157, in __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
ValueError: can't have text and binary mode at once
>>> with gzip.open("ex1.sam.gz", "rt") as handle:
... line = handle.readline()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/pjcock/lib/python3.2/gzip.py", line 46, in open
return GzipFile(filename, mode, compresslevel)
File "/Users/pjcock/lib/python3.2/gzip.py", line 157, in __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
ValueError: can't have text and binary mode at once
See also Issue bpo-5148 which is perhaps somewhat related.
nadeemvawda commented on Feb 11, 2012
nadeemvawdamannequin
Mannequin
More actions
The problem here is that gzip.GzipFile does not support text mode, only
binary mode. Unfortunately, its __init__ method doesn't handle unexpected
mode strings sensibly, so you get a confusing error message.
If you need to open a compressed file in text mode in Python 3.2, use
io.TextIOWrapper:
In 3.3, it would be nice for gzip.open to do this transparently when mode
is "rt"/"wt"/"at". However, binary mode will still need to be the default
(for modes "r", "w" and "a"), to ensure backward compatibility.
In the meanwhile, I'll add a note to the documentation about this
limitation, and fix GzipFile.__init__ to produce a more sensible error
message.
Activity
peterjc commentedon Feb 10, 2012
Consider the following example where I have a gzipped text file,
Notice the file was opened in binary mode ("rb" is the default for gzip.open which is surprising given "t" is the default for open on Python 3), and a byte string is returned.
Now try explicitly using non-binary reading "r", and again you get bytes rather than a (unicode) string as I would expect:
Now try and use "t" or "rt" to be even more explicit that text mode is desired,
See also Issue bpo-5148 which is perhaps somewhat related.
nadeemvawda commentedon Feb 11, 2012
The problem here is that gzip.GzipFile does not support text mode, only
binary mode. Unfortunately, its __init__ method doesn't handle unexpected
mode strings sensibly, so you get a confusing error message.
If you need to open a compressed file in text mode in Python 3.2, use
io.TextIOWrapper:
In 3.3, it would be nice for gzip.open to do this transparently when mode
is "rt"/"wt"/"at". However, binary mode will still need to be the default
(for modes "r", "w" and "a"), to ensure backward compatibility.
In the meanwhile, I'll add a note to the documentation about this
limitation, and fix GzipFile.__init__ to produce a more sensible error
message.
python-dev commentedon Feb 11, 2012
New changeset 4b32309631da by Nadeem Vawda in branch '3.2':
Issue bpo-13989: Document that GzipFile does not support text mode.
http://hg.python.org/cpython/rev/4b32309631da
New changeset 8dbe8faea0e7 by Nadeem Vawda in branch 'default':
Merge: bpo-13989: Document that GzipFile does not support text mode.
http://hg.python.org/cpython/rev/8dbe8faea0e7
python-dev commentedon May 6, 2012
New changeset 55202ca694d7 by Nadeem Vawda in branch 'default':
Closes bpo-13989: Add support for text modes to gzip.open().
http://hg.python.org/cpython/rev/55202ca694d7