代码是:
import sys
execfile('test.py')
在test.py我有:
import zipfile
with zipfile.ZipFile('test.jar', 'r') as z:
z.extractall("C:\testfolder")
此代码生成:
AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited
从python idle运行时,“test.py”中的代码有效.
我正在运行python v2.7.10
解决方法:
嗨也许迟到但我只是为我解决这个错误:).
我在python 2.7上创建我的代码但是当我把它放在使用2.6的服务器上时我有这个错误:
AttributeError:ZipFile实例没有属性’__exit__’
为了解决这个问题,我在这篇文章中使用了塞巴斯蒂安的答案:
Making Python 2.7 code run with Python 2.6
import contextlib
def unzip(source, target):
with contextlib.closing(zipfile.ZipFile(source , "r")) as z:
z.extractall(target)
print "Extracted : " + source + " to: " + target
像他说的那样:
contextlib.closing does exactly what the missing exit method on
the ZipFile would be supposed to do. Namely, call the close method
亲切的问候,(对不起我的英文)