[Tutor] Trouble getting os.execl() command to work

Alan Gauld alan.gauld at btinternet.com
Sun Feb 11 09:57:26 CET 2007

"Richard Querin" <rfquerin at gmail.com> wrote

> I'm having a slight problem here. I've got a script (shown below) 
> which is
> run from the command line. I am converting the filenames to 
> lowercase and
> then, for each .cr2 file, I'm building a command line and running 
> it.
> os.execl() won't seem to execute it.

execl() is probably the wrong tool for this particular job.
os.system would be easier.
But better still is to use the new subprocess module.
Look at the subprocess documentation for examples of running
simple commands.

> # run through the list again and for all .cr2 files run
> # the exiftool command to copy the exif data from cr2 to jpg file
> for name in filelist:
>    #extract extension
>    ext = name[-3:]
>    if ext == 'cr2':

You could do this more easily and more reliably using the
glob module.

for name in glob.glob('*.cr2'):

And it avoids the problem of using the old list of
mixed case names too.

>        jpg_dest = name[:-4]+".jpg"

You could also use the os.path.splitext() function here if you want
to avoid the slicing, but the benefit is less obvious in this case...

>        cmd_string = "/home/richard/ExifTool/exiftool -TagsFromFile " 
> + name
> + " -exif:all " + jpg_dest
>        print cmd_string    #this string looks correct
>        os.execl(cmd_string)   #the resulting command throws an error 
> ??


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list