Created
March 22, 2018 17:07
-
-
Save mherkazandjian/37f0bbc2a845b6846996b3f98ff40e8b to your computer and use it in GitHub Desktop.
snippet for extracting a tar (or .tar.gz file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tarfile | |
def extract_tar_gz_file(in_fpath: str, out_dir: str): | |
""" | |
Extract a zipped tar archive | |
:param in_fpath: the path to the .tar.gz file | |
:param out_dir: the path of the extracted content | |
""" | |
tar = tarfile.open(os.path.expanduser(in_fpath)) | |
for member, name in zip(tar.getmembers(), tar.getnames()): | |
fobj = tar.extractfile(member) | |
if fobj is not None: | |
dirname = os.path.dirname(name) | |
_out_dir = os.path.join(out_dir, dirname) | |
if not os.path.isdir(_out_dir): | |
os.makedirs(_out_dir, exist_ok=True) | |
_out_fpath = os.path.join(_out_dir, os.path.basename(name)) | |
log.info('extract {}'.format(name)) | |
with open(_out_fpath, 'wb') as _fobj: | |
_fobj.write(fobj.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment