Learn how to create zipped and unzipped files using Python

Learn how to create zipped and unzipped files using Python

Screenshot (120).png

What can you see in the image above? If you said a zip and unzip file then you're correct.

Every computer user knows how to create a zip file using their operating system's GUI.

I know you're a computer programmer and I want you to do it the "Pythonista" way.

What are zip files?

These are compressed files that make transferring data from one location to another easier. Zipped files are sent at higher speeds than unzipped files. The file format of Zip files is ".zip"

If you're ready to write some codes on how to create your Zip files and also extract the contents, then let's start!

How to Create a Zip File.

Step1

Let's create some files that you want to zip through the "creating of a file in python" in the working directory.

#create the first file
f = open('hashnode.txt', 'w+')
f.write("I'm a member of hashnode bloggers")
f.close()

#create the second file
f = open('blog_profile.txt', 'w+')
f.write("My blog's name is Jiresimon...A blog dedicated to python, C and related tech articles")
f.close()

Here's an image below showing the created files in the working directory.

Screenshot 2022-02-25 044655.png

After creating the files, you'll have to create a zip file that will contain the files - hashnode.txt, and blog_profile.txt

Step 2

Import the Zip file module to create the Zip file module.

import zipfile

#create a variable called "create_zip" and follow the syntax used to create the zip file
create_zip = zipfile.ZipFile("zipfile.zip",'w')

Step 3

Add the created files into the Zip folder.

#adding the created files to the zip file named "zipfile"

create_zip.write('hashnode.txt',compress_type=zipfile.ZIP_DEFLATED)
create_zip.write('blog_profile.txt',compress_type=zipfile.ZIP_DEFLATED)

#close the file 
create_zip.close()

Screenshot 2022-02-25 045519.png Well-done...You've created a Zip file.

How to Unzip or Extract the files from the Zip File

Let's extract the files from the Zip file.

Step 1

Extract the files in the zipped file to another folder.

#To extract the files in the zipped file...The created folder with the extracted file is created in the same working directory

#Folder for the extracted file is "extracted_files"; Zip folder name = "zipfile"
extract_zipfile = zipfile.ZipFile('zipfile.zip','r')
extract_zipfile.extractall('extracted_files')

Screenshot 2022-02-25 050131.png
Screenshot 2022-02-25 050226.png

Yes!!! You've been able to create a Zip file and extract the contents of a zip file.

Big Question

WERE THE STEPS MANY?

Interesting...Here's a module that makes things easier.

It's called the "shutil module."

Step 1

import shutil

#get the current working directory and assign it to a variable..."pwd" can do that on a jupyter notebook or copy it from your terminal on Vscode.

zip_file2 = 'C:\\Users\\user\\Desktop\\zip file\\ZIP AND UNZIP FOLDER\\extracted_files'

Step 2

Create a folder to contain the zip file.

output_filename = 'zipped_extracted_files'
shutil.make_archive(output_filename,'zip',zip_file2)

#output
'C:\\Users\\user\\Desktop\\zip file\\ZIP AND UNZIP FOLDER\\zipped_extracted_files.zip'

The output from the Jupyter notebook shows that the zip file has been created.

Step 4

Extract the files from the zipped file to another folder.

#extracting the zipped files using the "shutil module"
shutil.unpack_archive('zipped_extracted_files.zip','unzipped_extracted_files','zip,)

Conclusion

A Zip file can be created either through the "zipfile module" or the "shutil module." The "shutil module" makes zipping and unzipping of files easier. Congrats on completing another python exercise.