How to create a complete compressed tar file using Python?

How to create a .tar.gz file with compression in Python?

+86
python compression zip tarfile
Jan 09 '10 at 4:59
source share
5 answers

To create .tar.gz (aka .tgz ) for the entire directory tree:

 import tarfile def make_tarfile(output_filename, source_dir): with tarfile.open(output_filename, "w:gz") as tar: tar.add(source_dir, arcname=os.path.basename(source_dir)) 
+147
Jun 13 '13 at 6:58
source share
— -
 import tarfile tar = tarfile.open("sample.tar.gz", "w:gz") for name in ["file1", "file2", "file3"]: tar.add(name) tar.close() 

If you want to create a compressed tar.bz2 file, simply replace the file extension names ".tar.bz2" and "w: gz" with "w: bz2".

+83
Jan 09 '10 at 5:17
source share

You call tarfile.open with mode='w:gz' , which means "Open gzip for compressed mail."

You will probably want to end the file name ( name argument with open ) with .tar.gz , but this does not affect compression options.

By the way, you usually get better compression with the 'w:bz2' , just as tar can compress even better with bzip2 than it can compress with gzip .

+30
Jan 09 '10 at 5:19
source share

In previous answers, it is recommended to use the python tarfile module to create a .tar.gz file in python. This is obviously a good Python-style solution, although it does have a serious flaw in archiving speed. This question mentions that tarfile about twice as slow as calling a direct command on Linux. In my experience, this estimate is pretty true.

Therefore, for faster archiving, you can use the direct Linux command using the subprocess module:

 subprocess.call(['tar', '-czf', output_filename, file_to_archive]) 
+2
Jul 19 '19 at 11:55
source share

In this Compressing the tar.gz file in an open browsing directory In the solution, use os.path.basename (file_directory)

 with tarfile.open("save.tar.gz","w:gz"): for file in ["a.txt","b.log","c.png"]: tar.add(os.path.basename(file)) 

its use in tar.gz directory file compression

0
Sep 08 '19 at 17:42
source share



All Articles