To learn more about Python Zip applications, check out Python’s zipapp: Build Executable Zip Applications.
Exploring Additional Classes From zipfile
Exploring Additional Classes From
zipfile. So far, you’ve learned about
ZipInfo, which are two of the classes available in
This module also provides two more classes that can be handy in some situations. Those classes are
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.
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.
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.
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.
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
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.
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.
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
Just as with a
pathlib.Path object, you can list the contents of a ZIP file using
.iterdir() on a
zipfile.Path provides many useful features that you can use to manage member files in your ZIP archives quickly and efficiently.
Another useful class in
PyZipFile. This class is similar to
ZipFile, and it’s especially handy when you need to bundle Python modules and packages into ZIP files.
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.
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
-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.
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.
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.
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.
Here the call to
.writepy() automatically compiles
hello.pyc and stores it in
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.
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.
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.
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.
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,
07:06 Again, you can import your code from this archive by following the steps you’ve already seen.
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.
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.