AttributeError: the 'module' object does not have the 'ZipFile' attribute

as I said, this is a very strange question. Hope you guys could help me resolve this issue thanks

following 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()

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'
+5
source share
2 answers

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

+10
source

Your module imports itself as a zipfile. Call it something other than zipfile.py

+3
source

All Articles