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

經過os.system和subprocess.call()函數調用其餘程序

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

cmd中打開程序html

a.打開系統自帶程序python

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

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

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

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

    注意,雙引號是必須的this

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

在cmd中關閉程序code

    在cmd中關閉程序可使用taskkill命令,語法以下:orm

    taskkill /f /t /im 進程名

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

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

    以下圖所示:

cmd運行和關閉程序演示

a.os.system方法

os.system(command)  連接 https://docs.python.org/2/library/os.html#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 to sys.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 C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variableCOMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (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 subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocessdocumentation for some helpful recipes.

Availability: Unix, Windows.

os模塊中的system()函數能夠方便地運行其餘程序或者腳本。其函數原型爲:
os.system(command)
command 爲要執行的命令,近似於Windows下cmd窗口中輸入的命令。

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

b.用subprocess.call()代替os.system()

17.1.4.3. Replacing os.system()    

連接 https://docs.python.org/2/library/subprocess.html#replacing-os-system

1 status = os.system("mycmd" + " myarg")
2 # becomes
3 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", -retcode
5     else:
6         print >>sys.stderr, "Child returned", retcode
7 except OSError as e:
8     print >>sys.stderr, "Execution failed:", e

 實例演示:

打開記事本:

1 import os
2 os.system('notepad')

1 import subprocess
2 subprocess.call('notepad')

咱們看如下代碼:

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

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

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

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

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

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

1 import os
2 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()的區別之後補充。

相關文章
相關標籤/搜索