我对这个代码有问题:
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()
我不知道为什么它不打印任何东西。如果我取消注释文件.seek然后出现错误:
^{pr2}$仅供参考,此版本运行良好:
x = open("test.gzip", 'wb')
x.write(base64.b64decode(data))
x.close()
f = gzip.GzipFile('test.gzip', 'rb')
print f.read()
编辑:针对wrb问题。初始化时不会给我一个错误。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 <module>
IOError: [Errno 9] Bad file descriptor
'wrb'
不是有效模式。在这很好:
一些提示:
wrb
模式下.seek(0)
或{wb
或{__init__
仅通过查看wrb
的第一个字符将自身设置为READ
或{WRITE
)。在f = gzip.GzipFile(mode='rb', fileobj=file)
时,真正的文件是file
而不是f
,在阅读了gzip文件类定义之后,我明白了这一点。在我的一个有效的例子是:
我希望这能对其他人有用,我花了很多时间才让它发挥作用。在
相关问题 更多 >
编程相关推荐