我剛開始使用python子流程模塊。shell = True或False在python子流程中的區別
代碼subprocess.call("ls", shell = False) and subprocess.call("ls", shell = True)
都返回相同的結果。我只想知道兩個shell選項之間的主要區別。
我剛開始使用python子流程模塊。shell = True或False在python子流程中的區別
代碼subprocess.call("ls", shell = False) and subprocess.call("ls", shell = True)
都返回相同的結果。我只想知道兩個shell選項之間的主要區別。
如果殼爲True,則指定命令將通過殼來執行。如果您主要將Python用於其在大多數系統shell中提供的增強控制流,並且仍然想要方便地訪問其他shell功能(如shell管道,文件名通配符,環境變量擴展以及將〜擴展到用戶的home目錄。但是,請注意,Python本身提供了很多類似shell的功能(特別是glob,fnmatch,os.walk(),os.path.expandvars(),os.path.expanduser()和shutil)的實現。
這可以打開你的代碼,外殼注塑技術,它可以更好地解釋在這裏:
在Windows機器爲例,如果殼被設置爲假,見下圖:
import subprocess
subprocess.Popen("dir", shell = False)
運行時,此代碼應返回WindowsError: [Error 2]
,指出無法找到指定的文件。但是,如果shell爲True,則應返回一個對象。這是因爲'dir'
正在通過cmd'傳送',因此,諸如dir等內置命令將起作用。
與subprocess.call一樣。