bzip2 - unpack bz2 url without temporary file in python -
i want unpack data bz2 url directly target file. here code:
filename = 'temp.file' req = urllib2.urlopen('http://example.com/file.bz2') chunk = 16 * 1024 open(filename, 'wb') fp: while true: chunk = req.read(chunk) if not chunk: break fp.write(bz2.decompress(chunk)) fp.close()
error on bz2.decompress(chunk) - valueerror: couldn't find end of stream
use bz2.bz2decompressor
sequential decompression:
filename = 'temp.file' req = urllib2.urlopen('http://example.com/file.bz2') chunk = 16 * 1024 decompressor = bz2.bz2decompressor() open(filename, 'wb') fp: while true: chunk = req.read(chunk) if not chunk: break fp.write(decompressor.decompress(chunk)) req.close()
btw, don't need call fp.close()
long use with
statement.
Comments
Post a Comment