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

Dharman
  • 30,962
  • 25
  • 85
  • 135
george
  • 685
  • 2
  • 9
  • 22
  • 1
    The title says `__exit__`, the error in the question says `extractall`, which one is relevant? – bereal Oct 01 '15 at 09:44
  • 1
    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. – Martin Evans Oct 01 '15 at 09:58

2 Answers2

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

wovano
  • 4,543
  • 5
  • 22
  • 49
Nongi
  • 191
  • 1
  • 9
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).

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 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) – nigel222 Oct 01 '15 at 10:51
  • import sys print sys.version; returns: 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] – george Oct 01 '15 at 10:56
  • @ 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 problem – george Oct 01 '15 at 10:59
  • @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? – mhawke Oct 01 '15 at 11:14
  • 1
    [interpreter:] standard Idle which shipes with python 2.7.10. [sys.executable] in idle outputs: C:\Python27\pythonw.exe; outdside idle returns: None – george Oct 01 '15 at 11:20
  • @user3438538: how can it return `None`? What version is the external interpreter? – mhawke Oct 01 '15 at 11:25
  • 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....... – george Oct 01 '15 at 12:13