In many cases, you may need to compress or decompress files using Python. This can be done easily using the built-in zipfile
module. In this article, we will go through several methods to zip and unzip files using Python.
Method 1: Using the zipfile module
The zipfile
module provides classes for reading and writing ZIP files. To create a ZIP file, we can use the ZipFile
class. To extract the contents of a ZIP file, we can use the extractall()
method of the ZipFile
class. Here’s an example:
import zipfile # Create a new ZIP file with zipfile.ZipFile('example.zip', 'w') as zip: # Add some files to the ZIP file zip.write('file1.txt') zip.write('file2.txt') # Extract the contents of the ZIP file with zipfile.ZipFile('example.zip', 'r') as zip: zip.extractall()
In the above example, we imported the zipfile
module and created a new ZIP file called “example.zip” using the ZipFile
class. We added two files to the ZIP file using the write()
method. We then extracted the contents of the ZIP file using the extractall()
method.
Method 2: Using the shutil module
The shutil
module provides a higher-level interface for working with files and directories. It includes functions for copying, moving, and deleting files, as well as archiving and compressing files. To create a ZIP file using the shutil
module, we can use the make_archive()
function. To extract the contents of a ZIP file using the shutil
module, we can use the unpack_archive()
function. Here’s an example:
import shutil # Create a new ZIP file shutil.make_archive('example', 'zip', '.', 'file1.txt', 'file2.txt') # Extract the contents of the ZIP file shutil.unpack_archive('example.zip', '.')
In the above example, we imported the shutil
module and created a new ZIP file called “example.zip” using the make_archive()
function. We specified the format of the archive as “zip”, the root directory as “.”, and the files to include in the archive as “file1.txt” and “file2.txt”. We then extracted the contents of the ZIP file using the unpack_archive()
function.
Method 3: Using the gzip module
The gzip
module provides a way to compress and decompress files using the gzip format. To compress a file using the gzip
module, we can use the GzipFile
class. To decompress a file using the gzip
module, we can use the open()
function. Here’s an example:
import gzip # Compress a file with open('file.txt', 'rb') as f_in: with gzip.open('file.txt.gz', 'wb') as f_out: f_out.write(f_in.read()) # Decompress a file with gzip.open('file.txt.gz', 'rb') as f_in: with open('file.txt', 'wb') as f_out: f_out.write(f_in.read())
In the above example, we imported the gzip
module and compressed a file called “file.txt” using the GzipFile
class. We used the open()
function to open the input file in binary mode and the output file in write binary mode. We then wrote the compressed data to the output file.
We then demonstrated how to decompress a gzip file using the open()
function. We opened the compressed file in binary mode using the gzip.open()
function and the output file in write binary mode using the open()
function. We then wrote the decompressed data to the output file.
Method 4: Using the tarfile module
The tarfile
module provides classes for reading and writing tar archives, which are commonly used in Unix and Linux systems. To create a tar archive, we can use the TarFile
class. To extract the contents of a tar archive, we can use the extractall()
method of the TarFile
class. Here’s an example:
import tarfile # Create a new tar archive with tarfile.open('example.tar', 'w') as tar: # Add some files to the tar archive tar.add('file1.txt') tar.add('file2.txt') # Extract the contents of the tar archive with tarfile.open('example.tar', 'r') as tar: tar.extractall()
In the above example, we imported the tarfile
module and created a new tar archive called “example.tar” using the TarFile
class. We added two files to the tar archive using the add()
method. We then extracted the contents of the tar archive using the extractall()
method.
Conclusion
In this article, we covered several methods for zipping and unzipping files using Python. We demonstrated how to use the zipfile
, shutil
, gzip
, and tarfile
modules to compress and decompress files. Each method has its own advantages and disadvantages, so choose the method that best suits your needs.