Introduction
When coping with massive quantities of knowledge or recordsdata, you would possibly end up needing to compress recordsdata right into a extra manageable format. Among the best 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 seeking to save area, simplify file sharing, or simply hold 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 helpful 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 part of(root, file),
os.path.relpath(os.path.be part of(root, file),
os.path.be part of(directory_path, '..')))
On this operate, we first open a brand new zip file in write mode. Then, we stroll by way of the listing we wish to zip. For every file within the listing, we use the write()
technique 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 take a look at our operate:
zip_directory('test_directory', 'archive.zip')
After operating this code, it is best to see a brand new file named archive.zip
in your present listing. This zip file accommodates all of the recordsdata from test_directory
.
Be aware: Watch out when specifying the paths. If the zip_path
file already exists, it is going to be overwritten.
Python’s zipfile
module makes it straightforward to create a zipper archive of a listing. With just some strains of code, you may compress and manage your recordsdata.
Within the following sections, we’ll dive deeper into dealing with nested directories, massive directories, and error dealing with. This will likely 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 one of the best instrument for working with zip archives. It supplies features to learn, write, append, and extract knowledge from zip recordsdata. The module is a part of Python’s commonplace library, so there is no want to put in something additional.
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('take a look at.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 ‘take a look at.txt’ to the zip file utilizing the write()
technique. Lastly, we shut the zip file utilizing the shut()
technique.
Making a Zip Archive of a Listing
Creating a zipper archive of a listing entails a bit extra work, but it surely’s nonetheless pretty straightforward with the zipfile
module. It’s essential stroll by way of 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 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 option to traverse directories. It generates the file names in a listing tree by strolling the tree both top-down or bottom-up.
Be aware: Watch out with the file paths when including recordsdata to the zip archive. The write()
technique provides recordsdata to the archive with the precise path you present. In the event you present an absolute path, the file will likely be added with the complete absolute path within the zip archive. That is normally 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 primary 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 eventually 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 really 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 part of(root, file),
os.path.relpath(os.path.be part of(root, file),
os.path.be part of(path, '..')))
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('/path/to/listing', zipf)
zipf.shut()
The primary distinction is that we’re really creating the zip listing outdoors of the operate and move it as a parameter. Whether or not you do it throughout the operate itself or not is as much as private choice.
Dealing with Massive 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 if you happen to 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. By utilizing the with
assertion, we are able to be sure that every file is closed and its reminiscence freed after it is added to the archive.
Try our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and truly be taught 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 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 manner, we are able to safely zip massive 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 are able to deal with these exceptions whereas creating a zipper file:
import zipfile
strive:
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 simply too massive to be compressed.')
FileNotFoundError
is raised when the file we’re making an attempt to zip would not exist. RuntimeError
is a common exception that is perhaps raised for quite a few causes, so we print the exception message to grasp what went fallacious. zipfile.LargeZipFile
is raised when the file we’re making an attempt to compress is simply too massive.
Be aware: Python’s zipfile
module raises a LargeZipFile
error when the file you are making an attempt to compress is bigger than 2 GB. In the event you’re working with massive recordsdata, you may forestall this error by calling ZipFile
with the allowZip64=True
argument.
Widespread 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 put in writing 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. Be sure to have the proper permissions earlier than making an attempt to govern 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.
strive:
with zipfile.ZipFile('large_file.zip', 'w', allowZip64=True) as myzip:
myzip.write('large_file.txt')
besides zipfile.LargeZipFile:
print('The file is simply too massive to be compressed.')
On this snippet, we’re utilizing the allowZip64=True
argument to permit zipping recordsdata bigger than 2 GB.
Compressing Particular person Information
With zipfile
, not solely can it compress directories, however it could possibly additionally compress particular person recordsdata. As an example you could have a file known as doc.txt
that you simply wish to compress. This is the way you’d do 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, if you happen to verify your listing, it is best to see a brand new zip file named compressed_file.zip
.
And at last, let’s have a look at how you can reverse this zipping by extracting the recordsdata. As an example we wish to extract the doc.txt
file we simply compressed. This is how you can 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()
technique. This technique extracts all of the recordsdata within the zip archive to the present listing.
Be aware: If you wish to extract the recordsdata to a selected listing, you may move the listing path as an argument to the extractall()
technique like so: myzip.extractall('/path/to/listing/')
.
Now, if you happen to verify your listing, it is best to see the doc.txt
file. That is all there’s to it!
Conclusion
On this information, we targeted on creating and managing zip archives in Python. We explored the zipfile
module, discovered how you can create a zipper archive of a listing, and even dove into dealing with nested directories and huge directories. We have additionally coated error dealing with inside zipfile, frequent errors and their options, compressing particular person recordsdata, and extracting zip recordsdata.