[英]Python 3: Using subprocess Instead Of os.system
我有一個名為variable
的字符串,需要執行os.system
的subprocess
os.system
等效os.system
。 我試圖找到一種方法來做到這一點,但只發現了:
variable2 = subprocess.Popen(args, stdout=subprocess.PIPE)
print variable2.communicate()[0]
但是,我在理解如何使用它時遇到了麻煩。 我如何實現目標?
該文檔提供了幾種舊式子流程創建功能的等效項。 os.system()
在這里解釋。
In [4]: os.system('uname -a')
Linux diego-workstation 3.0.0-16-generic #28-Ubuntu SMP Fri Jan 27 17:44:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Out[4]: 0
In [8]: subprocess.call(['uname', '-a'])
Linux diego-workstation 3.0.0-16-generic #28-Ubuntu SMP Fri Jan 27 17:44:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Out[8]: 0
查看subprocess.call
, subprocess.check_call
和subprocess.check_output
函數。 如果要執行shell命令(如os.system所給),則可能需要傳遞shell=True
,而不是顯式指定可執行文件和參數序列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.