Python Unzip – Extract Files from Zip to Directory
Python provides a built-in module called “zipfile” that allows you to easily unzip all files in a directory, preserving the original directory structure in the process.
In this article, we will show you how to use Python to unzip files in a directory, and provide tips and examples to help you get started.
- Before using the Python code provided in this article, make sure you have the “zipfile” library installed on your computer. You can install it using pip by running one of the following commands:
pip install zipfile38 pip install zipfile36
The above step will download zipfile library & install in your computer. Then use the python unzip code in next sections.
Python Unzip all Files in directory – Code
Once the library is installed, you can use the following Python code to unzip files in a directory.
Just like adding files to a Zip archvie this Python code also uses the ‘Zipfile’ Python open source library.
#Import Libraries import zipfile #Directory path to extract files dirpath = 'E:\ArchiveUnzip' #Create Zipfile object to process the Archive file. with zipfile.ZipFile('E:\Archive.zip', 'r') as unzipObject: #Extract all files in one Go unzipObject.extractall(dirpath) print ("All files in Zip archive are extracted")
- Make sure to replace the directory path and zip file path in the above code with the appropriate values for your specific use case. Once the code is executed, you can verify the destination path to ensure that all the files in the archive have been extracted to the given destination directory and subdirectory paths.
- In addition to the “extractall” function, the “zipfile” module also provides other functions that allow you to extract specific files from a zip archive, or extract files to a different directory than the one specified in the archive. You can find more information about these functions in the official Python documentation.
The first line of the code imports the “zipfile” library, which allows us to work with zip files in Python. The next line specifies the directory path where the files will be extracted. This can be any directory on your computer where you have write permissions.
The following line creates a “Zipfile” object, which we will use to process the archive file. The ‘r’ parameter in the “ZipFile” function specifies that the file will be opened in read mode. The path of the archive file is also passed as an argument to the function.
The next line, “unzipObject.extractall(dirpath)”, is where the actual extraction takes place. This function extracts all the files in the archive to the directory specified by the “dirpath” variable. The extractall() function automatically preserves the original directory structure of the archive.
Finally, the last line of the code prints a message to indicate that the extraction is complete.
Additional Info on UnZip
It’s important to note that you should replace the directory path and zip file path in the above code with the appropriate values for your specific use case. Once the code is executed, you can verify the destination path to ensure that all the files in the archive have been extracted to the given destination directory and subdirectory paths.
In addition to the “extractall” function, the “zipfile” module also provides other functions that allow you to extract specific files from a zip archive, or extract files to a different directory than the one specified in the archive. You can find more information about these functions in the official Python documentation.
In conclusion, Python’s built-in “zipfile” module makes it easy to extract files from a zip archive, preserving the original directory structure in the process. With just a few lines of code, you can extract all the files in a zip archive to a specific directory. This is a simple yet powerful solution that can save you time and effort when working with large zip
External Reference:
- Discussion on how to unzip files from a directory – Click here.
- Learn how to Zip a file in Python – Click here.