log = open("exeoutput.txt", "w")
sys.stdout = log
cmd="cksum `ls -lrt | grep -v ^d | awk '{print $9}'`"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
print(output)
log.close()
print "File has been executed successfully.\n"
结果:成功创建了输出文件,但它为print语句引发了一个错误
Traceback (most recent call last):
File "demo.py", line 19, in <module>
print "File has been executed successfully.\n"
ValueError: I/O operation on closed file
您用文件句柄替换了
sys.stdout
,然后将其关闭。除非将原始文件对象放回原处,否则将来的打印将失败。您可以保存原始标准输出并在完成后替换它相关问题 更多 >
编程相关推荐