如何重定向第三方 python 库使用 os.system 运行的程序的 stderr
我使用外部库,如下所示:
from some_lib import runThatProgram
infile = '/tmp/test'
outfile = '/tmp/testout'
runThatProgram(infile, outfile)
while runThatProgram is:
def runThatProgram(infile, outfile):
os.system("%s %s > %s" % ('thatProgram', infile, outfile))
问题是 'thatProgram'
在 STDERR 上返回很多内容,我想将其重定向到一个文件,但我无法编辑< /strong> runThatProgram
code 因为它位于第三方库中!
I use external library, like this:
from some_lib import runThatProgram
infile = '/tmp/test'
outfile = '/tmp/testout'
runThatProgram(infile, outfile)
while runThatProgram is:
def runThatProgram(infile, outfile):
os.system("%s %s > %s" % ('thatProgram', infile, outfile))
The problem is that 'thatProgram'
returns lots of stuff on STDERR, I want to redirect it to a file, but I cannot edit runThatProgram
code because it is in third party lib!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了说明 Rosh Oxymoron 所说的内容,您可以像这样破解代码:
这样,它将调用
将 stderr (2) 重定向到 stdout (1),并且所有内容都将记录在您的输出文件中。
To illustrate what Rosh Oxymoron said, you can hack the code like this :
with this, it will call
that will redirected stderr (2) to stdout (1), and everything will be logged in your outfile.
要详细说明如何使用 subprocess,您可以打开它,给它一个管道,然后从那里所以
To elaborate on using subprocess, you can open it, give it a pipe and then work from there so