简体   繁体   中英

How to capture output from bourne shell command

我想执行一个 bourne shell 命令,捕获它的 stdout 和它的 stderr(单独),以及它的退出代码。

You can use subprocess (part of stdlib):

import subprocess

cmd = input("enter your command: " )
result = subprocess.run(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True)
print("stdout = %s" % (result.stdout))
print("stderr = %s" % (result.stderr))
print("return = %d" % (result.returncode))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM