我有一个名为的字符串variable
,需要做subprocess
相当于os.system
. 我试图找到一种方法来做到这一点,但只发现:
variable2 = subprocess.Popen(args, stdout=subprocess.PIPE)
print variable2.communicate()[0]
但是,我无法理解如何使用它。我如何实现我的目标?
我有一个名为的字符串variable
,需要做subprocess
相当于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=True
如果您正在执行一个 shell 命令(如将给 os.system 的命令)而不是显式指定可执行文件和一系列参数,则可能需要传递。