How can I avoid: "ZipFile instance has no attribute '__exit__''" when extracting a zip file?
Asked Answered
G

2

6

The code is:

import sys
execfile('test.py')

In test.py I have:

import zipfile
with zipfile.ZipFile('test.jar', 'r') as z:
    z.extractall("C:\testfolder")

This code produces:

AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited

The code from "test.py" works when run from python idle. I am running python v2.7.10

Gastrectomy answered 1/10, 2015 at 9:39 Comment(2)
The title says __exit__, the error in the question says extractall, which one is relevant?Nihi
Does the same happen if you try to execute test.py directly? Also you should either add a second backslash or use r"C:\testfolder" to avoid \t being a tab character.Manche
V
13

I made my code on python 2.7 but when I put it on my server which use 2.6 I have this error :

AttributeError: ZipFile instance has no attribute '__exit__'

For solve this problems I use Sebastian's answer on this post : 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

Like he said :

contextlib.closing does exactly what the missing __exit__ method on the ZipFile would be supposed to do. Namely, call the close method

Violate answered 13/11, 2015 at 13:17 Comment(0)
F
0

According to the Python documentation, ZipFile.extractall() was added in version 2.6. I expect that you'll find that you are running a different, older (pre 2.6), version of Python than that which idle is using. You can find out which version with this:

import sys
print sys.version

and the location of the running interpreter can be obtained with

print sys.executable

The title of your question supports the likelihood that an old version of Python is being executed because the with statement/context managers (classes with a __exit__() method) were not introduced until 2.6 (well 2.5 if explicitly enabled).

Facies answered 1/10, 2015 at 10:48 Comment(7)
Just tried this under Python 3.4.1 and no strange errors. Obviously, with a different .zip file. If not a Python version issue, could it be a corrupt zip file issue? (Check with zip -T file from command line)Willianwillie
import sys print sys.version; returns: 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]Gastrectomy
@ Martin Evans ; no. when running from idle I do not face any issues, thus the archive gets extracted @nigel222. I tried with different zip file, all return the same problemGastrectomy
@user3438538: which interpreter were you running when you printed the version? Was it within idle, or run using the standalone interpreter? Try printing the version and sys.executable within test.py then execute from idle and then from outside idle. You should see a difference. What output does print sys.executable produce?Facies
[interpreter:] standard Idle which shipes with python 2.7.10. [sys.executable] in idle outputs: C:\Python27\pythonw.exe; outdside idle returns: NoneGastrectomy
@user3438538: how can it return None? What version is the external interpreter?Facies
hmm. I believed I found the cause. when running "import sys print sys.version" outside default idle I get: 2.5.4rc1 (2.5:723492dbab02, Feb 8 2013, 10:13:55) . Stil investigating.......Gastrectomy

© 2022 - 2024 — McMap. All rights reserved.