简体   繁体   中英

Gzip a tar file in python

Is there an easy way to gzip a tar file in python so when I unzip, I can directly get the contents? I can create a gzipped file from a tar file but on untaring I still get the tar file rather than contents.

Thanks, Jason

Yes, but this is a system issue, not Python.

tar -xzf <tar_zip_file>

The -z flag works on creation, as well.

Thanks to the good people at link to the docs here.

getting info out of a tar.gz file

import tarfile
with tarfile.open("sample.tar.gz", 'r:gz') as tfile:
   with tfile.extractfile('somedir/somefile.txt') as target_file:
       data = target_file.read()

getting info into a tar.gz file

import tarfile
with tarfile.open('sample.tar.gz','w:gz') as tar:
    tar.add('dir_or_file')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM