Running zipfile From the Command Line
00:00
Running 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 -l
or --list
option as seen on-screen to list the content of an existing ZIP file.
00:22
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.
00:36
In this case, you can use the -c
or --create
option.
00:48
This command creates a new_sample.zip
file containing the hello.txt
, lorem.md
, and realpython.md
files.
01:05
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.
01:23
With this command, zipfile
places 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 -l
option.
01:39
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.
01:50
You can also extract all of the content of a given ZIP file using the -e
or --extract
option. After running this command, you’ll have a sample/
folder in your working directory.
02:06
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 -t
or --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.