os.execl()

Ignacio Vazquez-Abrams ignacio at openservices.net
Mon Aug 27 10:26:44 EDT 2001


On Mon, 27 Aug 2001, Brian E Gallew wrote:

> Then <eli at udel.edu> spoke up and said:
> > User-Agent: Pan/0.9.7 (Unix)
> > NNTP-Posting-Host: 65.1.161.108
> > 	I have a question about the ececl function from the os module. I was
> > trying to write a script to untar a bunch of tarballs in a directory. The
> > script looked like this
> >
> > #!/usr/bin/env python
> >
> > import os
> >
> > os.system("ls *.gz > data_file")
> >
> > tar_file = open("data_file"), "r")
> >
> > tar_array = tar_file.readlines()
> >
> > for tarball in tar_array:
> > 	os.execl("tar", "xzvf", tarball)
> >
> > os.system("rm data_file")
>
> That really won't do what you want it to do.  You probably want to do
> something like this:
>
> #! /usr/bin/env python
> import os,glob
>
> cmd = [ '/usr/bin/tar',
> 	'-x',
> 	'-z',
> 	'-f',
>       ]
>
> tar_file_list = glob.glob('*.tar.gz')
>
> for tar_file in tar_file_list:
>     if os.fork():
>        os.wait()
>     else:
>        os.exec(cmd[0], cmd + [tar_file,])

Oh man, you two are doing WAY too much work. Here's my version:

---
#! /bin/bash

for i in *.tar.gz; do
  tar zxvf "$i";
done
---

or if you're running under DOS or Windows:

---
@echo off
for i in (*.tar.gz) do tar zxvf "%%i"
---

Python is nice, but it's not always the best tool for certain tasks.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list