Compress / decompress tarfile
1
import tarfile
with tarfile.open('test.tar.gz', 'w:gz') as tr:
tr.add('test_dir')
#test_test the directory called dir.tar.Compress to tarfile with filename gz
with tarfile.open('test.tar.gz', 'r:gz') as tr:
tr.extractall(path='test_tar')
#test_test in a directory called tar.tar.Expand gz
2
import tarfile
with tarfile.open('test.tar.gz', 'r:gz') as tr:
with tr.extractfile('test_dir/sub_dir/sub_test.txt') as f:
print(f.read())
#test compressed into tarfile.tar.sub without expanding gz_test.Output txt