Python下調用Linux的命令

有時候難免需要直接調用Shell命令來完成一些比較簡單的操作,比如mount一個文件系統之類的。那麼我們使用Python如何調用Linux的Shell命令?下面來介紹幾種常用的方法:

1. os 模塊


1.1. os模塊的exec方法族

Python的exec系統方法同Unix的exec系統調用是一致的。這些方法適用於在子進程中調用外部程序的情況,因爲外部程序會替換當前進程的代碼,不會返回。( 這個看了點 help(os)  --> search "exec" 的相關介紹,但是沒太搞明白咋使用)


1.2. os模塊的system方法

system方法會創建子進程運行外部程序,方法只返回外部程序的運行結果。這個方法比較適用於外部程序沒有輸出結果的情況。

[python] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. >>> import os  
  2. >>> os.system("echo \"Hello World\"")   # 直接使用os.system調用一個echo命令  
  3. Hello World         ——————> 打印命令結果  
  4. 0                   ——————> What's this ? 返回值?  
  5. >>> val = os.system("ls -al | grep \"log\" ")   # 使用val接收返回值  
  6. -rw-r--r--  1 root       root       6030829 Dec 31 15:14 log    ——————> 此時只打印了命令結果  
  7. >>> print val             
  8. 0                   ——————> 注意,此時命令正常運行時,返回值是0  
  9. >>> val = os.system("ls -al | grep \"log1\" ")  
  10. >>> print val         
  11. 256                 ——————> 使用os.system調用一個沒有返回結果的命令,返回值爲256~  
  12. >>>   

注意:上面說了,此方法脂肪會外部程序的結果,也就是os.system的結果,所以如果你想接收命令的返回值,接着向下看~


1.3. os模塊的popen方法

當需要得到外部程序的輸出結果時,本方法非常有用,返回一個類文件對象,調用該對象的read()或readlines()方法可以讀取輸出內容。比如使用urllib調用Web API時,需要對得到的數據進行處理。os.popen(cmd) 要得到命令的輸出內容,只需再調用下read()或readlines()等 如a=os.popen(cmd).read()

[python] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. >>> os.popen('ls -lt')                  # 調用os.popen(cmd)並不能得到我們想要的結果  
  2. <open file 'ls -lt ', mode 'r' at 0xb7585ee8>  
  3. >>> print os.popen('ls -lt').read()     # 調用read()方法可以得到命令的結果  
  4. total 6064  
  5. -rwxr-xr-x 1 long       long            23 Jan  5 21:00 hello.sh  
  6. -rw-r--r-- 1 long       long           147 Jan  5 20:26 Makefile  
  7. drwxr-xr-x 3 long       long          4096 Jan  2 19:37 test  
  8. -rw-r--r-- 1 root       root       6030829 Dec 31 15:14 log  
  9. drwxr-xr-x 2 long       long          4096 Dec 28 09:36 pip_build_long  
  10. drwx------ 2 Debian-gdm Debian-gdm    4096 Dec 23 19:08 pulse-gylJ5EL24GU9  
  11. drwx------ 2 long       long          4096 Jan  1  1970 orbit-long  
  12. >>> val = os.popen('ls -lt').read()     # 使用變量可以接收命令返回值  
  13. >>> if "log" in val:                    # 我們可以使用in來判斷返回值中有木有一個字符串  
  14. ...     print "Haha,there is the log"  
  15. ... else:  
  16. ...     print "No,not happy"  
  17. ...  
  18. Haha,there is the log  


2. commands 模塊


使用commands模塊的getoutput方法,這種方法同popend的區別在於popen返回的是一個類文件對象,而本方法將外部程序的輸出結果當作字符串返回,很多情況下用起來要更方便些。
主要方法:  

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

[python] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. long@zhouyl:/tmp/tests$ python  
  2. Python 2.7.3 (default, Jan  2 201316:53:07)   
  3. [GCC 4.7.2] on linux2  
  4. Type "help""copyright""credits" or "license" for more information.  
  5. >>> import commands  
  6. >>> commands.getstatusoutput('ls -lt')      # 返回(status, output)  
  7. (0'total 5900\n-rwxr-xr-x 1 long long      23 Jan  5 21:34 hello.sh\n-rw-r--r-- 1 long long     147 Jan  5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan  5 21:34 log')  
  8. >>> commands.getoutput('ls -lt')            # 返回命令的輸出結果(貌似和Shell命令的輸出格式不同哈~)  
  9. 'total 5900\n-rwxr-xr-x 1 long long      23 Jan  5 21:34 hello.sh\n-rw-r--r-- 1 long long     147 Jan  5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan  5 21:34 log'  
  10. >>> commands.getstatus('log')               # 調用commands.getoutput中的命令對'log'文件進行相同的操作  
  11. '-rw-r--r-- 1 long long 6030829 Jan  5 21:34 log'  
  12. >>>   


3. subprocess模塊


根據Python官方文檔說明,subprocess模塊用於取代上面這些模塊。有一個用Python實現的並行ssh工具—mssh,代碼很簡短,不過很有意思,它在線程中調用subprocess啓動子進程來幹活。
[python] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. >>> from subprocess import call  
  2. >>> call(["ls""-l"])  

subprocess與system相比的優勢是它更靈活(你可以得到標準輸出,標準錯誤,“真正”的狀態代碼,更好的錯誤處理,等..)。我認爲使用os.system已過時,或即將過時。

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會返回一個類文件對象,使用它來訪問標準輸入、輸出。

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)


os模塊中還有C中那樣的fork/exec/spawn函數,但是我不建議直接使用它們。
subprocess可能更加適合你。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章