os.popen(cmd) 与 os.system(cmd) 的区别

1,os.popen(cmd) 不会直接返回任何数据,os.system(cmd) 会直接输出结果(返回的却是int状态码)

2,os.popen(cmd).read() 才会返回str类型的输出结果,os.system(cmd) 返回的是int状态码

3,如果需要对输出结果做操作时,需要使用os.popen(cmd).read()

tmp = os.popen("ipconfig")
res = tmp.read()# 要用read()方法读取后才是文本对象
tmp.close()# 需将对象关闭
print('==========================')
print(res)
print('==========================')
print(type(res))
if '192.168.10.' in str(res):
print('现在是在10网段')
else:
print('不在10 网段,请切换网络') 输出:
==========================
Windows IP 配置
以太网适配器 以太网:
............................此处省略...............................
............................此处省略...............................
==========================
<class 'str'>
现在是在10网段
ip = os.system('ipconfig')
print('-------------------------')
print(type(ip))
print('-------------------------')
print(str(ip)) 输出:
Windows IP 配置
以太网适配器 以太网:
.......................此处省略..........................
.......................此处省略.......................... -------------------------
<class 'int'>
-------------------------
0

python通过cmd命令获取本机ip类型及其值

#encoding:utf-8

import os
import re cmd = "ipconfig"
tmp = os.popen(cmd)
res = tmp.read()# 要用read()方法读取后才是文本对象
tmp.close()# 需将对象关闭
pattern = re.compile('.+:$',re.M)
ip_type = pattern.findall(res)
result=[item.strip() for item in res.split("\n") if item.strip() != "" and (item in ip_type or "IPv4" in item)]
# for index,item in enumerate(result):
# print(index,item)
index=[len(result)]
for item in ip_type:
index.insert(len(index)-1,result.index(item))
# print(index)
res=[]
for i in range(len(index)-1):
if index[i+1]-index[i]<2:
index[i]=-1
else:
res.append(result[index[i]:index[i+1]]) # index=list(filter(lambda item:item>-1,index))
# print(index)
ip_key=[ip[0].split(':')[0].strip() for ip in res]
ip_value=[ip[1].split(':')[1].strip() for ip in res]
ip_dic=dict(zip(ip_key,ip_value)) print(ip_dic)

输出结果:-------------------------------------------------------------

{
'以太网适配器 VMware Network Adapter VMnet1': '***.***.***.***',
'以太网适配器 VMware Network Adapter VMnet8': '***.***.***.***',
'无线局域网适配器 WLAN': '***.***.***.***'
}



 

os.popen(cmd) 与 os.system(cmd) 的区别的更多相关文章

  1. os.system() 和 os.popen()

    1.os.popen(command[, mode[, bufsize]])  os.system(command) 2.os.popen() 功能强于os.system() , os.popen() ...

  2. python笔记16-执行cmd指令(os.system和os.popen)

    os.system 1.如果想在cmd执行python脚本,可以直接用如下指令 python [xx.py绝对路径] 比如我写了个hello.py的脚本,在脚本里面写入内容:print("h ...

  3. os.system('cmd')在linux和windows系统下返回值的差异

    今天,用os.system('cmd')分别在windows和linux平台上执行同一ping命令,命令执行失败时返回码不同,windows为1,而linux下返回为256,如下: linux下: & ...

  4. os.popen('python hello_out.py')中Python程序执行时默认的当前路径为MS-DOS CMD的默认路径

    >>> import os >>> os.getcwd() 'D:\\pythonCode\\pp4e' >>> os.chdir('Stream ...

  5. os.system和os.popen

    使用os.popen调用test.sh的情况: python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执 ...

  6. 调用系统命令 os.system()和os.popen()

    Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...

  7. Python中os.system和os.popen区别

    Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...

  8. Python执行系统命令的方法 os.system(),os.popen(),commands

    os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...

  9. [py] os.system os.popen commands 执行shell

      1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd)   2.popen可以保存命令结果 pwd=os.popen('pwd').r ...

  10. Python os.system 和 os.popen的区别

    (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command ...

随机推荐

  1. redis安装和基础使用

    redis安装 mkdir /server/tools -p cd /server/tools echo 'PATH=/usr/local/redis/src:$PATH' >>/etc/ ...

  2. iOS 如何保持线程一直在运转

    1.简单的可以想到,写一个while循环 while (TRUE) { } 但是这种方式存在缺陷,将导致CPU占用100%,两个核. p.p1 { margin: 0; font: 12px &quo ...

  3. unaipp 发送验证码倒计时

    view代码 <view class="margin-top" @tap="getCheckNum()"> <view class=" ...

  4. pands基础--数据结构:Series

    从本文开始介绍pandas的相关知识. pandas含有是数据分析工作变得更快更简单的高级数据结构和操作工具,是基于numpy构建的. 本章节的代码引入pandas约定为:import pandas ...

  5. JDBC的简单使用以及介绍

    JDBC(Java DataBase Connectivity) Java 语言连接数据库 再本模块中,java提供里一组用于连接数据库的类和接口 Java 语言开发者,本身没有提供如何具体连接数据库 ...

  6. 状态管理(redux)

    https://www.redux.org.cn/ 2013年 Facebook 提出了 Flux 架构的思想,引发了很多的实现.2015年,Redux 出现,将 Flux 与函数式编程结合一起,很短 ...

  7. linux系统下,搭建kafka环境(单机版)

    安装前准备: 安装JDK 安装zookeeper(如果使用kafka内置的zookeeper,可以忽略) 第一步.安装JDK kafka依赖JAVA环境,所以首先要安装jdk 第二步.安装zookee ...

  8. OpenSearch 与 Elasticsearch:哪个开源搜索引擎适合您?

    当谈论到搜索引擎产品时,Elasticsearch 和 OpenSearch 是两个备受关注的选择.它们都以其出色的功能和灵活性而闻名,但在一些方面存在一些差异.在本文中,我们将从功能和延展性.工具与 ...

  9. Vue学习:9.watch监听器

    "watch" 监听器是一种在 Vue.js 中用于监视数据变化并执行相应操作的机制.它是 Vue 实例的一个选项,允许你监视指定的数据,并在数据变化时执行特定的逻辑.通常情况下, ...

  10. Java动态获取实现类 Class.forName(clazz).newInstance()和applicationContext.getBean, bean Map寻找方式,Java Map定义和初始化方法

    Java动态获取实现类 Class.forName(clazz).newInstance()和applicationContext.getBean, bean Map寻找方式,Java Map定义和初 ...