compression level with tarfile (w:gz) ?

Esmail ebonak at
Mon Aug 10 14:50:21 CEST 2009


I was wondering if it possible to specify a compression level when I
tar/gzip a file in Python using the tarfile module. I would like to
specify the highest (9) compression level for gzip.


    t ='.tar.gz', mode='w:gz:9')

When I create a simple tar and then gzip it 'manually' with compression
level 9, I get a smaller archive than when I have this code execute with
the w:gz option.

Is the only way to accomplish the higher rate to create a tar file
and then use a different module to gzip it (assuming I can specify
the compression level there)?


My current code:

def tar_it_up(target_dir_name, tar_file_name=None):
     tar up target_dir_name directory and create a
     tar/zip file with base name tar_file_name

     appends a date/timestamp to tar_file_name

     time_string = time.strftime("_%b_%d_%Y_%a_%H_%M")

     if tar_file_name is None:
         tar_file_name = target_dir_name

     tar_file_name += time_string

     print ('Creating archive of %s ...' % target_dir_name),

     t ='.tar.gz', mode='w:gz')
#   t ='.tar', mode='w')

     print ('saved to %s.tar.gz' % tar_file_name)

More information about the Python-list mailing list