python調用shell命令之三慷慨法

來源:互聯網
上載者:User

標籤:字元   docstring   inpu   實現   blog   標準   def   好的   plot   

preface: 忙於近期的任務,須要用到libsvm的一些命令。如在終端執行javasvm_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.popen

subprocess模組的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.call

subprocess模組的call函數。

 

它基本上就像Popen類並都使用同樣的參數,可是它僅僅簡單的等待命令完畢並給你傳回碼。比方:
return_code = subprocess.call("echo Hello World", shell=True)

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

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.