13

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

2

2 Answers 2

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.

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

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.