os.execl()

Grant Edwards grante at visi.com
Mon Aug 27 10:30:33 EDT 2001


In article <200108271401.f7RE1P67003979 at smtp6.andrew.cmu.edu>, Brian E Gallew wrote:

>> 	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)

The call to exec replaces the current program with the "tar"
program.  It will never return.

>> 
>> 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,])

The os.spawnv() function is simpler to use:

  for tar_file in tar_file_list:
      os.spawnv(os.P_WAIT,"/bin/tar",("xzvf",tar_file))

I haven't tested this, so I'm sure there's a typo somewhere...

-- 
Grant Edwards                   grante             Yow!  Used staples are good
                                  at               with SOY SAUCE!
                               visi.com            



More information about the Python-list mailing list