views:

553

answers:

8

Hello all, I just started working on python and I have been trying to run an outside executable form python. I have an executable for a program written in Fortran. Lets say the name for the executable is flow.exe. And my executable is lacated in C:\Documents and Settings\flow_model I tried both os.system and popen commands but so far couldnt make it work. The following code seems like opens the command window but wouldnt execute the model.

# Import system modules
import sys, string, os, arcgisscripting
os.system("C:/Documents and Settings/flow_model/flow.exe")

Any suggestions out there? Any help would be greatly appreciated.

+1  A: 

Is that trying to execute C:\Documents with arguments of "and", "Settings/flow_model/flow.exe" ?

Also, you might consider subprocess.call()

Thanatos
+1  A: 

Try

import subprocess
subprocess.call(["C:/Documents and Settings/flow_model/flow.exe"])
int3
tried it and the same thing happened. it seems like it opens the cmd window but then it wouldnt run the executable. I tried the executable separately and it works fine. I also moved the folder as C:/flow_model. Didnt make a difference.??
Mesut
A: 

That's the correct usage, but perhaps the spaces in the path name are messing things up for some reason.

You may want to run the program under cmd.exe as well so you can see any output from flow.exe that might be indicating an error.

Dan Olson
A: 

I like more subprocess.Popen instead of call. It gives you more control. See here for details.

Davide
Irrelevant to the question. call is a shortcut function to Popen anyway...
gahooa
Did you ever use it? `call` just return the exit code. `Popen` has `poll()`, `wait()`, `communicate()` etc. Regarding the relevance, when I wrote my answer, the only other one was this: http://stackoverflow.com/questions/1811691/running-an-outside-program-executable-in-python/1811698#1811698 which currently has one upvote and which (with your metric) should be equally irrelevant
Davide
+1  A: 

Those whitespaces can really be a bother:-(. Try os.chdir('C:/Documents\ and\ Settings/') followed by relative paths for os.system, subprocess methods, or whatever...

If best-effort attempts to bypass the whitespaces-in-path hurdle keep failing, then my next best suggestion is to avoid having blanks in your crucial paths. Couldn't you make a blanks-less directory, copy the crucial .exe file there, and try that? Are those havoc-wrecking space absolutely essential to your well-being...?

Alex Martelli
Alex, thanks. This worked too. I have one more question if you dont mind.How about if my executable is asking for the name of my input file? I tried to do it using stdin but couldn't succeed so far?
Mesut
@mesut, what's that executable using to "ask for" the filename? If it's its own standard-input, for example, you might be able to use `subprocess.Popen` to pipe your desired values there. If it goes deeper (using "console" input), try wexpect, http://sage.math.washington.edu/home/goreckc/sage/wexpect/ .
Alex Martelli
the input file is a basic .txt file in the same folder.The code right now opens the exe from the cmd window. And the executable asks for the name of the input file. Is there a way that I can just define this name (maybe as a path) within the code? I tried subprocess.popen and I tried to look in the variables. I thought that I can use stdin but didnt really work out. I would really appreciate if you can give me a sample with subprocess.popen with the executable located in C:\flow_model, the name of the executable as flow.exe and name of the input file located in the same folder as sac_bsl.txt
Mesut
The "ask for the name of the input file" part is the troublesome one. Not knowing anything about how that exe is coded, as I already mentioned, `wexpect` is the likeliest solution, as it lets you simulate exactly what that exe file would see when invoked at a cmd window.
Alex Martelli
A: 

I'd try inserting an 'r' in front of your path if I were you, to indicate that its a raw string - and then you won't have to use forward slashes, for example:

os.system(r"C:\Documents and Settings\flow_model\flow.exe")
Jon Mills
A: 

your usage is correct, i bet that your external program, flow.exe, needs to be executed in its directory because it accesses some external files stored there.

so you might try:

import sys, string, os, arcgisscripting
os.chdir( 'c:\\documents and settings\\flow_model' )
os.system( '"C:\\Documents and Settings\\flow_model\\flow.exe"' )

(beware of the double quotes inside the single quotes...)

Adrien Plisson
Adrien,Thanks, this worked out.
Mesut
A: 

If it were me, I'd put the exe in the root diretcory (C:) and see if it works like that. If so, it's probably the (already mentioned) spaces in the directory name. If not, it may be some environment variables.

Also, try to check you stderr (using an earlier answer by int3):

import subprocess
process = subprocess.Popen(["C:/Documents and Settings/flow_model/flow.exe"], \
                           stderr=subprocess.PIPE)
if process.stderr:
    print process.stderr.readlines()

The code might not be entirely correct as I usually don't use Popen or windows, but should give the idea. It might well be that the error message is on the error stream.

extraneon