python調用shell命令之三大方法

來源:互聯網
上載者:User

標籤:subprocess   commands   popen   shell   

preface: 忙於最近的任務,需要用到libsvm的一些命令,如在終端運行java svm_train train_file model_file. pythonsubset.py file train_num train_file test_file等命令,但file的準備又是通過python寫好的,file需要是libsvm能夠接受的格式,故用python寫好特徵,轉為libsvm能夠接受的格式,產生file,然後在終端調用訓練。故想著直接在python代碼裡面直接運行終端的命令。博友部落格描述得很詳細,這裡直接轉載過來並做些注釋了。

#=====================================================

一、os 模組1.1.os模組的exec方法簇:

ipython互動介面中:

In [1]: import osIn [2]: os.execos.execl    os.execlp   os.execv    os.execvp   os.execle   os.execlpe  os.execve   os.execvpe  In [2]: os.execl?Type:        functionString form: <function execl at 0xb73673e4>File:        /home/shifeng/anaconda/lib/python2.7/os.pyDefinition:  os.execl(file, *args)Docstring:execl(file, *args)Execute the executable file with argument list args, replacing thecurrent process. 

os.exec+Tab鍵智能提示可以看到有8個,help(os.execl)等可以找到其用法說明。

1.2.os模組的system方法

system方法會建立子進程運行外部程式,方法只返回外部程式的運行結果。0表示運行成功。

In [10]: os.system("echo \"hello world\"")hello worldOut[10]: 0In [11]: os.system("ls")all_roc_plot.py~  task1_feature_all2.py  test.py   test.sh   ui_without_buy~scrapy_work  test test.py~  test.sh~Out[11]: 0In [12]: os.system("cat test.sh")echo "hello world"Out[12]: 0In [13]: os.system("sh test.sh")hello worldOut[13]: 0
如上,一些基本的shell命令,傳入os.system()參數裡面,便能執行。不過無法得到命令的返回值。

1.3.os模組 popen方法

popen方法能夠得到shell命令的返回值,os.popen(cmd)後,需要再調用read()或者readlines()這兩個命令。輸出結果。

In [14]: os.popen("ls")Out[14]: <open file 'ls', mode 'r' at 0xb67efd30>In [15]: os.popen("ls").read()Out[15]: 'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~\n'In [16]: os.popen("ls").readlines()Out[16]: ['all_roc_plot.py~\n', 'scrapy_work\n', 'task1_feature_all2.py\n', 'test\n', 'test.py\n', 'test.py~\n', 'test.sh\n', 'test.sh~\n', 'ui_without_buy~\n']In [17]: s = os.popen("ls").read()In [18]: for i in s.split("\n"):   ....:     print i   ....:     all_roc_plot.py~scrapy_worktask1_feature_all2.pytesttest.pytest.py~test.shtest.sh~ui_without_buy~

注意,read()或者readlines()後,其每個元素包含一個斷行符號符\n。

二、 commands模組

使用commands模組的getoutput方法,這種方法同popend的區別在於popen返回的是一個檔案控制代碼,而本方法將外部程式的輸出結果當作字串返回,很多情況下用起來要更方便些。
主要方法:  

*   commands.getstatusoutput(cmd)         返回(status, output)
*   commands.getoutput(cmd)                   只返回輸出結果
*   commands.getstatus(file)                     返回ls -ld file的執行結果字串,調用了getoutput,不建議使用此方法

In [8]: import commands In [9]: commands.getoutput("ls")Out[9]: 'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~'In [10]: commands.getstatusoutput("ls")Out[10]: (0, 'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~')
三、 subprocess模組

使用subprocess模組可以建立新的進程,可以與建立進程的輸入/輸出/錯誤管道連通,並可以獲得建立進程執行的返回狀態。使用subprocess模組的目的是替代os.system()、os.popen*()、commands.*等舊的函數或模組。

3.1.subprocess.call(["some_command","some_argument","another_argument_or_path"])

subprocess.call(command,shell=True)

3.2.subprocess.Popen(command,shell=True)如果command不是一個可執行檔,shell=True不可省。
使用subprocess模組可以建立新的進程,可以與建立進程的輸入/輸出/錯誤管道連通,並可以獲得建立進程執行的返回狀態。使用subprocess模組的目的是替代os.system()、os.popen*()、commands.*等舊的函數或模組。最簡單的方法是使用classsubprocess.Popen(command,shell=True)。Popen類Popen.stdin,Popen.stdout,Popen.stderr三個有用的屬性,可以實現與子進程的通訊。
In [11]: from subprocess import  callIn [12]: call(["ls","-l"])總用量 40-rw-rw-r-- 1 shifeng shifeng  3266  5月  3 14:14 all_roc_plot.py~drwxrwxr-x 6 shifeng shifeng  4096  6月 18 16:59 scrapy_work-rw-rw-r-- 1 shifeng shifeng 12786  6月 10 10:20 task1_feature_all2.pydrwxrwxr-x 2 shifeng shifeng  4096  6月  8 11:36 test-rw-rw-r-- 1 shifeng shifeng  3649  6月 19 10:21 test.py-rw-rw-r-- 1 shifeng shifeng   255  6月 11 21:21 test.py~-rw-rw-r-- 1 shifeng shifeng    19  6月 25 19:49 test.sh-rw-rw-r-- 1 shifeng shifeng     0  6月 25 19:49 test.sh~-rw-rw-r-- 1 shifeng shifeng     0  4月  8 22:15 ui_without_buy~Out[12]: 0In [13]: from subprocess import  PopenIn [14]: Popen(["ls","-l"])Out[14]: <subprocess.Popen at 0xb67c91ac>In [15]: 總用量 40-rw-rw-r-- 1 shifeng shifeng  3266  5月  3 14:14 all_roc_plot.py~drwxrwxr-x 6 shifeng shifeng  4096  6月 18 16:59 scrapy_work-rw-rw-r-- 1 shifeng shifeng 12786  6月 10 10:20 task1_feature_all2.pydrwxrwxr-x 2 shifeng shifeng  4096  6月  8 11:36 test-rw-rw-r-- 1 shifeng shifeng  3649  6月 19 10:21 test.py-rw-rw-r-- 1 shifeng shifeng   255  6月 11 21:21 test.py~-rw-rw-r-- 1 shifeng shifeng    19  6月 25 19:49 test.sh-rw-rw-r-- 1 shifeng shifeng     0  6月 25 19:49 test.sh~-rw-rw-r-- 1 shifeng shifeng     0  4月  8 22:15 ui_without_buy~

1、subprocess.call(command, shell=True)#會直接列印出結果。

2、subprocess.Popen(command, shell=True) 也可以是subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) 這樣就可以輸出結果了。如果command不是一個可執行檔,shell=True是不可省略的。shell=True意思是shell下執行command。

#========================以下轉載============

4. 眾方法的比較以及總結4.1. 關於 os.system os.system("some_command with args")將命令以及參數傳遞給你的系統shell,這很好,因為你可以用這種方法同時運行多個命令並且可以設定管道以及輸入輸出重新導向。比如:
os.system("some_command < input_file | another_command > output_file")
然而,雖然這很方便,但是你需要手動處理shell字元的轉義,比如空格等。此外,這也只能讓你運行簡單的shell命令而且不能運行外部程式。
4.2. 關於os.popen使用stream = os.popen("some_command with args")也能做與os.system一樣的事,與os.system不同的是os.popen會給你一個像檔案的對象從而你可以使用它來訪問哪個程式的標準輸入、輸出。而且popen還有三個變種都是在I/O處理上有輕微不同。假如你通過一個字串傳遞所有東西,你的命令會傳遞給shell;如果你通過一個列表傳遞他們,你不用擔心逃避任何事。
4.3. 關於subprocess.popensubprocess模組的Popen類,意圖作為os.popen的替代,但是因為其很全面所以比os.popen要顯得稍微複雜,使用起來需要學習哦~~。
比如你可以使用  print Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read()  來替代  print os.popen("echo Hello World").read()。但是相比之下它使用一個統一的類包括4中不同的popen函數還是不錯的。
4.4. 關於subprocess.callsubprocess模組的call函數。它基本上就像Popen類並都使用相同的參數,但是它只簡單的等待命令完成並給你傳回碼。比如:
return_code = subprocess.call("echo Hello World", shell=True)

#==========================================

參考:

1.博友部落格:http://blog.csdn.net/longerzone/article/details/17889969

2.博友部落格:http://zhou123.blog.51cto.com/4355617/1312791

python調用shell命令之三大方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.