最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python实现获取命令行输出结果的方法
时间:2022-06-24 19:46:10 编辑:袖梨 来源:一聚教程网
本文实例讲述了Python实现获取命令行输出结果的方法。分享给大家供大家参考,具体如下:
Python获取命令行输出结果,并对结果进行过滤找到自己需要的!
这里以获取本机MAC地址和IP地址为例!
| 代码如下 | 复制代码 |
# coding: GB2312 importos, re # execute command, and return the output defexecCmd(cmd): r=os.popen(cmd) text=r.read() r.close() returntext # write "data" to file-filename defwriteFile(filename, data): f=open(filename,"w") f.write(data) f.close() # 获取计算机MAC地址和IP地址 if__name__=='__main__': cmd="ipconfig /all" result=execCmd(cmd) pat1="Physical Address[. ]+: ([w-]+)" pat2="IP Address[. ]+: ([.d]+)" MAC=re.findall(pat1, result)[0] # 找到MAC IP=re.findall(pat2, result)[0] # 找到IP print("MAC=%s, IP=%s"%(MAC, IP)) | |
运行结果:
| 代码如下 | 复制代码 |
E:ProgramPython>del.py MAC=00-1B-77-CD-62-2B, IP=192.168.1.110 E:ProgramPython> | |
相关文章
- 逆水寒无相玄袍如何获取 07-16
- 孤女困魇有哪些游戏特色内容 07-16
- 纸嫁衣8第二章木门如何打开 07-16
- 燕云十六声挐云蛇怎么打-拏云蛇打法教学 07-16
- 怪物猎人荒野1.021版本更新了什么 07-16
- 下一站江湖2风神腿任务如何完成 07-16