要从subprocess.run()
函数的结果中获得与在终端中看到的相同的打印输出,可以通过以下步骤实现:
subprocess.run()
函数执行命令,并将capture_output
参数设置为True
,以捕获命令的标准输出和错误输出:import subprocess
result = subprocess.run(['command'], capture_output=True)
result.stdout
属性获取标准输出的内容,并使用result.stderr
属性获取错误输出的内容。可以使用decode()
方法将字节转换为字符串:output = result.stdout.decode('utf-8')
error = result.stderr.decode('utf-8')
print()
函数:print(output)
print(error)
这样就可以从subprocess.run()
函数的结果中获得与在终端中看到的相同的打印输出。
关于subprocess.run()
函数的更多信息,可以参考腾讯云的相关文档:subprocess.run()函数 - Python标准库
领取专属 10元无门槛券
手把手带您无忧上云