12Python標準庫系列之subprocess模塊
阿新 • • 發佈:2017-05-15
return error false
Python標準庫系列之subprocess模塊
This module allows you to spawn processes, connect to their input/output/error pipes, and obtain their return codes.
常用方法實例
call()
執行命令,並返回狀態碼,狀態碼0
代表命令執行成功,其他的都表示命令執行不成功
>>> ret = subprocess.call(["ls", "-l"], shell=False) total 4 -rw-r--r-- 1 root root 172 May 25 21:21 file.conf >>> ret 0
另一種執行方式
# shell=True表示調用原生的shell命令去執行 >>> ret = subprocess.call("ls -l", shell=True) total 4 -rw-r--r-- 1 root root 172 May 25 21:21 file.conf >>> ret 0
check_call()
執行命令,如果執行狀態碼是0,則返回0,否則拋異常
# 執行一個正確的命令就會返回執行結果和狀態碼 >>> subprocess.check_call(["ls", "-l"]) total 4 -rw-r--r-- 1 root root 172 May 25 21:21 file.conf 0 # 如果執行的是一個錯誤的命令,那麽就會返回錯誤信息 >>> subprocess.check_call(["ls", "a"]) ls: cannot access a: No such file or directory Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.6/subprocess.py", line 505, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command ‘[‘ls‘, ‘a‘]‘ returned non-zero exit status 2
check_output()
執行命令,如果狀態碼是0,則返回執行結果,否則拋異常
# 執行成功就把執行的結果賦值給變量V >>> V = subprocess.check_output("python -V", shell=True) # 執行錯誤的命令就會輸出異常 >>> subprocess.check_output("pasas", shell=True) ‘pasas‘ 不是內部或外部命令,也不是可運行的程序 或批處理文件。 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python35\lib\subprocess.py", line 629, in check_output **kwargs).stdout File "C:\Python35\lib\subprocess.py", line 711, in run output=stdout, stderr=stderr) subprocess.CalledProcessError: Command ‘pasas‘ returned non-zero exit status 1
以上的三種執行方式在執行命令的時候,shell
默認等於True
,等於True
的時候,括號內的命令是一行的,如果shell
等於False
,那麽[]
內的字符串就是命令的一個元素,執行的時候會把[]
內的字符串拼接起來執行。
subprocess.Popen()
call()
、check_call()
、check_output()
默認內部調用的都是subprocess.Popen()
,而subprocess.Popen()
則用於執行更復雜的系統命令。
參數
參數 | 說明 |
---|---|
stdin | 標準輸入 |
stdout | 標準輸出 |
stderr | 錯誤句柄 |
cwd | 用於設置子進程的當前目錄 |
env | 用於指定子進程的環境變量。如果env = None,子進程的環境變量將從父進程中繼承 |
執行普通命令
>>> subprocess.Popen("Python -V", shell=True) <subprocess.Popen object at 0x0000025C97233C88> # Python 3.5.1是輸出出來的結果 >>> Python 3.5.1
執行命令分為兩種:
輸入即可得到輸出,如:ifconfig
輸入進行某交互式環境,依賴再輸入,如:python
>>> import subprocess # 先進入‘/tmp‘目錄,然後在創建subprocess文件夾,shell=True可有可無 >>> subprocess.Popen("mkdir subprocess", shell=True, cwd=‘/tmp‘,) <subprocess.Popen object at 0x7f267cc3d390> >>> import os >>> os.system("ls /tmp") subprocess
subprocess.Popen()實例
# 導入subprocess模塊 import subprocess # 執行python命令,進入python解釋器,stdin標準輸入、stdout標準輸出、stderr錯誤輸出,universal_newlines=True自動輸入換行符 obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # 執行標準輸入,write後面是輸入的命令 obj.stdin.write("print(1)\n") obj.stdin.write("print(2)") # 輸入之後關閉 obj.stdin.close() # 讀取標準輸出的內容,賦值給cmd_out對象 cmd_out = obj.stdout.read() # 關閉標準輸出 obj.stdout.close() # 讀取錯誤輸出的內容,賦值給cmd_error對象 cmd_error = obj.stderr.read() # 關閉錯誤輸出 obj.stderr.close() # 輸出內容 print(cmd_out) print(cmd_error)
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Week5/Day02/sub.py 1 2 Process finished with exit code 0
# 導入subprocess模塊 import subprocess # 執行python命令,進入python解釋器,stdin標準輸入、stdout標準輸出、stderr錯誤輸出,universal_newlines=True自動輸入換行符 obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # 執行兩條命令 obj.stdin.write("print(1)\n") obj.stdin.write("print(2)") # communicate把錯誤輸出或者標準輸出的內容賦值給out_error_list對象,如果有錯誤就賦值錯誤輸出,否則就復制標準輸出 out_error_list = obj.communicate() # 輸出out_error_list對象的內容 print(out_error_list)
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Week5/Day02/sub.py (‘1\n2\n‘, ‘‘) Process finished with exit code 0
# 導入subprocess模塊 import subprocess # 執行python命令,進入python解釋器,stdin標準輸入、stdout標準輸出、stderr錯誤輸出,universal_newlines=True自動輸入換行符 obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # 直接執行print("hello")命令,然後把錯誤或者正確的結果賦值給out_error_list對象 out_error_list = obj.communicate(‘print("hello")‘) # 輸出out_error_list對象的內容 print(out_error_list)
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Week5/Day02/sub.py (‘hello\n‘, ‘‘) Process finished with exit code 0
#Python標準庫 #Subprocess
12Python標準庫系列之subprocess模塊