with open('WarningErr.txt', 'w') as err:
subprocess.call(cmd, stderr=err)
with open('WarningOut.txt', 'w') as out:
return_code = subprocess.call(cmd, stdout=out)
现在,我在" .txt和stdout"中写下错误和输出。
但它太慢了。我写在一个,然后在另一个。 我必须能同时写两个。
稍微复制Bash中的T恤行为。
答案 0 :(得分:0)
我在@Rightleg建议的命令调用中包含了tee。我改变了命令。但现在它完美无缺。
我没想到我可以通过子进程调用tee。但它的确很有效!
# for example
command = 'ls'
# To write at the same time in the shell and a file both errors, and stdout (TEE behaviour)
cmd = ['bash', '-c', '. myBash.bash; {} > >(tee {}/WarningOut.txt) 2> >(tee {}/WarningErr.txt >&2)'
.format(command, PATHTOWHEREYOUWANTIT_g, PATHTOWHEREYOUWANTIT_g)]
return_code = subprocess.call(cmd)
答案 1 :(得分:0)
猜你想登录到stdout / stderr和一个文件。尝试使用日志库和此代码:
import logging
# create logger obj
_glogger = logging.getLogger("myApp")
# create file sink
hdlr = logging.FileHandler(os.path.join("dumpDir",'myApp.log'))
formatter = logging.Formatter('%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
_glogger.addHandler(hdlr)
# create steam sink = stdout
hdlr2 = logging.StreamHandler()
hdlr2.setFormatter(formatter)
_glogger.addHandler(hdlr2)
_glogger.setLevel(logging.DEBUG)
_glogger.debug("a debug message")