List Info

Thread: Trouble getting os.execl() command to work




Trouble getting os.execl() command to work
user name
2007-02-10 21:26:56
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. Seems pretty simple. I print the resulting command line and it looks fine, but os.execl() won't seem to execute it. It tells me "no such file or directory". Yet I can cut and paste the printed line onto the command line and execute it and it works fine. Am I missing something? Here's the code:


import os
import string

# get a list of the files in the current working directory

filelist = os.listdir(os.getcwd())

# run through the list and convert all of them to lowercase

for name in filelist:
  ;  lowered_name = string.lower(name)
    print name + " -> " + lowered_name
    os.rename (name,lowered_name)


# 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':
   ;     jpg_dest = name[:-4]+".jpg"
        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 ??

Re: Trouble getting os.execl() command to work
country flaguser name
United States
2007-02-11 00:07:38
See my comments in-line with the rest of the e-mail.
Richard Querin wrote:
> import os
> import string
>
> # get a list of the files in the current working
directory
>
> filelist = os.listdir(os.getcwd())
Ok we have a list of all the files
>
> # run through the list and convert all of them to
lowercase
>
> for name in filelist:
we loop over the list
>     lowered_name = string.lower(name)
>     print name + " -> " + lowered_name
>     os.rename (name,lowered_name)
and rename any that are not lowercase.
>
>
> # 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:
oops! filelist still contains the non-normalized names of
the files!
>
>     #extract extension
>     ext = name[-3:]
>
>     if ext == 'cr2':
>         jpg_dest = name[:-4]+".jpg"
>         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 ??
It can't find the files because of this.

HTH,
-Luke
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: Trouble getting os.execl() command to work
country flaguser name
United Kingdom
2007-02-11 02:57:26
"Richard Querin" <rfqueringmail.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 
> ??

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://ww
w.freenetpages.co.uk/hp/alan.gauld 


_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: Trouble getting os.execl() command to work
user name
2007-02-11 07:05:28


On 2/11/07, Luke Paireepinart < rabidpoobeargmail.com">rabidpoobeargmail.com> wrote:

> for name in filelist:
oops! filelist still contains the non-normalized names of the files!
>;

Dang! Thank you sir. I should have recaptured the file list before continuing on.


Alan - thanks for the great info as well. I will check it out and hopefully streamline it. I haven't worked with the os module much before, thanks for pointing me in the right direction.


 


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )