12

Possible Duplicate:
How to call external command in Python

I'm writing a Python script on a windows machine. I need to launch another application "OtherApp.exe". What is the most suitable way to do so?

Till now I've been looking at os.system() or os.execl() and they don't quite look appropriate (I don't even know if the latter will work in windows at all).

Community
  • 1
  • 1
Frederick The Fool
  • 31,355
  • 20
  • 78
  • 112

2 Answers2

5

The recommended way is to use the subprocess module. All other ways (like os.system() or exec) are brittle, unsecure and have subtle side effects that you should not need to care about. subprocess replaces all of them.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
-1

Note that this answer is specific to python versions 2.x which I am locked to due to an embedded system. I am only leaving it for historical reasons just in case.

One of the things the subprocess offers is a method to catch the output of a command, for example using [popen] on a windows machine

import os
os.popen('dir').read()

will yield

' Volume in drive C has no label.\n Volume Serial Number is 54CD-5392\n\n Directory of C:\Python25\Lib\site-packages\pythonwin\n\n[.] [..] dde.pyd license.txt\nPythonwin.exe [pywin] scintilla.dll tmp.txt\nwin32ui.pyd win32uiole.pyd \n 7 File(s) 984,178 bytes\n 3 Dir(s) 30,539,644,928 bytes free\n'

Which can then be parsed or manipulated any way you want.

Dennis
  • 611
  • 2
  • 6
  • 10
  • the subprocess module has replaced os.popen in newer version of Python. That is the recommended way to handle this. http://docs.python.org/library/subprocess.html – Corey Goldberg May 26 '09 at 17:18