29

I have an external executable file which I am trying to run from a Python script. CMD executable runs but without generating output. Probably it exit before output can be generated. Any suggestion about how to delay exit until outputs are generated?

import subprocess, sys
from subprocess import Popen, PIPE
exe_str = r"C:/Windows/System32/cmd C:/temp/calc.exe"

parent = subprocess.Popen(exe_str, stderr=subprocess.PIPE)
Neuron
  • 5,141
  • 5
  • 38
  • 59
Ibe
  • 5,615
  • 7
  • 32
  • 45
  • Maybe you are right. Input data is elevation and flow direction that is why i posted it here. –  Nov 01 '12 at 16:20
  • DOS? Do you not mean a command line interface (CLI) appliction? [MS-DOS](http://en.wikipedia.org/wiki/MS-DOS) was discontinued over 12 years ago. – blah238 Nov 02 '12 at 01:36
  • yes I mean command prompt. thanks for clarifying it. –  Nov 02 '12 at 01:40
  • It worked from SO example using os: `import sys, string, os` `os.chdir( 'c:\\temp' )` `os.system( '"C:\\temp\\calc.exe"' )` Not sure why subprocess isn't working. –  Nov 02 '12 at 02:26
  • Does this answer your question? [how to run an exe file with the arguments using python](https://stackoverflow.com/questions/15928956/how-to-run-an-exe-file-with-the-arguments-using-python) – Thomas Weller Feb 20 '23 at 08:14

4 Answers4

22

use subprocess.call, more info here:

import subprocess
subprocess.call(["C:\\temp\\calc.exe"])

or

import os
os.system('"C:/Windows/System32/notepad.exe"')

i hope it helps you...

urcm
  • 2,274
  • 1
  • 19
  • 45
  • It runs but without generating output. I tested executable using cmd and it requires at least 2-3 sec before generating output. This is the issue, how to make it stay for 3 sec before exit using python? –  Nov 01 '12 at 12:53
  • 1
    Weird. Even with 3 sec condition it didn't generate any output. I increased time but no use. Not sure what else could be the problem. –  Nov 01 '12 at 13:18
14

The os.system method is depreciated and should not be used in new applications. The subprocess module is the pythonic way to do what you require.

Here is an example of some code I wrote a few weeks ago using subprocess to load files, the command you need to use to delay exit until data has been received and the launched program completes is wait():

import subprocess

cmd = "c:\\file.exe"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
process.wait()

On Windows platforms, creationflags=0x08000000 is an optional parameter that suppresses the launch of a window, which can be useful if the program you are calling does not need to be directly seen.

Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
sgrieve
  • 632
  • 1
  • 8
  • 16
  • It is still not generating outputs. Runs perfectly from cmd window but no output using pythonic way as you suggested. –  Nov 02 '12 at 01:37
  • Note, when running on Linux: `ValueError: creationflags is only supported on Windows platforms` – Paul Wintz Jul 28 '23 at 09:01
6

Option 1

import subprocess

subprocess.call('C:\Windows\System32\calc.exe')

Option 2

subprocess.Popen(['C:\Windows\System32\calc.exe'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True).communicate()

Option 3

import os
os.system('C:\Windows\System32\calc.exe')
the.salman.a
  • 945
  • 8
  • 29
0

This worked for me after trying everything else: Change the location of your python program to be the same as where the .exe is located. And then the simple:

subprocess.call("calc.exe")

would work.

Neuron
  • 5,141
  • 5
  • 38
  • 59
DIVYA RATHOD
  • 229
  • 3
  • 15