【发布时间】: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