Zipfile#

There is a special tool in Python that allows you to work with ZIP files. This page explores the options available in this library.

import shutil
import seedir as sd
from pathlib import Path
from zipfile import ZipFile

ZipFile object#

The ZipFile object behaves like a regular file stream. You have to specify file and mode.


The following example opens an archive with mode='w', so a new archive is created at the path specified in file.

archive_path = Path('experimental.zip')
zip = ZipFile(file=archive_path, mode='w')
zip.close()

The following cell demonstrates that the file was indeed created and then immediately deletes it.

print("File exists - ", archive_path.exists())
archive_path.unlink()
File exists -  True

Archive’s content#

With ZipFile, you can manipulate files inside an archive. The following methods are available:

  • write: Writes a file to the archive.

  • namelist: Allows you to check the contents of the archive.

  • extractall and extract: Allows you to extract files from the archive to the regular filesystem.


The following cell creates filesystem that we’ll use for example.

toy_folder = Path("toy_folder")
toy_folder.mkdir()

archieve_file_path = (toy_folder/"archived_file")
archieve_file_path.write_text("this is some message")

inner_folder = toy_folder/"inner_folder"
inner_file = inner_folder/"file"
inner_folder.mkdir()
inner_file.write_text("test")

sd.seedir(toy_folder)
toy_folder/
├─inner_folder/
│ └─file
└─archieved_file

Now, we are creating an archive and writing files to it.

archive_path = Path("archive.zip")

with ZipFile(file=archive_path, mode='w') as a:
    a.write(archieve_file_path)
    a.write(inner_file)

The following cell displays the contents of the archive, confirming that it matches what we created in the previous cell.

with ZipFile(file=archive_path, mode="r") as a:
    print(a.namelist())
['toy_folder/archieved_file', 'toy_folder/inner_folder/file']

Now, using extractall, we extract the entire contents of the archive. The file tree of the extracted folder is printed below.

test_extraction_folder = Path("test_extraction")
with ZipFile(file=archive_path, mode="r") as f:
    f.extractall(test_extraction_folder)
sd.seedir(test_extraction_folder)
shutil.rmtree(test_extraction_folder)
test_extraction/
└─toy_folder/
  ├─inner_folder/
  │ └─file
  └─archieved_file

After the experiments, we will delete the toy files.

archive_path.unlink()
shutil.rmtree(toy_folder)