tags:

views:

371

answers:

3

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

A: 

os.system() should be fine. It's the simplest way. There's also the subprocess module, which is intended to replace os.system() over time. It gives you more control, if you need it.

unwind
-1 os.system() has lots of security issues; you really shouldn't recommend it anymore.
Aaron Digulla
+3  A: 

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
A: 

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