tmpfile and gzip combination problem
I have problem with this code:
file = tempfile.TemporaryFile(mode='wrb')
file.write(base64.b64decode(data))
file.flush()
os.fsync(file)
# file.seek(0)
f = gzip.GzipFile(mode='rb', fileobj=file)
print f.read()
I dont know why it doesn't print out anything. If I uncomment file.seek then error occurs:
File "/usr/lib/python2.5/gzip.py", line 263, in _read
self._read_gzip_header()
File "/usr/lib/python2.5/gzip.py", line 162, in _read_gzip_header
magic = self.fileobj.read(2)
IOError: [Errno 9] Bad file descriptor
Just for information this version works fine:
x = open("test.gzip", 'wb')
x.write(base64.b64decode(data))
x.close()
f = gzip.GzipFile('test.gzip', 'rb')
print f.read()
EDIT: For wrb problem. It doesn't give me an error when initialize it. Python 2.5.2.
>>> t = tempfile.TemporaryFile(mode="wrb")
>>> t.write("test")
>>> t.seek(0)
>>> t.read()
Traceback (most recent call last):
File "<stdin>", line 1, in &l开发者_如何学运维t;module>
IOError: [Errno 9] Bad file descriptor
'wrb'
is not a valid mode.
This works fine:
import tempfile
import gzip
with tempfile.TemporaryFile(mode='w+b') as f:
f.write(data.decode('base64'))
f.flush()
f.seek(0)
gzf = gzip.GzipFile(mode='rb', fileobj=f)
print gzf.read()
Some tips:
- You can't
.seek(0)
or.read()
a gzip file inwrb
mode orwb
orw+b
. GzipFile class__init__
set itself toREAD
orWRITE
only by looking at the first character ofwrb
(set itself toWRITE
for this case). - When doing
f = gzip.GzipFile(mode='rb', fileobj=file)
your real file isfile
notf
, I understood that after reading GzipFile class definition.
A working example for me was:
from tempfile import NamedTemporaryFile
import gzip
with NamedTemporaryFile(mode='w+b', delete=True, suffix='.txt.gz', prefix='f') as t_file:
gzip_file = gzip.GzipFile(mode='wb', fileobj=t_file)
gzip_file.write('SOMETHING HERE')
gzip_file.close()
t_file.seek(0)
# Do something here with your t_file, maybe send it to an external storage or:
print t_file.read()
I hope this can be useful for someone out there, took a lot of my time to make it work.
精彩评论