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. Linux Shell命令提示样式修改

    对linux shell命令样式进行美化. 修改前的效果: 修改后的效果: 直接给出.bashrc脚本代码: 1 # ~/.bashrc: executed by bash(1) for non-lo ...

  2. 分布式定理--CAP定理

    cap定理指的是,在一个分布式系统中,只能满足cap中的两项. C consistency 一致性 A availability 可用性 P partition tolerance 分区可容错性 -- ...

  3. 地址栏hash模式以?问号分割也可以分割的

    可以看到href里面hash没有? 但是还是以?分割了 就很不明白 但是我就indexof判断有没有? 再进行下一步逻辑 这里记录一下坑

  4. react 网络请求 axios

    react中通过npm来安装axios扩展 cnpm i -S axios 发起请求 import React, { Component } from 'react' import axios fro ...

  5. web服务器 传统开发和前后端分离开发 服务器相关概念

    web服务器 Web服务器一般指的是网站服务器,是指驻留因特网上某一台或N台计算机的程序,可以处理浏览器等Web客户端的请求并返回相应响应,目前最主流的三个Web服务器是Apache. Nginx . ...

  6. 支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象

    支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象 SPI 三方服务接入指南https://opendocs.alipay.com/isv/spiforisv 服务端实现 ...

  7. Thread交互及interrupt示例

    package com.test.docxml; /** Thread交互及interrupt示例 * 线程模拟:一个在睡觉,一个在敲墙,敲墙完成之后,把睡觉的吵醒了. */ public class ...

  8. es应用服务初始化步骤

    第一步:创建索引+settings+mappingtestes_v1 PUT { "settings": { "index.mapping.total_fields.li ...

  9. Springboot项目密码加密器jasypt

    最新版依赖 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>j ...

  10. Azure Storage Blob 启用sftp协议支持

    背景 我这边需要给前端同学一个上传静态文件的地方,比如js.css.图片.icons等等,前端上传后直接在项目中:我这边用的是Azure Storage blob:为了单独分配权限,我这边打算启用SF ...