completed_process = subprocess.run(
['your_command', 'parameter'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# this starts the process, waits for it to finish, and gives you...
completed_process.returncode
completed_process.stdout
completed_process.stderr
使用Python运行stuff的规范方法是使用},这些函数往往有一些神秘的警告,比如“不要将stdout=PIPE或stderr=PIPE与此函数一起使用”,因此让我再提供一些:
subprocess
模块,但是它有很多函数神秘地称为check_call
或{步骤1:运行脚本
现在该进程正在后台运行,并且您有了对它的引用。在
编辑:我差点忘了——如果您想稍后检索输出,您必须告诉Python为标准输出创建读取管道。如果不执行此步骤,stdout和stderr将只转到程序的标准输出和标准错误,
communicate
将不会在步骤2中获取它们。在步骤2:等待进程完成并获得其输出
^{pr2}$^{} 还允许您执行更多操作:
input=
参数)timeout=
参数)确保还捕获并正确处理来自^{} 或{a1}的任何异常。在
如果您不关心旧的Python,有一种更简单的方法,称为^{} 来完成这一切:
对于错误检查,您可以调用
completed_process.check_returncode()
,或者只将check=True
作为run
的附加参数传递。在另一种可能更简单的方法是:
相关问题 更多 >
编程相关推荐