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

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -