我有这个简单的python函数,可以提取zip文件(独立于平台)
def unzip(source, target):
with zipfile.ZipFile(source , "r") as z:
z.extractall(target)
print "Extracted : " + source + " to: " + target
Run Code Online (Sandbox Code Playgroud)
这在Python 2.7中运行良好,但在Python 2.6中失败:
AttributeError: ZipFile instance has no attribute '__exit__':
Run Code Online (Sandbox Code Playgroud)
我发现这个建议需要升级2.6 - > 2.7 https://bugs.launchpad.net/horizon/+bug/955994
但是可以将上面的代码移植到Python 2.6并仍然保持跨平台吗?
我需要用 Python 处理 .gz 文件。
我将文件名传递到我的 Python 脚本中:
infile = sys.argv[1]
with gzip.open(infile, 'rb') as f:
logfile = f.read()
Run Code Online (Sandbox Code Playgroud)
这给了我:
with gzip.open(infile, 'rb') as f:
AttributeError: GzipFile instance has no attribute '__exit__'
Run Code Online (Sandbox Code Playgroud)
如果我手动压缩我的 .gz 文件,然后将其传递给我的 Python 脚本,一切正常。
logfile = open(infile, 'r').read()
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是 Python 2.6.6(r266:84292,2016 年 8 月 18 日,15:13:37)。我无法在这台计算机上更新 Python。如何使用 Python 2.6 处理 gzip 压缩的文本文件?