Python調用外部程序——os.system()和subprocess.call

經過os.system函數調用其餘程序

預備知識:cmd中打開和關閉程序

cmd中打開程序python

a.打開系統自帶程序shell

系統自帶的程序的路徑通常都已加入環境變量之中,只需在cmd窗口中直接輸入程序名稱便可。ide

以notepad爲例,直接在cmd窗口中輸入notepad後回車便可打開。函數

b.打開本身安裝且沒有加入環境變量的程序ui

以網易雲音樂爲例,在cmd窗口中須要輸入完整的程序路徑 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"。this

注意,雙引號是必須的。spa

若將網易雲音樂的路徑加入環境變量之中,則在cmd窗口中輸入cloudmusic後回車便可運行。code

在cmd中關閉程序orm

在cmd中關閉程序能夠使用taskkill命令,語法以下:blog

taskkill /f /t /im 進程名

注意,這裏只須要程序的進程名便可,而非完整路徑名。

仍以網易雲音樂爲例,在cmd窗口中輸入 taskkill /f /t /im cloudmusic.exe 後回車便可關閉網易雲音樂。

以下圖所示:

Python調用外部程序——os.system()和subprocess.call

a.os.system方法

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system, and has the same limitations. Changes tosys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait. Note that POSIX does not specify the meaning of the return value of the Csystemfunction, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after runningcommand, given by the Windows environment variableCOMSPEC: oncommand.comsystems (Windows 95, 98 and ME) this is always0; oncmd.exesystems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocessmodule provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See theReplacing Older Functions with the subprocess Modulesection in thesubprocessdocumentation for some helpful recipes.

Availability: Unix, Windows.

os模塊中的system函數能夠方便地運行其餘程序或者腳本。其函數原型爲:

os.system(command)

command 爲要執行的命令,近似於Windows下cmd窗口中輸入的命令。

若是要向程序或者腳本傳遞參數,能夠使用空格分隔程序及多個參數。

1 status = os.system("mycmd" + " myarg")2 # becomes3 status = subprocess.call("mycmd" + " myarg", shell=True)

Notes:

  • Calling the program through the shell is usually not required.

A more realistic example would look like this:

1 try:2 retcode = call("mycmd" + " myarg", shell=True)3 if retcode < 0:4 print >>sys.stderr, "Child was terminated by signal", -retcode5 else:6 print >>sys.stderr, "Child returned", retcode7 except OSError as e:8 print >>sys.stderr, "Execution failed:", e

實例演示:

打開記事本:

1 import os2 os.system('notepad')

1 import subprocess2 subprocess.call('notepad')

咱們看如下代碼:

1 import os2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"')

這段代碼會啓動網易雲音樂,效果和咱們在cmd窗口中輸入 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe" 效果同樣。注意字符串中含有空格,因此有 r''。

而如下代碼也能夠實現一樣的功能:

1 import subprocess2 subprocess.call("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe")

同上面那段代碼的區別只是括號中的 r''。

到目前爲止一切正常,咱們再看下面的代碼,嘗試着同時打開兩個程序:

1 import os2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"')3 或4 os.system("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad")5 或6 os.system(""D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"")

以上嘗試都不會成功。

換作subprocess.call函數也不能實現。

這個問題早在07年就有人提交過,請參考http://bugs.python.org/issue1524

os.system和subprocess.call的區別之後補充。

相關文章
相關標籤/搜索