python获取控制台输出_Python获取CMD命令行输出结果

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-17 04:09   11   0

学Python,用RPA

艺赛旗RPA2020.1版本 正在免费下载使用中,欢迎下载使用艺赛旗-RPA机器人免费下载|提供流程自动化解决方案www.i-search.com.cnv2-afb8881366f34f63920087775aa60d30_ipico.jpg

一,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\PythonBasic

2019/04/29 10:17

2019/04/29 10:17

2019/04/29 11:45

2019/03/31 21:36

2019/03/31 21:35

2019/03/31 21:36

...(略)

0 File(s) 0 bytes

15 Dir(s) 41,743,155,200 bytes free

Out[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')

# 返回的结果是一个对象,需要读取后才能处理

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 subprocess

obj = 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 world

hello 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, ip

result = 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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP