use*_*964 8 python compatibility 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/horizon/+bug/955994
但是可以将上面的代码移植到Python 2.6并仍然保持跨平台吗?
seb*_*ian 15
关于什么:
import contextlib
def unzip(source, target):
with contextlib.closing(zipfile.ZipFile(source , "r")) as z:
z.extractall(target)
print "Extracted : " + source + " to: " + target
Run Code Online (Sandbox Code Playgroud)
contextlib.closing确实应该做的是丢失的__exit__方法ZipFile.即,调用该close方法
| 归档时间: |
|
| 查看次数: |
5042 次 |
| 最近记录: |