Search code examples
pythonziparchive

ZipFile module creating .pyc file


I can't find any info on this but when I try and create a zip archive in Python it creates a .pyc instead.

#!/Python27/python

import zipfile

z = zipfile.ZipFile('test.zip', 'w')
z.write('README.txt')
z.close()

This is the result of running the script.

enter image description here


Solution

  • The script you're running (or some other script you have) is actually called zipfile.py, and so Python is actually first looking in the folder your script is in to find a module called zipfile. When it finds this script, it imports that instead of the actual module.

    Any time a script is imported to another Python file, Python automatically creates a compiled .pyc file resulting in that zipfile.pyc. If you rename your file to something more specific (and also fix your typo) you should be able to avoid this problem.