版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_37887537/article/details/81476280
os.popen方式:
返回值:cmd的输出信息。
import os
f = os.popen(cmd)
print(f.read())
如果正确输出可以看到信息。但是有两个问题:
1.正确的输出信息不是同步反馈的
2.如果调用该命令出错,返回信息为空,并不返回错误信息。
os.system方式
返回值:0(成功),1,2
并不能反馈信息。
重定向
1.这里并不能像如下使用,只能显示Python自身抛出的异常
with open("output.txt","w") as file:
out = sys.stderr
sys.stderr = file
-- * --
sys.stderr = out
2.使用这种window命令的方式,初步解决这个问题
ret = os.system("cmd" + " 2>out.txt")
if ret == 0:
print("success")
else:
with open("output.txt","r") as file:
print(file.read())