python-zipfile

Using Python zipfile but Windows cannot unzip the resulting file


Working through an example on RealPython

import zipfile

filenames = [r"..\Binary\titanic_train.csv"
             , r"..\Binary\titanic_train.csv"]

with zipfile.ZipFile(r"..\Binary\multiple_files.zip", mode="w") as archive:
    for filename in filenames:
        archive.write(filename)

It works fine - in Python, but Windows can't uncompress the file. Says it is invalid. Any idea where I am going astray? Thanks in advance.


Solution

  • The problem is the path. It would appear the Zipfile method doesn't like the use of the relative paths in the pathname. It works (in Windows AND Python) when I use absolute paths.