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.

Exploring Additional Classes From zipfile

To learn more about Python Zip applications, check out Python’s zipapp: Build Executable Zip Applications.

00:00 Exploring Additional Classes From zipfile. So far, you’ve learned about ZipFile and ZipInfo, which are two of the classes available in zipfile.

00:10 This module also provides two more classes that can be handy in some situations. Those classes are zipfile.Path and zipfile.PyZipFile.

00:20 In this section of the course, you’ll learn the basics of these classes and their main features. When you open a ZIP file with your favorite archiver application, you see the archive’s internal structure.

00:32 You may have files at the root of the archive. You may also have subdirectories with more files. The archive looks like a normal directory on your file system, with each file located at a specific path.

00:47 The zipfile.Path class allows you to construct path objects to quickly create and manage paths to member files and directories inside a given ZIP file.

00:56 The class takes two arguments. root accepts a ZIP file, either as a ZipFile object or a string-based path to a physical ZIP file.

01:04 at holds the location of a specific member file or directory inside the archive. It defaults to the empty string, representing the root of the archive.

01:16 With sample.zip as the target, run the code seen on-screen. This code shows that zipfile.Path implements several features that are common to a pathlib.Path object.

01:29 You can get the name of the file with .name, and you can check if the path points to a regular file with .is_file(). You can check if a given file exists inside a particular ZIP file, and you can read the text of a file.

01:55 Path also provides an .open() method to open a member file using different modes. For example, the code seen on-screen opens hello.txt for reading.

02:07 With Path, you can quickly create a path object pointing to a specific member file in a given ZIP file and access its content immediately using .open().

02:28 Just as with a pathlib.Path object, you can list the contents of a ZIP file using .iterdir() on a zipfile.Path object.

02:47 zipfile.Path provides many useful features that you can use to manage member files in your ZIP archives quickly and efficiently.

02:59 Another useful class in zipfile is PyZipFile. This class is similar to ZipFile, and it’s especially handy when you need to bundle Python modules and packages into ZIP files.

03:10 The main difference from ZipFile is that the initializer of PyZipFile takes an optional argument called optimize, which allows you to optimize the Python code by compiling it into bytecode before archiving it.

03:23 PyZipFile provides the same interface as ZipFile with the addition of .writepy(). This method can take a Python file (.py) as an argument and add it to the underlying ZIP file. If optimize is -1 (the default), then the .py file is automatically compiled to a .pyc file and then added to the target archive. Why does this happen?

03:47 Since version 2.3, the Python interpreter has supported importing Python code from ZIP files, a capability known as Zip imports. This convenient feature allows you to create importable ZIP files to distribute your modules and packages as a single archive.

04:03 PyZipFile is helpful when you need to generate importable ZIP files. Packaging the .pyc file rather than the .py file makes the importing process much more efficient because it skips the compilation step.

04:17 Note that you can also use the ZIP file format to create and distribute Python executable applications, which are commonly known as Python Zip applications.

04:26 To learn how to create them, check out Python’s zipapp: Build Executable Zip Applications. Inside the python-zipfile/ directory, you have a hello.py module with the content seen on-screen.

04:41 As you can see, this code defines a function called greet(), which takes name as an argument and prints a greeting message to the screen.

04:48 Now, let’s say you want to package this incredibly complex module into a ZIP file for distribution purposes. To do that, you can run the code seen on-screen.

05:07 Here the call to .writepy() automatically compiles hello.py to hello.pyc and stores it in hello.zip.

05:17 This becomes clear when you list the archive’s content using .printdir(). Once you have hello.py bundled into a ZIP file, then you can use Python’s import system to import this module from the containing archive.

05:37 The first step to import code from a ZIP file is to make the file available in sys.path. This variable holds a list of strings that specify Python’s search path for modules. To add an item to sys.path, you can use .insert(). For this example to work, you’ll need to change the placeholder path and pass the path to hello.zip on your file system. Once your importable ZIP file is in this list, then you can import your code just like you would do with a regular module.

06:17 Finally, consider the hello/ subdirectory in your working folder. It contains a small Python package with a structure seen on-screen. The __init__.py module turns the hello/ directory into a Python package.

06:31 The hello.py module is the same one you saw just now. Now, if you want to bundle this package into a ZIP file, you can do so as seen on-screen.

06:50 The call to .writepy() takes the hello package as an argument and searches for .py files inside it, compiles them to .pyc files, and finally adds them to the target ZIP file, hello.zip.

07:06 Again, you can import your code from this archive by following the steps you’ve already seen.

07:20 Because the code is in a package now, you first need to import the hello module from the hello package. Then you can access your greet() function normally.

07:39 Throughout this course, you’ve been running zipfile from a Python REPL session. In the next section of the course, you’ll make a change and see how to run zipfile from the command line.

Become a Member to join the conversation.