Compress and extract folders in Python

The module 'tarfile' provides the functionality to compress and extract folders.

To create compressed file,

import tarfile
with tarfile.open('folder.tar.gz', "w:gz") as tar:
        tar.add('folder')

Where 'folder' is the name of the folder that need to be compressed.

To extract compressed file,

import tarfile
t = tarfile.open('folder.tar.gz', 'r')
t.extractall('')

Where 'folder.tar.gz' is the file that need to be extracted.

 

Leave a Reply

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