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


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

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

cmd中打開程序

a.打開系統自帶程序

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

以notepad為例,直接在cmd窗口中輸入notepad后回車即可打開。

b.打開自己安裝且沒有加入環境變量的程序

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

    注意,雙引號是必須的

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

在cmd中關閉程序

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

    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()的區別以后補充。

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM