Introduction
When coping with giant quantities of knowledge or recordsdata, you would possibly end up needing to compress recordsdata right into a extra manageable format. The most effective methods to do that is by creating a zipper archive.
On this article, we’ll be exploring how one can create a zipper archive of a listing utilizing Python. Whether or not you are trying to save house, simplify file sharing, or simply maintain issues organized, Python’s zipfile
module supplies a to do that.
Making a Zip Archive with Python
Python’s commonplace library comes with a module named zipfile
that gives strategies for creating, studying, writing, appending, and itemizing contents of a ZIP file. This module is beneficial for creating a zipper archive of a listing. We’ll begin by importing the zipfile
and os
modules:
import zipfile
import os
Now, let’s create a operate that can zip a listing:
def zip_directory(directory_path, zip_path):
with zipfile.ZipFile(zip_path, 'w') as zipf:
for root, dirs, recordsdata in os.stroll(directory_path):
for file in recordsdata:
zipf.write(os.path.be a part of(root, file),
os.path.relpath(os.path.be a part of(root, file),
os.path.be a part of(directory_path, '..')))
On this operate, we first open a brand new zip file in write mode. Then, we stroll by the listing we wish to zip. For every file within the listing, we use the write()
methodology so as to add it to the zip file. The os.path.relpath()
operate is used in order that we retailer the relative path of the file within the zip file, as an alternative of absolutely the path.
Let’s check our operate:
zip_directory('test_directory', 'archive.zip')
After operating this code, you must see a brand new file named archive.zip
in your present listing. This zip file comprises all of the recordsdata from test_directory
.
Observe: Watch out when specifying the paths. If the zip_path
file already exists, will probably be overwritten.
Python’s zipfile
module makes it straightforward to create a zipper archive of a listing. With only a few strains of code, you’ll be able to compress and arrange your recordsdata.
Within the following sections, we’ll dive deeper into dealing with nested directories, giant directories, and error dealing with. This may increasingly appear a bit backwards, however the above operate is probably going what most individuals got here right here for, so I wished to indicate it first.
Utilizing the zipfile Module
In Python, the zipfile
module is the most effective software for working with zip archives. It supplies capabilities to learn, write, append, and extract information from zip recordsdata. The module is a part of Python’s commonplace library, so there isn’t any want to put in something further.
This is a easy instance of how one can create a brand new zip file and add a file to it:
import zipfile
zip_file = zipfile.ZipFile('instance.zip', 'w')
zip_file.write('check.txt')
zip_file.shut()
On this code, we first import the zipfile
module. Then, we create a brand new zip file named ‘instance.zip’ in write mode (‘w’). We add a file named ‘check.txt’ to the zip file utilizing the write()
methodology. Lastly, we shut the zip file utilizing the shut()
methodology.
Making a Zip Archive of a Listing
Creating a zipper archive of a listing includes a bit extra work, nevertheless it’s nonetheless pretty straightforward with the zipfile
module. You could stroll by the listing construction, including every file to the zip archive.
import os
import zipfile
def zip_directory(folder_path, zip_file):
for folder_name, subfolders, filenames in os.stroll(folder_path):
for filename in filenames:
file_path = os.path.be a part of(folder_name, filename)
zip_file.write(file_path)
zip_file = zipfile.ZipFile('example_directory.zip', 'w')
zip_directory('/path/to/listing', zip_file)
zip_file.shut()
We first outline a operate zip_directory()
that takes a folder path and a ZipFile
object. It makes use of the os.stroll()
operate to iterate over all recordsdata within the listing and its subdirectories. For every file, it constructs the complete file path and provides the file to the zip archive.
The os.stroll()
operate is a handy approach to traverse directories. It generates the file names in a listing tree by strolling the tree both top-down or bottom-up.
Observe: Watch out with the file paths when including recordsdata to the zip archive. The write()
methodology provides recordsdata to the archive with the precise path you present. For those who present an absolute path, the file will probably be added with the complete absolute path within the zip archive. That is often not what you need. As a substitute, you sometimes wish to add recordsdata with a relative path to the listing you are zipping.
In the principle a part of the script, we create a brand new zip file, name the zip_directory()
operate so as to add the listing to the zip file, and at last shut the zip file.
Working with Nested Directories
When working with nested directories, the method of making a zipper archive is a little more sophisticated. The primary operate we confirmed on this article truly handles this case as properly, which we’ll present once more right here:
import os
import zipfile
def zipdir(path, ziph):
for root, dirs, recordsdata in os.stroll(path):
for file in recordsdata:
ziph.write(os.path.be a part of(root, file),
os.path.relpath(os.path.be a part of(root, file),
os.path.be a part of(path, '..')))
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('/path/to/listing', zipf)
zipf.shut()
The principle distinction is that we’re truly creating the zip listing exterior of the operate and cross it as a parameter. Whether or not you do it inside the operate itself or not is as much as private desire.
Dealing with Giant Directories
So what if we’re coping with a big listing? Zipping a big listing can eat a variety of reminiscence and even crash your program should you do not take the correct precautions.
Fortunately, the zipfile
module permits us to create a zipper archive with out loading all recordsdata into reminiscence directly. Through the use of the with
assertion, we will be sure that every file is closed and its reminiscence freed after it is added to the archive.
Take a look at our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and really study it!
import os
import zipfile
def zipdir(path, ziph):
for root, dirs, recordsdata in os.stroll(path):
for file in recordsdata:
with open(os.path.be a part of(root, file), 'r') as fp:
ziph.write(fp.learn())
with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
zipdir('/path/to/listing', zipf)
On this model, we’re utilizing the with
assertion when opening every file and when creating the zip archive. It will assure that every file is closed after it is learn, liberating up the reminiscence it was utilizing. This fashion, we will safely zip giant directories with out operating into reminiscence points.
Error Dealing with in zipfile
When working with zipfile
in Python, we have to bear in mind to deal with exceptions so our program would not crash unexpectedly. The commonest exceptions you would possibly encounter are RuntimeError
, ValueError
, and FileNotFoundError
.
Let’s check out how we will deal with these exceptions whereas creating a zipper file:
import zipfile
attempt:
with zipfile.ZipFile('instance.zip', 'w') as myzip:
myzip.write('non_existent_file.txt')
besides FileNotFoundError:
print('The file you are attempting to zip doesn't exist.')
besides RuntimeError as e:
print('An sudden error occurred:', str(e))
besides zipfile.LargeZipFile:
print('The file is just too giant to be compressed.')
FileNotFoundError
is raised when the file we’re making an attempt to zip would not exist. RuntimeError
is a normal exception that could be raised for quite a few causes, so we print the exception message to know what went improper. zipfile.LargeZipFile
is raised when the file we’re making an attempt to compress is just too large.
Observe: Python’s zipfile
module raises a LargeZipFile
error when the file you are making an attempt to compress is bigger than 2 GB. For those who’re working with giant recordsdata, you’ll be able to forestall this error by calling ZipFile
with the allowZip64=True
argument.
Frequent Errors and Options
Whereas working with the zipfile
module, you would possibly encounter a number of frequent errors. Let’s discover a few of these errors and their options:
FileNotFoundError
This error occurs when the file or listing you are making an attempt to zip doesn’t exist. So all the time verify if the file or listing exists earlier than trying to compress it.
IsADirectoryError
This error is raised while you’re making an attempt to write down a listing to a zipper file utilizing ZipFile.write()
. To keep away from this, use os.stroll()
to traverse the listing and write the particular person recordsdata as an alternative.
PermissionError
As you most likely guessed, this error occurs when you do not have the required permissions to learn the file or write to the listing. Ensure you have the right permissions earlier than making an attempt to control recordsdata or directories.
LargeZipFile
As talked about earlier, this error is raised when the file you are making an attempt to compress is bigger than 2 GB. To stop this error, name ZipFile
with the allowZip64=True
argument.
attempt:
with zipfile.ZipFile('large_file.zip', 'w', allowZip64=True) as myzip:
myzip.write('large_file.txt')
besides zipfile.LargeZipFile:
print('The file is just too giant to be compressed.')
On this snippet, we’re utilizing the allowZip64=True
argument to permit zipping recordsdata bigger than 2 GB.
Compressing Particular person Recordsdata
With zipfile
, not solely can it compress directories, however it might additionally compress particular person recordsdata. As an example you’ve gotten a file known as doc.txt
that you just wish to compress. This is the way you’d try this:
import zipfile
with zipfile.ZipFile('compressed_file.zip', 'w') as myzip:
myzip.write('doc.txt')
On this code, we’re creating a brand new zip archive named compressed_file.zip
and simply including doc.txt
to it. The ‘w’ parameter implies that we’re opening the zip file in write mode.
Now, should you verify your listing, you must see a brand new zip file named compressed_file.zip
.
And eventually, let’s examine find out how to reverse this zipping by extracting the recordsdata. As an example we wish to extract the doc.txt
file we simply compressed. This is find out how to do it:
import zipfile
with zipfile.ZipFile('compressed_file.zip', 'r') as myzip:
myzip.extractall()
On this code snippet, we’re opening the zip file in learn mode (‘r’) after which calling the extractall()
methodology. This methodology extracts all of the recordsdata within the zip archive to the present listing.
Observe: If you wish to extract the recordsdata to a particular listing, you’ll be able to cross the listing path as an argument to the extractall()
methodology like so: myzip.extractall('/path/to/listing/')
.
Now, should you verify your listing, you must see the doc.txt
file. That is all there may be to it!
Conclusion
On this information, we centered on creating and managing zip archives in Python. We explored the zipfile
module, discovered find out how to create a zipper archive of a listing, and even dove into dealing with nested directories and huge directories. We have additionally lined error dealing with inside zipfile, frequent errors and their options, compressing particular person recordsdata, and extracting zip recordsdata.