Monday 6 September 2021

Zipping Files and Folders using Python

This is the python code to zip a file:

import zipfile
zip_file = zipfile.ZipFile('temp.zip','w')
zip_file.write('test.txt',compress_type=zipfile.ZIP_DEFLATED)
zip_file.close()


Before the code is run : 







After the code is run :



You can see 'temp.zip' is created which is the zipped file of 'test.txt'  .



Now let's look into the code line by line. 


Line 1 :

 zipfile is a class of zipfile module for reading and writing zip files.


Line 2 : 

Here zip file object is created(zip_file), 'temp.zip' is the name of the zip file you want to make. 

zipfile.ZipFile is the class for reading and writing ZIP files.

We use 'w' because here the system wants to create a new zip file.


Line 3 : 

'test.txt' is the file you want to zip.

ZIP_DEFLATED is the numeric constant for the usual ZIP compression method.


Line 4 :

zip_file.close() is used to close the archive file before exiting the program



Next is the python code to zip a folder:

import shutil
try:
    shutil.make_archive('new','zip','test')
except Exception as e:
    print(e)
else:
    print('Zipping Done')


Before the code is run :




After the code is run :



You can see 'new'  is created which is the zipped file of the folder 'test' .



Now let's look into the code line by line. 


Line 1 :

shutil module offers a number of high-level operations on files and collections of files.


Line 3 :

shutil.make_archive create an archive file(such as zip or tar) and return its name.

'new' is the name of the zip file you want to make.

'test' is the name of the folder you want to zip.


If you folder is zipped successfully you'll be prompted as 'Zipping Done'.
 


2 comments:

Introduction to the Python Calendar Module

 The Calendar module is built into Python 3. But for some reason it is installed by default. We can install it to  - Windows Administrator u...