How to Extract All Files and Folders From a Zip File in Python



Python


In this article, we show how to extract all file and folders from a zip file in Python.

So Python can be used to extract all files and folders within a zip file.

This can be done through the zipfile module.

The zipfile module allows us to work with zip files, doing things such as creating zip files, reading zip files, extracting all files and folders from the zip file, and adding files to zip files.

So if you want an example of a zip file if you don't have your own, you can use this basic zip file here: documents.zip.

This is a simple zip file composed of 4 files: document1.txt, document2.txt, document3.txt, and document4.txt

This is a zip file, meaning it is compressed.

In our code below, we will extract all files from this zip file, documents.zip.



So the first thing we need to do is import the zipfile module.

Again, this is the module that allows us to work and do functions on zip files.

We import the os module just to show the current working directory that we are working with, where the zip file is located and where the extracted files will be.

We then create a variable, myzipfile, which is created to hold a zipfile object.

myzipfile is now a zipfile object we can perform functions on.

The function we use on this zipfile object, myzipfile, is extractall()

Using the extractall() function, we extract all the files and folders from the zip file we are working with; these files will all appear in the same directory that the original zip file is located in.

We then close the myzipfile object once we are done with all that we need to do with it.

If you look inside the folder of the file you are working with, you should see all the extracted files within this folder.

It is worth nothing that you don't have to extract the files into the same folder that the zip file is located. You can extract the files into any folder.

If you want to extract the files into a different folder than the zip file is located in, you pass in the folder into the extractall() function. The extractall() function will then either place the files in the existing directory that you specified, or if you specify a folder that does not exist, the extractall() function will create the folder and place the extracted files in that folder.

This is shown in the following code below.



So everything is the same as above but instead of using the extractall() function with no parameters passed into it, we specify 'documents' in the function.

If the documents folder exists, the extracted files will be placed in it. If the documents folder does not exist, the extractall() function will create the folder and then place the extracted files inside of it. So either way, the extracted files will be placed inside of the documents folder.

The typical convention is to name the folder that you place the extracted files in as the same name of the zip file. This is what Windows does by default when you are not using the Python programming language.

If you simply want to extract a single file from a zip file, then you would use the extract() function and pass into it 1 or 2 parameters. The first parameter is the file that you want to extract. The second parameter is the location where you want to extract it to.

The code below extracts the 'document1.txt' file from zipfile object.



So this again is how to extract a single file from a zipfile object in Python, just so that you can know how to do so. This program extracts the file, 'document1.txt' from the zipfile object and places the extracted file in the documents folder. All other code is the same as above.

But the much more common thing that is done is simply extracting all files from a zip folder.

And this is how we can extract all files and folders from a zip file in Python.


Related Resources

How to Create a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...