Python調用外部程式__Python

來源:互聯網
上載者:User
通過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 後斷行符號即可關閉網易雲音樂。

    如下圖所示:

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.