Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Creating and Populating ZIP Files

00:01 Creating and Populating ZIP Files. So far, you’ve learned how to work with existing ZIP files. You’ve learned to read, write, and append member files to them by using different modes of ZipFile.

00:13 You’ve also learned how to read relevant metadata and how to extract the content of a given ZIP file. In this section of the course, you’ll code a few practical examples that will help you learn how to create ZIP files from several input files and from an entire directory using zipfile and other Python tools.

00:30 You’ll also learn how to use zipfile for file compression and more. Sometimes you need to create a ZIP archive from several related files. This way, you can have all the files in a single container for distributing them over a computer network or sharing with friends and colleagues.

00:49 To this end, you can create a list of target files and write them into an archive using ZipFile and a loop.

01:03 Here you create a ZipFile object with a desired archive name as its first argument. The "w" mode allows you to write member files into the final ZIP file.

01:14 The for loop iterates over your list of input files and writes them into the underlying ZIP file using .write(). Once the execution flow exits the with statement, ZipFile automatically closes the archive, saving the changes for you.

01:34 Now you have a multiple_files.zip archive containing all the files from your original list of files. Bundling the content of a directory into a single archive is another everyday use case for ZIP files.

01:48 Python has several tools that you can use with ZipFile to approach this task. For example, you can use pathlib to read the content of a given directory, and with that information, you can create a container archive using ZipFile.

02:03 In the python-zipfile/ directory, you have a subdirectory called source_dir/ with the content seen on-screen. In source_dir/, you have three regular files.

02:13 Because the directory doesn’t contain subdirectories, you can use pathlib.Path.iterdir() to iterate directly over its content. With this idea in mind, on-screen you’ll see how to build a ZIP file from the content of source_dir/.

02:36 Here you create a pathlib path object from source directory. The first with statement creates a ZipFile object ready for writing.

02:51 Then the call to .iterdir() returns an iterator over the entries in the underlying directory. Because you don’t have any subdirectories in source_dir/, the .iterdir() function yields only files. The for loop iterates over the files and writes them into the archive. In this case, you pass file_path.name to the second argument of .write().

03:14 This argument is called arcname and holds the name of the member file inside the resulting archive. All of the examples you’ve seen so far rely on the default value of arcname, which is the same filename you passed as the first argument to .write().

03:30 If you don’t pass file_path.name to arcname, then your source directory will be at the root of your ZIP file, which can also be a valid result depending on your needs.

03:43 Now check out the root_dir/ folder in your working directory. You should find the structure seen on-screen. You have the usual files and a subdirectory with a single file in it.

03:54 If you want to create a ZIP file with this same internal structure, then you need a tool that recursively iterates through the directory tree under root_dir/. On-screen, you can see how to zip a complete directory tree like this one using zipfile along with Path.rglob() from the pathlib module.

04:13 First you import pathlib and zipfile. Then you specify a pathlib.Path for the root directory. The new archive is created.

04:32 You use Path.rglob() to recursively traverse the directory tree under root_dir/.

04:41 Then you write every file and sub directory to the target ZIP archive. This time you use Path.relative_to to get the relative path to each file, and then pass the result to the second argument of .write(). This way, the resulting ZIP file ends up with the same internal structure as your original source directory. Again, you can get rid of this argument if you want your source directory to be at the root of your ZIP file.

05:12 In the next section of the course, you’ll look further into the creation of ZIP files, starting with the creation of compressed files and directories.

Become a Member to join the conversation.