Recommended way to run another program from within a Python script

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


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.


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-5392nn Directory of C:Python25Libsite-packagespythonwinnn[.] [..] dde.pyd license.txtnPythonwin.exe [pywin] scintilla.dll tmp.txtnwin32ui.pyd win32uiole.pyd n 7 File(s) 984,178 bytesn 3 Dir(s) 30,539,644,928 bytes freen'

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

链接地址: http://www.djcxy.com/p/13498.html

上一篇: 在Python中调用Inkscape

下一篇: 推荐使用Python脚本运行另一个程序的方法