Running an external program (executable) in Python?

I was just starting to work on Python, and I was trying to run an external executable with Python.

I have an executable file for a program written in Fortran. Suppose the name of the executable file is flow.exe. And my executable is located in C:\Documents and Settings\flow_model . I tried both the os.system and popen commands, but so far I have not been able to get it to work. The following code looks like it opens a command window, but it will not execute the model.

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

How can i fix this?

+76
python executable
Nov 28 '09 at 5:36
source share
16 answers

These spaces can really bother :-( Try os.chdir('C:/Documents\ and\ Settings/') , followed by relative paths for the os.system , subprocess methods or something else ...

If trying as hard as you can to get around obstacles with spaces in the path continues to fail, then my next best suggestion is to avoid having spaces in your main paths. Could you make a directory without spaces, copy the key .exe and try which one ? Are these places for the destruction of chaos absolutely important for your well-being ...?

+37
Nov 28 '09 at 6:08
source share

If you are using Python 2.7 or higher (especially before Python 3.5), you can use the following:

 import subprocess 
  • subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) Runs the command described by the arguments. Waits for the command to complete, then returns the return code attribute.
  • subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False) Runs a command with arguments. Awaiting completion of a command. If the return code was null, then it is returned; otherwise, a CalledProcessError occurs. The CalledProcessError object will have a return code in the returncode attribute

Example: subprocess.check_call([r"C:\pathToYourProgram\yourProgram.exe", "your", "arguments", "comma", "separated"])

On regular Python strings, the \ U character combination signals exit from the Unicode extended code point.

Here is the link to the documentation: http://docs.python.org/3.2/library/subprocess.html

For Python 3. 5+, you can now use run () in many cases: https://docs.python.org/3.5/library/subprocess.html#subprocess.run

+39
Nov 08
source share

I would try inserting "r" in front of your path, if you were you to indicate that it is a raw string, and then you do not have to use slashes. For example:

 os.system(r"C:\Documents and Settings\flow_model\flow.exe") 
+21
Nov 28 '09 at 7:42
source share

The easiest way:

 import os os.startfile("C:\Documents and Settings\flow_model\flow.exe") 

It is working; I tried.

+20
May 01 '12 at 8:17
source share

Your use is correct. I am sure that your external program, flow.exe, must be executed in its directory, because it accesses some external files stored there.

So you can 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 double quotes inside single quotes ...)

+19
Nov 28 '09 at 9:08
source share

Use a subprocess , this is a smaller module, so it runs .exe faster.

 import subprocess subprocess.Popen([r"U:\Year 8\kerbal space program\KSP.exe"]) 
+9
Oct 06 '14 at 12:00
source share

Using os.system :

 import os os.system(r'"C:/Documents and Settings/flow_model/flow.exe"') 
+7
Oct. 16 '14 at 20:51
source share

Try

 import subprocess subprocess.call(["C:/Documents and Settings/flow_model/flow.exe"]) 
+3
Nov 28 '09 at 5:39
source share

Is this an attempt to execute C:\Documents with arguments "and", "Settings/flow_model/flow.exe" ?

Alternatively, you can consider subprocess.call() .

+1
Nov 28 '09 at 5:39
source share

If it were me, I would put the EXE file in the root directory (C :) and see if it works like that. If so, it’s possible the (already mentioned) spaces in the directory name. If not, these may be some environment variables.

Also try checking stderr (using an earlier int3 answer):

 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 may not be entirely correct, since I usually do not use Popen or Windows, but should give this idea. It is possible that the error message is in the error stream.

+1
Nov 28 '09 at 11:43
source share

This is a correct use, but perhaps spaces in the path name are useless for some reason.

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

0
Nov 28 '09 at 5:40
source share

for the above question this solution works.

just change the path to where your executable is located.

 import sys, string, os os.chdir('C:\\Downloads\\xpdf-tools-win-4.00\\xpdf-tools-win-4.00\\bin64') os.system(r"C:\\Downloads\\xpdf-tools-win-4.00\\xpdf-tools-win-4.00\bin64\\flowwork.exe) '''import sys, string, os os.chdir('C:\\Downloads\\xpdf-tools-win-4.00\\xpdf-tools-win-4.00\\bin64') os.system(r"C:\\Downloads\\xpdf-tools-win-4.00\\xpdf-tools-win-4.00\bin64\\pdftopng.exe test1.pdf rootimage")''' 

Here is test1.pdf rootimage for my code.

0
Aug 16 '18 at 5:34
source share
 import os path = "C:/Documents and Settings/flow_model/" os.chdir(path) os.system("flow.exe") 
0
Oct. 15 '18 at 21:36
source share

There are many different solutions, and the results will largely depend on:

  • OS you use: Windows, Cygwin, Linux, MacOS
  • used version of python: Python2 or Python3x

Since I discovered some things that claim to work only on Windows, this does not happen, probably because I use Cygwin, which outwitted the way the OS works with Windows paths. Other things only work on a clean nix-based OS or in Python2 or 3.

Here are my findings:

  • Generally speaking, os.system() is the most forgiving method.
  • os.startfile() is the least forgiving. (Windows && only, if you're lucky)
  • subprocess.Popen([...]) not recommended
  • subprocess.run(winView, shell=True) recommended!
  • Remembering that using subprocess for anything can be a security risk .

Try it:

 import os, subprocess ... winView = '/cygdrive/c/Windows/explorer.exe %s' % somefile ... # chose one of these: os.system(winView) subprocess.Popen(['/cygdrive/c/Windows/explorer.exe', 'somefile.png']) subprocess.run(winView, shell=True) 

Q: Why do you want to use explorer on Windows?

A: Because if you just want to see the results of a new file, explorer will automatically open the file using any default Windows program installed for this type of file. Therefore, there is no need to re-specify the default program.

0
Nov 03 '18 at 2:53
source share
 The Only best solution is: import win32api win32api.WinExec('notepad.exe') 
0
Dec 05 '18 at 8:51
source share

in Python 2.6, use the string enclosed in quotation marks "and apostrophes". Also change single / double //. Your working example would look like this:

 import os os.system("'C://Documents and Settings//flow_model//flow.exe'") 

You can also use any parameters if your program accepts them.

 os.system('C://"Program Files (x86)"//Maxima-gcl-5.37.3//gnuplot//bin//gnuplot -e "plot [-10:10] sin(x),atan(x),cos(atan(x)); pause mouse"') 

finally, you can use a string variable, as an example - plotting using gnuplot directly from python:

 this_program='C://"Program Files (x86)"//Maxima-gcl-5.37.3//gnuplot//bin//gnuplot' this_par='-e "set polar; plot [-2*pi:2*pi] [-3:3] [-3:3] t*sin(t); pause -1"' os.system(this_program+" "+this_par) 
0
Jan 25 '19 at 5:57
source share



All Articles