Search code examples
pythonpython-zipfile

Python zipfile module creates multiple files with same name


I have following code in python:

>>> import zipfile
>>> zip = zipfile.ZipFile('abc.zip', 'w')
>>> zip.writestr('myfile', 'This is sample text')
>>> zip.writestr('myfile', 'This is sample text')
>>> zip.close()

This creates an archive with two files having exactly same name and path.

Why is this?

enter image description here


Solution

  • This is allowed by Zip and some other archive formats, like Tar, and even addressed by the Python API:

    Note: The open(), read() and extract() methods can take a filename or a ZipInfo object. You will appreciate this when trying to read a ZIP file that contains members with duplicate names.