1. 程式人生 > >python調用shell命令之三慷慨法

python調用shell命令之三慷慨法

ech ces nds 不用 準備 每一個 popen函數 通信 hello

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 os

In [2]: os.exec
os.execl    os.execlp   os.execv    os.execvp   
os.execle   os.execlpe  os.execve   os.execvpe  

In [2]: os.execl?
Type:        function
String form: <function execl at 0xb73673e4>
File:        /home/shifeng/anaconda/lib/python2.7/os.py
Definition:  os.execl(file, *args)
Docstring:
execl(file, *args)

Execute the executable file with argument list args, replacing the
current process. 

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

1.2.os模塊的system方法

system方法會創建子進程執行外部程序。方法僅僅返回外部程序的執行結果。0表示執行成功。

In [10]: os.system("echo \"hello world\"")
hello world
Out[10]: 0

In [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]: 0

In [12]: os.system("cat test.sh")
echo "hello world"
Out[12]: 0

In [13]: os.system("sh test.sh")
hello world
Out[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_work
task1_feature_all2.py
test
test.py
test.py~
test.sh
test.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  call

In [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.py
drwxrwxr-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]: 0

In [13]: from subprocess import  Popen

In [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.py
drwxrwxr-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命令之三慷慨法