How to run external executable using Python?

Ibe picture Ibe · Nov 1, 2012 · Viewed 88.8k times · Source

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)

Answer

Aragon picture Aragon · Nov 1, 2012

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...