How to zip a file in Python: Why is "arcname" in "write" function important?

Python zipfile module has methods to make zip files. The below is the simplest code.

from zipfile import ZipFile, ZIP_DEFLATED

with ZipFile('a.zip', 'w', compression=ZIP_DEFLATED) as z:
    z.write('a.jpg', arcname='a.jpg')

a.jpg and the current Python file should be in the same directory. After running the script, a.zip is created.

Unzipping a.zip, a.jpg is made in the same directory.

Zip a file in the other directory

If a Python script and a file you want to zip are not in the same directory, it is better to get the file path using pathlib.

from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED

p = Path(__file__).parent / 'data' / 'a.jpg'

with ZipFile('a.zip', 'w', compression=ZIP_DEFLATED) as z:
    z.write(p.as_posix(), arcname='a.jpg')

As explained in Python Path, Path(__file__) is the current path (to be exact a PosixPath object) and Path(__file__).parent is the directory of the current file.

Python PosixPath can be concatenated with a slash like URL. PosixPath is not string so the above code uses as_posix to get the actual path string.

Unzip as directory

Now is the time for explaining the second argument of write function.

from zipfile import ZipFile, ZIP_DEFLATED

with ZipFile('a.zip', 'w', compression=ZIP_DEFLATED) as z:
    z.write('a.jpg', arcname='d/a.jpg')

In this code, arcname is a relative path. In this case, d directory will be created after unzipping a.zip. d is a folder and contains a.jpg.

Why is arcname important?

arcname is optional but important. You can understand why it is from the following code.

from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED

p = Path(__file__).parent / 'data' / 'a.jpg'

print(p.as_posix())
# /Users/serif/python/test/data/a.jpg

with ZipFile('a.zip', 'w', compression=ZIP_DEFLATED) as z:
    z.write(p.as_posix())

After running the script, a.zip is created. After unzipping a.zip, what happens? If you use a Mac computer, maybe Users directory will be created. Where is a.jpg?

In fact, a.jpg is in an unzipped folder /Users/serif/python/test/data/. If you omit arcname, a full path of an original file is automatically used as new path.

Comments

Powered by Markdown

More