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. 将mnist训练的caffemodel生成动态链接库DLL

    在项目程序中经常看到动态链接库,非常好奇,想自己实现一下,于是乎尝试一波.就因为这种好奇,每天都被bug所困扰... 1. 训练caffemodel 在windows环境下搭建caffe无果,转投Ub ...

  2. vue-devtools

    今天分享个vue的开发者工具,vue2和vue3都可以使用 先来看个效果: 如何安装? 打开官网vue-devtools 选择你需要版本 安装完记得重启下浏览器,不然是没效果的

  3. vue 实现商品列表的添加、删除,搜索

    大江东去,浪淘尽,千古风流人物.故垒西边,人道是,三国周郎赤壁.乱石穿空,惊涛拍岸,卷起千堆雪.江山如画,一时多少豪杰.遥想公瑾当年,小乔初嫁了,雄姿英发.羽扇纶巾,谈笑间,樯橹灰飞烟灭.故国神游,多 ...

  4. linux系统下,安装maven教程

    1.下载 官网:https://maven.apache.org/download.cgi 2.上传包 将下载好的maven安装包apache-maven-3.8.6-bin.tar.gz放在磁盘的 ...

  5. json null (空值)不序列化

    json null (空值)不序列化 JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.DefaultV ...

  6. mysql 常用脚本语法

    mysql 常用脚本语法 创建表: CREATE TABLE test_db1.test_table1 ( id INT auto_increment NOT NULL, my_name varcha ...

  7. 微博Lite版安装

    微博Lite版安装 前置条件,需要使用chrome,或chromium内核的浏览器. 1.浏览器输入:https://m.weibo.cn/ 2.在地址栏最右侧点击安装. --

  8. C#.NET FRAMEWORK XML私钥转PKCS1,PKCS8

    C#.NET FRAMEWORK XML私钥转PKCS1,PKCS8 使用了 BouncyCastle 这个dll ,到nuget中下载即可. XML私钥转PKCS1 public string Xm ...

  9. 基于阿里Anolis OS8.8 的Hadoop大数据平台建设

    基于阿里Anolis OS8.8 的Hadoop大数据平台建设 VNC安装与使用 0 Anolis OS基本操作 0.1 Anolis OS用户与组管理 0.2 系统进程管理 0.3 文件操作命令及权 ...

  10. 记一次 React context 使用

    学习 React 之 Context 使用 记录一次React context 使用 React.createContext Api 新建文件 contexts.js 文件用来存放 context 对 ...