Python获取CMD命令行输出结果

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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 <DIR> .2019/04/29 10:17 <DIR> ..2019/04/29 11:45 <DIR> .idea2019/03/31 21:36 <DIR> venv2019/03/31 21:35 <DIR> _01_HelloWorld2019/03/31 21:36 <DIR> _02_数据类型...(略) 0 File(s) 0 bytes 15 Dir(s) 41,743,155,200 bytes freeOut[5]: 0 注意末尾的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')# 返回的结果是一个<class 'os._wrap_close'>对象,需要读取后才能处理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 = municate()for line in out.splitlines(): print(line.decode('gbk', 'ignore'))
注:如果子进程输出到数据到stdout或stderr的管道,并达到了
系统pipe的缓存大小的话,子进程会等待父进程转换管道,而父进程
此时正wait着着话,将会产生传说中的死锁。

建议使用communication()来避免这种情况的发生。

municate(input = None)和子进程交互:发送数
据到stdin,并从stdout和stderr读数据,直到收到EOF。

等待子进
程结束。

可选的输入如有有话,要为字符串类型。

此函数返回一个元组:(stdoutdata,stderrdata),元素类型为<class'bytes'>,需
要进行转码,上面的代码——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 = pile(r'物理地址[\. ]+: ([\w-]+)') ip_re_compile = pile(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。

相关文档
最新文档