Showing posts with label python zip file. Show all posts
Showing posts with label python zip file. Show all posts

Solved : How to zip and unzip files in Python



In this post we will see how to zip and unzip files in python using two modules zipfile and shutil .

You can execute below commands in Jupyter notebook or IDE like Pycharm.

Note: If you are looking for python zip() function check here.

Create a zip using zipfile

First we will use a python module called zipfile to create a zip file containing multiple files.

#import required modules
import zipfile

#Let's create a zip file first by giving it a name
new_zip = zipfile.ZipFile('zip_file.zip','w')
#Add two files which we want to zip
new_zip.write("abc.txt",compress_type=zipfile.ZIP_DEFLATED)
new_zip.write("fileloop.py",compress_type=zipfile.ZIP_DEFLATED)

#Close the zip file
new_zip.close()

So, with the above steps we have created a zip file called zip_file.zip . Now let's try to extract those files in a directory.

Unzip using zipfile 

#import required modules
import zipfile

#First open the file in read mode
unzip_files = zipfile.ZipFile('zip_file.zip','r')

#Now we will unzip and put those files in a directory called extracted_dir
unzip_files.extractall("extracted_dir")

Great! so now our files are extracted in a directory called extracted_dir .


Let's try doing the same using other python module called shutil

Create a zip using shutil

First we will zip a directory.

#import required modules
import shutil
import os

#Give name of your final zipped file. .zip extension will be added automatically.
output_file = 'zip_file_new'

# Give the name of directory which you want to zip.
# If you are in same location as the directory you can simply give it's name or
# else give full path of directory.
# Check your current directory using this command
print(os.getcwd())

#full path of directory to be zipped
zip_dir = 'E:\Software\Python\CloudVedas\extracted_dir'

#Create a zip archive
shutil.make_archive(output_file,'zip',zip_dir)

Note: If your folder name has n in it at the start e.g. new_extracted_dir in that case you will have to escape it using extra "\" since \n will be treated as new line by python so in that case file path should be mentioned as 'E:\Software\Python\CloudVedas\\new_extracted_dir'

So, above we have created a zip file called zip_file_new.zip


Unzip using shutil

Now let's unzip the file zip_file_new.zip we have created earlier .

#import required modules
import shutil

#zipped file full path
zipped_file = 'E:\Software\Python\CloudVedas\zip_file_new.zip'

#full path of directory to where the zipped files will be extracted
extracted_shutil_dir = 'E:\Software\Python\CloudVedas\extracted_dir_new'

#extract the files
shutil.unpack_archive(zipped_file,extracted_shutil_dir,'zip')

Above steps has extracted all the files from our zip and put them in a directory called extracted_dir_new .

In this post we have seen two methods of zipping and unzipping files in python. Do let us know in comments section if you have any query or feedback.

If you want to extract an archive in linux shell you can check our post on how archive or unarchive using tar command.