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. zoxide更新后 (cd)异常

    关于zoxide github地址:https://github.com/ajeetdsouza/zoxide 简单来说 zoxide是一个cd的强化版.它会记录你曾经cd过的目录,在你使用cd的时候 ...

  2. 使用vscode编辑c语言

    在 Visual Studio Code (VSCode) 中配置 C 语言环境 步骤指南: 一,前期准备(安装扩展,软件包) 安装 C/C++ 扩展 打开 VSCode. 点击左侧边栏的扩展按钮(或 ...

  3. Windows下cmd命令行sftp上传至Linux服务器

    1.Windows+R进入运行 2.输入cmd,进入命令行 3.命令建立连接 sftp 用户名@ip地址 例如: 输入密码,即可建立连接 上传方式: 1)直接拖动文件到命令行窗口,可以直接显示该文件的 ...

  4. Go版RuoYi

    RuoYi-Go  https://github.com/Kun-GitHub/RuoYi-Go 1. 关于我 个人介绍 2. 介绍 后端用Go写的RuoYi权限管理系统 (功能正在持续实现)后端 G ...

  5. Sass报错: Using / for division is deprecated

         原因是:当sass版本>= 1.33.0时,会不支持 / 这种语法:  解决方式一:      降低sass版本,将sass版本换成:"sass": "^ ...

  6. javascript 生成器和迭代器

    前置知识 生成器函数会返回一种称为Generator的迭代器 迭代器是一个对象,定义一个序列,并在终止时返回一个返回值 Symbol.iterator为每一个对象定义了默认的迭代器,可以被for..o ...

  7. @ConfigurationProperties(prefix = “xx.xx.xx“) 从配置文件中取值赋给类的属性

    @ConfigurationProperties(prefix = "xx.xx.xx") 从配置文件中取值赋给类的属性 @ConfigurationProperties(pref ...

  8. 《最新出炉》系列入门篇-Python+Playwright自动化测试-51- 字符串操作 - 上篇

    1.简介 在日常的自动化测试工作中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥 ...

  9. 使用Python爬取公众号的合集内容

    使用Python爬取公众号的合集 前言 ...最近老是更新关于博客的文章,很久没更新其他的了,然后写一下如何爬取微信公众号里面的图片吧! 先看看微信公众号的样子吧: 我爬取的是公众号的合集内容 讲解 ...

  10. windows下rust环境的安装(现在是2023年5月份)

    在自己家电脑上安装一下rust,还是遇到一些问题,这里记录一下,免得后面再踩坑. 官方网站 获取主要信息还得靠官网,比如安装软件:) 地址是 https://www.rust-lang.org/zh- ...