views:

44

answers:

3

The title says it all... 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. Thanks!

+3  A: 

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

larsmans
A: 

As larsmans says, gzip compression is not used for directories, only single files. The usual way of doing things with linux is to put the directory in a tarball first and then compress it.

Daenyth
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.
synthesizerpatel
+1  A: 

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

tar = tarfile.open( folder_path + ".tgz", "w:gz" )
    for name in os.listdir( folder_path ):
        tar.add(name)
tar.close()
Russell Borogove