Running zipfile From the Command Line
zipfile From the Command Line. Python’s
zipfile also offers a minimal command-line interface that allows you to access the module’s main functionality quickly. For example, you can use the
--list option as seen on-screen to list the content of an existing ZIP file.
This command shows the same output as an equivalent call to
.printdir() on the
sample.zip archive. Now, let’s say you want to create a new ZIP file containing several input files.
In this case, you can use the
This command creates a
new_sample.zip file containing the
What if you need to create a ZIP file to archive an entire directory? You may have your own
source_dir/ with the same three files you’ve just seen.
01:14 You can create a ZIP file from that directory using the following command.
With this command,
source_dir/ at the root of the resulting
source_dir.zip file. As you’ve already seen, you can list the archive content by running
zipfile with the
Note that when you use
zipfile to create an archive from the command line, the library implicitly uses the Deflate compression algorithm when archiving the files.
You can also extract all of the content of a given ZIP file using the
--extract option. After running this command, you’ll have a
sample/ folder in your working directory.
The new folder will contain the current files in the
sample.zip archive. The final option that you can use with
zipfile from the command line is
--test. This option allows you to test if a given file is a valid ZIP file.
02:37 In the next section of the course, you’ll take a look at other parts of the Python standard library that can work with ZIP files.
Become a Member to join the conversation.