subprocess模块
subprocess模块
os.system(cmd) 输出命令结果到屏幕;返回执行命令状态(0为成功,非0为不成功);
os.popen(cmd)返回执行命令的内存地址;
os.popen(cmd).read() 读取出执行命令的结果;
如果即想要返回状态,又想得到返回结果,可以使用subprocess模块
python2,可以使用commands.getstatusoutput("dir")
subprocess.run("dir",shell=True)
res = subprocess.Popen("dir",shell=True,stdout = subprocess.PIPE)
res.stdout.read()
获取的内容未打印到屏幕上,暂时保存在管道中,通过stdout.read()输出.