How do I compress a folder with the Python GZip module?

23,677

Solution 1

The code to compress a folder in to tar file is:

import tarfile

tar = tarfile.open("TarName.tar.gz", "w:gz")
tar.add("folder/location", arcname="TarName")
tar.close()

It works for me. Hope that works for you too.

Solution 2

I don't do UI, so you're on your own for getting the folder name from the user. Here's one way to make a gz-compressed tarfile. It does not recurse over subfolders, you'll need something like os.walk() for that.

# assume the path to the folder to compress is in 'folder_path'

import tarfile
import os

with tarfile.open( folder_path + ".tgz", "w:gz" ) as tar:
    for name in os.listdir( folder_path ):
        tar.add(name)

Solution 3

GZip doesn't do compression of folders/directories, only single files. Use the zipfile module instead.

Share:
23,677
Noah R
Author by

Noah R

Updated on July 05, 2022

Comments

  • Noah R
    Noah R almost 2 years

    I'm creating Python software that compresses files/folders... How would I create a section of the code that asks for the user input of the folder location and then compresses it. I currently have the code for a single file but not a folder full of files. Please explain in detail how to do this.

  • synthesizerpatel
    synthesizerpatel over 13 years
    It's worth mentioning that you can get better compression when you tar and then compress than if you were to compress each file individually.
  • Jim Oldfield
    Jim Oldfield over 8 years
    Actually you can just write tar.add(folder_path) and it will be added recursively. (Of course, this could be a new feature added in the five years since this answer was posted!) Also, the for loop shouldn't be indented here, unless you change tar = tarfile.open(...) to with tarfile.open(...) as tar: (which is a good idea, and then you can get rid of the tar.close() line).
  • Russell Borogove
    Russell Borogove over 8 years
    Thanks, not sure how that indent error survived for 5 years.
  • rer
    rer over 5 years
    Don't you need to have tar.add(os.path.join(folder_path, name))?