【问题标题】:python redirecting stderr to file does not workpython将stderr重定向到文件不起作用
【发布时间】:2018-02-11 00:52:43
【问题描述】:

我有一个 python 脚本,它运行具有不同输入的程序。我想将 stderr 输出重定向到文件并构建一个 html 文件。

我的问题是,我的脚本运行了,但文件中没有写入标准错误,就像它在某个地方消失了一样。我试过os.system()subprocess.call

#!/usr/bin/python 
import os
import subprocess
indir = 'good'
name = 'good'
count = 0
badcount = 0
file_num = 0
with open('output.html', 'w') as ofh:
    test = open('output.html')
    ofh.write("""<!DOCTYPE html>
            <html>
                <head>
                    <meta charset="utf-8" />
                </head>
                <body>\n""")
    for i in range(0, 2):
        for root, dirs, filenames in os.walk(indir):
            for f in filenames:
                if f.endswith('.tig'):
                    prog = './../src/tc ' + indir + '/' + f
                    prog1 = './../src/tc'
                    prog2 = indir + '/' + f
                    ofh.write('%s <br>\n' % (f))
                    x = os.system('%s &>output.html' % (prog))
                    x = subprocess.call([prog1, prog2], stderr=test)
                    if x == 0:
                        count += 1
                    else:
                        ofh.write("<br>\n")
                        badcount += 1
                        file_num += 1
        ofh.write("<br>\n")
        ofh.write('%s /%s good tests found in %s folder<br>\n'
                % (count, file_num, name))
        ofh.write('%s /%s bad tests found in %s folder<br>\n'
                % (badcount, file_num, name))
        count = 0
        badcount = 0
        file_num = 0
        if i == 0:
            indir = 'syntax'
            name = 'syntax'
        elif i == 1:
            indir = 'bind'
            name = 'bind'
        elif i == 2:
            indir = 'type'
            name = 'type'
    ofh.write("""   </body>
    </html>""")

在这里,我用 os.system 和 subprocess.call 进行了 2 次尝试,有人知道会发生什么吗?还是我做错了什么?

【问题讨论】:

  • 为什么会有这条线? test = open('output.html')
  • 我将它与 call 一起使用,因为不知何故它不想与 ofh 一起工作,我发现我实际上需要写 open('output.html', 'w')
  • 是的,那条线不应该在那里。如果您在移除线路后仍然遇到问题,抱歉我无法确定问题所在。

标签: python subprocess os.system


【解决方案1】:

您可能希望使用 subprocess 中的 Popen 并将 stdout 和 stderr 重定向到 PIPE

p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

这是一个模拟代码,我在其中运行的命令会产生捕获到html 文件中的错误。

from subprocess import Popen, PIPE

with open('output.html', 'w') as ofh:
    ofh.write("""<body>\n""")

    cmd = 'python file.png' #this will produce error
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

    ofh.write("""{}""".format(stderr))
    ofh.write("""</body>""")

这应该会导致以下错误:

C:\python file.png
  File "file.png", line 1
SyntaxError: Non-ASCII character '\x89' in file file.png on line 1, but no encod
ing declared; see http://python.org/dev/peps/pep-0263/ for details

在html文件中捕获

【讨论】:

  • 感谢它适应它并使它工作!我的印刷有点古怪,但它仍然有效!
  • 很高兴它有帮助,stdout 和 stderr 只是文本。不过,您可以使用 HTML 标签进行迭代并使其漂亮。
  • 只是一个问题,我在执行你的方法时得到一个 : b'' 格式,你知道它可能来自哪里吗?
猜你喜欢
  • 2021-06-29
  • 2017-03-09
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多