Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87708 > unrolled thread
| Started by | PK Khatri <piseema@gmail.com> |
|---|---|
| First post | 2015-03-18 16:22 -0700 |
| Last post | 2015-03-18 20:28 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | PK Khatri <piseema@gmail.com> |
|---|---|
| Date | 2015-03-18 16:22 -0700 |
| Subject | how should i add extension and unzip again? |
| Message-ID | <7604e075-4d1f-4e53-b1f6-28696044e2bd@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-03-18 23:58 +0000 |
| Message-ID | <mailman.14.1426723088.10327.python-list@python.org> |
| In reply to | #87708 |
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)
[toc] | [prev] | [next] | [standalone]
| From | PK Khatri <piseema@gmail.com> |
|---|---|
| Date | 2015-03-18 20:28 -0700 |
| Message-ID | <97366c15-8f34-45e5-9ea9-f837a793dc5f@googlegroups.com> |
| In reply to | #87710 |
On Wednesday, March 18, 2015 at 4:58:34 PM UTC-7, MRAB wrote: > 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) Wow!! Thank you so much, that did it. thank you. PK.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web