how should i add extension and unzip again?
MRAB
python at mrabarnett.plus.com
Wed Mar 18 19:58:03 EDT 2015
On 2015-03-18 23:22, PK Khatri wrote:
> This script untars .gz file to file without extension
> --------
> import gzip
> import glob
> import os.path
> import tarfile
>
> source_dir = "C:\\TEST"
> dest_dir = "C:\\TEST"
> for src_name in glob.glob(os.path.join(source_dir, '*.gz')):
> base = os.path.basename(src_name)
> dest_name = os.path.join(dest_dir, base[:-3])
> with tarfile.open(src_name, 'r') as infile:
> with open(dest_name, 'w') as outfile:
> for line in infile:
> outfile.write(str(line))
> ---------
> but i know it is a compressed file so I need to add extension .zip to it and run untar/unzip one more time so it will fully unzip to folders and files, not sure how, can someone help please?
> Thank you.
>
There's a method called 'extractall':
with tarfile.open(src_name, 'r') as infile:
infile.extractall(dest_dir)
More information about the Python-list
mailing list