python模組之subprocess類與常量

2021-09-16 13:13:38 字數 4556 閱讀 7524

subprocess.devnull:可傳遞給stdin,stdout,stderr引數的特殊值,意味著將使用特殊檔案os.devnull重定向輸入輸出

subprocess.pipe:可傳遞給stdin,stdout,stderr引數的特殊值,意味著使用管道重定向輸入輸出

subprocess.stdout:可傳遞給stderr引數的特殊值,表示重定向標準錯誤到標準輸出

在乙個新的程序中執行子程式。

構造引數

(args, bufsize=-1, executable=none, stdin=none, stdout=none, stderr=none, preexec_fn=none, close_fds=true, shell=false, cwd=none, env=none, universal_newlines=none, startupinfo=none, creationflags=0, restore_signals=true, start_new_session=false, pass_fds=(), *, encoding=none, errors=none, text=none)

args: 字串或序列。如果是序列,則args中的第乙個元素是要執行的程式;如果是字串,解釋執行與平台有關,在posix系統args將被解釋為要執行的程式的名稱或路徑(前提是不傳遞任何引數給程式)。

在posix系統,shell=true預設使用/bin/sh作為shell。如果args為字串,該字串表示要通過shell執行的命令;如果args為序列,第乙個元素指定要執行的程式,其他元素視為引數。

在windows系統,shell=true預設使用comspec環境變數指定的shell,一般是c:\windows\system32\cmd.exe。唯一需要指定shell=true的場景是要執行的指令是shell內建的,如dir,copy

bufsize: 建立stdin/stdout/stderr管道檔案物件時作為對應的引數傳遞給open()函式。

0:不始用緩衝

1:使用行緩衝

其他正整數:緩衝大小

負整數(預設):使用系統預設值

executable: 使用shell=true的場景很少。shell=true時,在posix系統上此引數表示指定乙個新的shell程式替換預設shell/bin/sh

stdin/stdout/stderr: 分別指定程式執行的標準輸入,標準輸出,標準錯誤。可選值包括pipedevnull,已存在的檔案描述符(正整數),已存在的檔案物件,none。子程序檔案控制代碼繼承自父程序。除此之外,stderr還可以是stdout,表示標準錯誤輸出重定向到標準輸出。

preexec_fn: 限於posix系統,設定乙個可呼叫物件,先於子程序中的程式執行。

close_fds: 如果為false,檔案描述符遵循 inheritance of file descriptors 中描述的inheritable標識。

如果為true,在posix系統下,在子程序執行前關閉除0,1,2外的檔案描述符。

pass_fds: 限於posix,可選的檔案描述符序列,用於在父子程序間保持開放。只要提供了此引數,close_fds強制設為true。

cwd: 在子程序執行前改變工作目錄為cwd,可以是字串或path-like物件。

restore_signals: 限於posix,略

start_new_session: 限於posix,略

env: dict物件,為新程序定義環境變數,替換繼承自父程序的變數。在windows下,要執行side-by-side assembly必須包含可用的環境變數systemroot。 如果指定了env,就必須提供程式執行依賴的所有環境變數

encoding/errors/text/universal_newlines: stdin/stdout/stderr預設以二進位制模式開啟。但如果指定了encoding/errors或者text為true,將使用指定的encoding和errors以文字模式開啟stdin/stdout/stderr。universal_newlines引數等同於text,用於後向相容。

startupinfo: 僅限於windows,略

creationflags: 僅限於windows,略

方法poll(): 檢查子程序是否終止。返回none表示未終止,否則設定returncode屬性並返回。

wait(timeout=none): 如果子程序在timeout後沒有終止,丟擲timeoutexpired異常。否則設定returncode屬性並返回。

communicate(input=none, timeout=none): 程序互動:傳送資料到stdin,讀取stdout或stderr的資料知道讀取到結束符。返回(stdout_data, stderr_data)形式的元組。

input為none或要傳送到子程序的資料,根據stream開啟模式的不同,可以是string或byte型別。

如果要和程序的stdin互動,建立popen物件時需要指定stdin=pipe。類似的,返回的tuple如果希望是非none,需要設定stdout=pipe和/或stderr=pipe。

如果子程序在timeout後沒有終止,丟擲timeoutexpired異常,但子程序並未kill掉,乙個良好的應用應該kill掉子程序並結束互動:

proc = subprocess.popen(...)

try:

outs, errs = proc.communicate(timeout=15)

except timeoutexpired:

proc.kill()

outs, errs = proc.communicate()

send_signal(signal): 傳送訊號到子程序

terminate(): 終止子程序。posix系統上,傳送sigterm訊號到子程序,windows系統上會呼叫terminateprocess()終止程序

kill(): 強制終止子程序。posix系統上,傳送sigkill訊號到子程序。windows系統上kill()是terminate()的別名

屬性args: 傳入popen構造器的第乙個引數,list或string型別

stdin: 如果傳遞給popen的stdin引數是pipe,該屬性表示string或byte型別的可寫stream物件。如果傳遞給popen的stdin引數不是pipe,此屬性值為none

stdout: 與popen.stdin相近,但stream物件是可讀的

stderr: 與popen.stdout相近

pid: 子程序程序號。如果設定了shell=true,pid表示派生shell的程序號

returncode: 子程序返回碼,none表示程序未終止。負數-n表示程序被訊號n終止(僅限posix)。

run()函式執行的返回值,表示程序執行完成。

屬性args: 傳入run()函式的第乙個引數,list或string型別

returncode: 子程序退出碼。如果為負數,表示程序因為某個訊號退出

stdout: 捕獲的子程序的標準輸出,預設為byte型別,如果run()函式呼叫時指定了encoding或errors,或設定了text=true則為string型別。如果未捕獲標準輸出返回none

stderr: 捕獲的子程序的標準錯誤,預設為byte型別,如果run()函式呼叫時指定了encoding或errors,或設定了text=true則為string型別。如果未捕獲標準錯誤返回none

方法check_returncode(): 如果returncode非0,丟擲calledprocesserror異常

subprocess模組的異常基類

子程序執行超時。

屬性cmd: 指令

timeout: 秒為單位的時間

output:run()check_output()函式捕獲到的子程序的輸出,否則為none

stdout: output屬性別名

stderr:run()函式捕獲到的子程序的錯誤輸出,否則為none

check_call()check_output()函式返回非0狀態碼時丟擲。

屬性returncode: 子程序退出碼。如果為負數,表示程序因為某個訊號退出

cmd: 同timeoutexpired

output: 同timeoutexpired

stdout: 同timeoutexpired

stderr: 同timeoutexpired

python模組之shutil模組

高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 import shutil shutil.copyfileobj open old.xml r open new.xml w shutil.copyfile ...

python模組之timeit模組

timeit模組用來測量函式執行時間,通過實際 學習怎樣應用timeit模組 fromtimeitimport print timeit x 7 print timeit x 7 number 1000000 print timeit x 7 number 1000000 print 上面三個列印說...

python模組 之 re模組

功能 實現python對正規表示式對支援與應用,將想要得到對內容按照正規表示式匹配出來 應用場景 爬蟲指令碼 對使用者輸入內容進行合規檢查 如qq格式檢查 等 功能 匹配物件中所有符合正規表示式的內容,並取出來 返回值 列表,所匹配到對項都會返回到列表中 import re content 1362...