python 获取cmd结果_Python获取CMD命令行输出结果

一,os.system()

这种方式虽然可以在控制台看到回显的结果,但是却无法接收到这些内容,更无法对结果进行处理

官方文档对返回结果说明如下在Windows上,返回值是运行命令后系统外壳程序返回的值。…通常是cmd.exe,它返回命令运行的退出状态;

即os.system()返回值是命令执行后退出的状态,正常为0,异常为1

正常情况

In[5]: os.system('dir') Volume in drive F is 数据 Volume Serial Number is 0006-F904 Directory of F:\Practice\PycharmProjects\PythonBasic2019/04/29 10:17

注意末尾的Out [5]:0,这才是真正的返回值

异常情况In[6]: os.system('directory')'directory' is not recognized as an internal or external command,operable program or batch file.Out[6]: 1

Out [6]:1,表示执行出现异常

二,os.popen()

具体用法如下:

result = os.popen('ipconfig')# 返回的结果是一个对象,需要读取后才能处理context = result.read()for line in context.splitlines(): print(line)result.close()

os.popen()的返回值是一个类_wrap_close,需要重定向read()之后才能得到一个str

官方文档对返回值说明:打开到命令cmd或来自命令cmd的管道。返回值是连接到管道的打开文件对象,可以根据模式是“ r”(默认)还是“ w” 来进行读取或写入。

从命令cmd:一个管道,返回值是连接管道的文件对象,通过该对象可以进行读或写。

三,commands.getstatusoutput()

特别说明:commands模块已经被废弃,并且3.x中已经被删除,这里不做过多说明用法如下:output = commands.getstatusoutput('ipconfig') print output

四,subprocess.Popen()

从的python2.4版本开始,可以用子这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回值。子意在替代其他几个老的模块或函数,例如:os.system,os.spawn *,os.popen *,popen2。,命令。subprocess模块可用于执行复杂的系统命令,包括os.popen()不适用的交互模式的场景,例如python

相互场景

import subprocessobj = subprocess.Popen(['python'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)obj.stdin.write('print('hello world')')obj.stdin.write('\n')obj.stdin.write('print('hello python')')obj.stdin.close()cmd_out = obj.stdout.read()obj.stdout.close()cmd_error = obj.stderr.read()obj.stderr.close()print(cmd_out)print(cmd_error) # 程序没有异常,只输出空行

执行结果:hello worldhello python

非共有场景

p = subprocess.Popen('ipconfig', shell=True, stdout=subprocess.PIPE)out, err = p.communicate()for line in out.splitlines(): print(line.decode('gbk', 'ignore'))

注:如果子进程输出到数据到stdout或stderr的管道,并达到了系统pipe的缓存大小的话,子进程会等待父进程转换管道,而父进程此时正wait着着话,将会产生传说中的死锁。建议使用communication()来避免这种情况的发生。

Popen.communicate(input = None)和子进程交互:发送数据到stdin,并从stdout和stderr读数据,直到收到EOF。等待子进程结束。可选的输入如有有话,要为字符串类型。此函数返回一个元组:(stdoutdata,stderrdata),元素类型为,需要进行转码,上面的代码——line.decode(“ gbk”,“ ignore”)

实践案例下面的演示根据系统命令config,获取本机MAC地址和IP地址的代码以网上的代码为基础修改def get_mac_and_ip(): ''' # 获取本机MAC地址和IP地址 :return: (MAC地址,IP地址) ''' # 使用with,不需要显式的写pipe.close() with os.popen('ipconfig -all') as pipe: str_config = pipe.read() # print('完整配置信息:', str_config) # 利用正则表达式和re模块检索结果 mac_re_compile = re.compile(r'物理地址[\. ]+: ([\w-]+)') ip_re_compile = re.compile(r'IPv4 地址[\. ]+: ([\.\d]+)') mac = mac_re_compile.findall(str_config)[0] # 找到MAC ip = ip_re_compile.findall(str_config)[0] # 找到IP # print('MAC=%s, IP=%s' % (mac, ip)) return mac, ipresult = get_mac_and_ip()print('MAC: %s\n IP: %s' % result)

执行结果

MAC: 26-0A-64-A7-68-84 IP: 192.168.1.103

MAC: 26-0A-64-A7-68-84 IP: 192.168.1.103

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值