相关疑难解决方法(0)

使用Python 2.6运行Python 2.7代码

我有这个简单的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/horizo​​n/+bug/955994

但是可以将上面的代码移植到Python 2.6并仍然保持跨平台吗?

python compatibility python-2.6 python-2.7

8
推荐指数
1
解决办法
5042
查看次数

在“with:”块中使用时,GzipFile 实例没有属性“__exit__”

我需要用 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 压缩的文本文件?

python gzip python-2.6

3
推荐指数
1
解决办法
4000
查看次数

标签 统计

python ×2

python-2.6 ×2

compatibility ×1

gzip ×1

python-2.7 ×1