Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   os.spawnl not working (http://www.programmingforums.org/showthread.php?t=10754)

titaniumdecoy Jul 17th, 2006 6:30 PM

os.spawnl not working
 
I can run the following command in Windows command line successfully (assuming I have cd'ed to the proper directory:

:

> Reader.exe sig5C "C:\path\to\file.ext"

I am trying to run this command from a Python script. What I have so far is below. It launches the program but it quits before it processes the files. What am I doing wrong?

:

fp = r'C:\path\to\file.ext'
READER = r'C:\path\to\Reader.exe'

os.spawnl(os.P_WAIT, READER, READER, 'sig5C', '"%s"'%fp)


Polyphemus_ Jul 17th, 2006 6:32 PM

Not sure how this is with Python so there exists the possibility I'm completely wrong, but shouldn't you use, like in most other programming languages, a double backslash?

Sane Jul 17th, 2006 7:33 PM

Quote:

Originally Posted by Polyphemus_
in most other programming languages, a double backslash?

Well, unlike "most other programming languages", Python tries to avoid pointless things for the sake of making things more difficult. The "r" before the literal is there for a purpose, it isn't there for decoration. It voids the forward slash's use as an escape character.

:

print 'first line\nsecond line'
print r'\nsecond line'



@OP: The program may not like arguments with forward slashes in them. Try writing a python script that spits out the arguments (sys.argv). Then direct your code to open that script and see what the arguments are translating to within the script.

EDIT!!

Oh, I totally missed the first thing you said. About it working with those arguments! The third parameter in os.spawnl is where the arguments begin, yet you began 'sig5C' on the fourth parameter. Your code has three additional arguments, when your working example has only two. It seems that you've repeated "READER" excessively.

titaniumdecoy Jul 17th, 2006 7:58 PM

Quote:

Originally Posted by Sane
Oh, I totally missed the first thing you said. About it working with those arguments! The third parameter in os.spawnl is where the arguments begin, yet you began 'sig5C' on the fourth parameter. Your code has three additional arguments, when your working example has only two. It seems that you've repeated "READER" excessively.

The same error occurs whether or not I pass in the program to execute twice, and although the os.spawnl docs would seem to agree with what you say, every example I have found online seems to follow this pattern. So, I'm trying everything both ways.

Quote:

Originally Posted by Sane
The program may not like arguments with forward slashes in them. Try writing a python script that spits out the arguments (sys.argv). Then direct your code to open that script and see what the arguments are translating to within the script.

That's a good idea, and I tried it, and am having even more problems. I created two scripts, poster.py and getter.py. Here is poster.py:

:

import os
execpath = os.path.abspath('getter.py')
os.spawnl(os.P_WAIT, execpath, 'arg1', 'arg2')

Can you see anything wrong with this? (I get an identical error regardless of whether the second argument is repeated.) I think you're right about the problem having something to do with slashes/spaces, but I can't figure it out.

:

Traceback (most recent call last):
 File "C:\Documents and Settings\jboyle\Desktop\DataView\optargs\poster.py", line 3, in ?
os.spawnl(os.P_WAIT, PATH, PATH, 'arg1', 'arg2')
 File "C:\Python24\Lib\os.py", line 597, in spawnl
return spawnv(mode, file, args)
OSError: [Errno 8] Exec format error

Script terminated.


The Dark Jul 17th, 2006 8:37 PM

I think you have to pass READER in twice, as the second READER is argv[0] (in C speak). That seemed to work for me, parameters and all.
It doesn't seem to let you exec python programs, you will need to invoke python.exe and pass it the parameter of the .py program you want to run.

Sane Jul 17th, 2006 9:36 PM

Maybe I'm misinterpreting exactly what os.spawnl does. Why not just use os.system, and use the appropriate call with the appropriate arguments?

Game_Ender Jul 18th, 2006 10:20 AM

You should really use a Popen object instead. Those old functions are deprecated. Here is the page from the python library reference. Remeber the docs are your friend and will usually give you answer quicker than a forum. You will also see some stuff you never knew existed in your digging.

titaniumdecoy Jul 18th, 2006 12:42 PM

Thank you, Game_Ender! Popen works!

:

import subprocess as sub

fp = r'C:\path\to\file.ext'
READER = r'C:\path\to\Reader.exe'

cmd = '%s %s "%s"' % (READER, 'sig5C', fp)

p = sub.Popen(cmd)
p.wait()



All times are GMT -5. The time now is 12:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC