Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87710
| Date | 2015-03-18 23:58 +0000 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: how should i add extension and unzip again? |
| References | <7604e075-4d1f-4e53-b1f6-28696044e2bd@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14.1426723088.10327.python-list@python.org> (permalink) |
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)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how should i add extension and unzip again? PK Khatri <piseema@gmail.com> - 2015-03-18 16:22 -0700
Re: how should i add extension and unzip again? MRAB <python@mrabarnett.plus.com> - 2015-03-18 23:58 +0000
Re: how should i add extension and unzip again? PK Khatri <piseema@gmail.com> - 2015-03-18 20:28 -0700
csiph-web