python 调用系统命令,并将输出重定向,输出其结果至文件或者字符串变量

import subprocess
file_output = open("debug.txt","w")
subprocess.Popen("ipconfig",stdout= file_output).wait() ######输出重定向,不卡住界面也不会因输出而在生成exe时有问题,如果不需要等待子进程结束,去掉.wait即可
file_output.close()

cmd_sys = subprocess.Popen('ipconfig',stdout=subprocess.PIPE)######将命令执行结果获取到 赋值给cmd_res
cmd_res =  cmd_sys.stdout.read()

猜你喜欢

转载自blog.csdn.net/caobin0825/article/details/51331492