函數(shù)式編程
2019-08-16 16:44:46
subprocess.Popen和os.system之間的區(qū)別subprocess.Popen()和之間有什么區(qū)別os.system()?
3 回答

幕布斯6054654
TA貢獻1876條經(jīng)驗 獲得超7個贊
如果檢查出Python文檔的子部分,你會發(fā)現(xiàn)有一個如何來代替的例子os.system()
有subprocess.Popen()
:
sts = os.system("mycmd" + " myarg")
......做同樣的事......
sts = Popen("mycmd" + " myarg", shell=True).wait()
“改進的”代碼看起來更復雜,但它更好,因為一旦你知道subprocess.Popen()
,你不需要任何其他東西。subprocess.Popen()
替換了os.system()
分散在其他三個Python模塊中的其他幾個工具(只是其中之一)。
如果有幫助,可以認為subprocess.Popen()
是非常靈活的os.system()
。

HUH函數(shù)
TA貢獻1836條經(jīng)驗 獲得超4個贊
os.system等同于Unix 系統(tǒng)命令,而subprocess是一個輔助模塊,用于提供Popen 命令提供的許多功能,并提供更簡單,可控的接口。這些設(shè)計類似于Unix Popen命令。
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed
在哪里
The popen() function opens a process by creating a pipe, forking, and invoking the shell.
如果您正在考慮使用哪一個,那么請務(wù)必使用子進程,因為您擁有所有執(zhí)行工具,并且可以對該進程進行額外控制。
添加回答
舉報
0/150
提交
取消