Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 17th, 2006, 5:30 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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)
titaniumdecoy is offline   Reply With Quote
Old Jul 17th, 2006, 5:32 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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?
Polyphemus_ is offline   Reply With Quote
Old Jul 17th, 2006, 6:33 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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.

Last edited by Sane; Jul 17th, 2006 at 6:44 PM.
Sane is offline   Reply With Quote
Old Jul 17th, 2006, 6:58 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jul 17th, 2006, 7:37 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Jul 17th, 2006, 8:36 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
Maybe I'm misinterpreting exactly what os.spawnl does. Why not just use os.system, and use the appropriate call with the appropriate arguments?
Sane is offline   Reply With Quote
Old Jul 18th, 2006, 9:20 AM   #7
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
Game_Ender is offline   Reply With Quote
Old Jul 18th, 2006, 11:42 AM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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()
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
working example badbasser98 C++ 16 Mar 31st, 2006 9:29 AM
Reading and writing files not working under Windows (find under unix/linux) .TD Java 2 Mar 30th, 2006 12:42 PM
DropDownList in DataGrid not working! wright45 ASP 1 Jun 19th, 2005 11:46 AM
working with xml and c# and get this error dark_omen C# 4 May 6th, 2005 6:55 AM
JSP not working on the live server Braveheart Java 0 Mar 9th, 2005 8:20 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 AM.

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