我想将bz2 url中的数据直接解压缩到目标文件.这是代码:
filename = 'temp.file'
req = urllib2.urlopen('http://example.com/file.bz2')
CHUNK = 16 * 1024
with open(filename, 'wb') as fp:
while True:
chunk = req.read(CHUNK)
if not chunk: break
fp.write(bz2.decompress(chunk))
fp.close()
bz2.decompress(chunk)出错 – ValueError:找不到流的结尾
解决方法:
使用bz2.BZ2Decompressor
进行顺序解压缩:
filename = 'temp.file'
req = urllib2.urlopen('http://example.com/file.bz2')
CHUNK = 16 * 1024
decompressor = bz2.BZ2Decompressor()
with open(filename, 'wb') as fp:
while True:
chunk = req.read(CHUNK)
if not chunk:
break
fp.write(decompressor.decompress(chunk))
req.close()
顺便说一下,只要你使用with语句就不需要调用fp.close().