Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

Python Tutorial Compressing and Extracting Files Using ZipFile Library Full Project

/
/
/
313 Views

Welcome folks today in this blog post i will be talking about how to compress and extract files into a zip file using python built in library called as zipfile. Let’s get started. You can see the step by step youtube video as follows:

READ  Python Tkinter Text to Audio GUI App Using GTTS Library Full Tutorial

 

Compressing Single File

 

In this block of code we will see how to compress a single file using zipfile module in python.

 

import zipfile
         
jungle_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\jungle.zip', 'w')
jungle_zip.write('C:\\Stories\\Fantasy\\jungle.pdf', compress_type=zipfile.ZIP_DEFLATED)
 
jungle_zip.close()

 

READ  Python OpenCV Tutorial To Capture Images From Webcam Full Project

 

Compressing Multiple Files

 

import os
import zipfile
 
fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip', 'w')
 
for folder, subfolders, files in os.walk('C:\\Stories\\Fantasy'):
 
    for file in files:
        if file.endswith('.pdf'):
            fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), 'C:\\Stories\\Fantasy'), compress_type = zipfile.ZIP_DEFLATED)
 
fantasy_zip.close()

 

READ  How to Upload Files to Node.js Express Server Using Express FileUpload Library

So now in this block of code we are compressing a whole directory of pdf files into a single zip file.

 

 

import zipfile
         
fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip')
fantasy_zip.extractall('C:\\Library\\Stories\\Fantasy')
 
fantasy_zip.close()

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :