Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'module' object has no attribute 'ZipFile'

Tags:

python

just like i said,it's a very strange question.i hope you guys could help me solve this question thanks

the following is my code:

import os
import zipfile

filename = "E:\\test.zip"
currdir = "E:\\vpn\\"

os.chdir(currdir)

tfile = zipfile.ZipFile(filename, 'w')
files = os.listdir(currdir)
for f in files:
    tfile.write(f)

for f in tfile.namelist():
    print "added %s"%f

tfile.close()

the error message:

Traceback (most recent call last):
  File "C:\pydemo\src\gzip\zipfile.py", line 7, in <module>
    import zipfile
  File "C:\pydemo\src\gzip\zipfile.py", line 14, in <module>
tfile = zipfile.ZipFile.(filename, 'w')
AttributeError: 'module' object has no attribute 'ZipFile'
like image 273
bspeng922 Avatar asked Jan 30 '13 01:01

bspeng922


2 Answers

You called your script zipfile.py, which means it is trying to import itself. Change the name of the file to basically anything else.

like image 185
David Robinson Avatar answered Sep 19 '22 22:09

David Robinson


Your module is importing itself as zipfile. Call it something other than zipfile.py

like image 24
greggo Avatar answered Sep 21 '22 22:09

greggo