Python基础(14)_python模块之configparser模块、suprocess
9、configparser模块
模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
常见的软件格式文档格式如下:
[DEFAULT]
ServerAliveInterval =
Compression = yes
CompressionLevel =
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port =
ForwardX11 = no
使用configparser模块,创建格式文档:
import configparser config=configparser.ConfigParser() config['DEFAULT'] ={
'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':'yes'
}
config['bitbucket'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('config.ini','w',encoding='utf-8') as f:
config.write(f)
查找文件:以字典的方式
import configparser config = configparser.ConfigParser() #---------------------------查找文件内容,基于字典的形式 print(config.sections()) # [] config.read('config.ini') #读取文件 print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key)
>>user
serveraliveinterval
compression
compressionlevel
forwardx11 print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
>>['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
>>[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
print(config.get('bitbucket.org','compression')) # yes get方法取深层嵌套的值
>>yes
增删改操作
import configparser config = configparser.ConfigParser()
config.read('example.ini') #增加
config.add_section('yuan')
#删除
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")
#修改
config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222') config.write(open('new2.ini', "w"))
10、subprocess模块
当我们需要调用系统的命令的时候,最先考虑的os模块。用os.system()和os.popen()来进行操作。但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出,判断该命令的运行状态,管理多个命令的并行等等。这时subprocess中的Popen命令就能有效的完成我们需要的操作。
简单命令:
a = subprocess.Popen('ls',shell=True)
在windows中,创建子进程需要添加:shell=True
在Linux中,创建子进程不需添加shell=True,在使用命令参数时需要添加shell=True
在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。
import subprocess a = subprocess.Popen('ls',shell=True) # 创建一个新的进程,与主进程不同步
print('>>>',a) #a是Popen的一个实例对象
>>> <subprocess.Popen object at 0x0000024BB71BB128> #in Linux 系统中
subprocess.Popen('ls -l',shell=True) #结果以字符串显示 subprocess.Popen(['ls','-l']) #结果以列表显示
subprocess.PIPE
在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。
import subprocess
s=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
print(s.stdout.read().decode('gbk'))
subprocess创建了子进程,结果本在子进程中,if 想要执行结果转到主进程中,就得需要一个管道,即 : stdout=subprocess.PIPE
Python基础(14)_python模块之configparser模块、suprocess的更多相关文章
- python基础14 ---函数模块4(configparser模块)
configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- python基础系列教程——Python3.x标准模块库目录
python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- 十四. Python基础(14)--递归
十四. Python基础(14)--递归 1 ● 递归(recursion) 概念: recursive functions-functions that call themselves either ...
- Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则
时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...
- python基础——14(shelve/shutil/random/logging模块/标准流)
一.标准流 1.1.标准输入流 res = sys.stdin.read(3) 可以设置读取的字节数 print(res) res = sys.stdin.readline() print(res) ...
- python基础-第六篇-6.2模块
python之强大,就是因为它其提供的模块全面,模块的知识点不仅多,而且零散---一个字!错综复杂 没办法,二八原则抓重点咯!只要抓住那些以后常用开发的方法就可以了,哪些是常用的?往下看--找答案~ ...
随机推荐
- 初识Quartz(三)
本文将介绍CronTrigger的使用方法,CronTrigger相比 SimpleTrigger可以支持更复杂的作业计划.cron这一观念来自UNIX,在UNIX中,cron是一个运行于后台的守护程 ...
- java - day07 - PrimeNum
判断质数 package day06; import java.util.Scanner; //验证质数 public class primeNum { public static void main ...
- Git merge two repositories (ZZ)
转自 https://stackoverflow.com/questions/2428137/how-to-rebase-one-git-repository-onto-another-one If ...
- Unity Tiling和offset的使用
offset是显示框的偏移量 1)拿flappy bird素材来说,起始状态是这种↓. 注意显示框(黄色).它包含的范围是这种↓ 2)当offset_x设置为0.5的时候,显示框(黄色)包含的范围应该 ...
- Git使用技巧(1)-- 配置【持续更新】
配置名字和邮箱 git config --global user.name "Your Name" git config --global user.email "ema ...
- error LNK2019: unresolved external symbol 的一个解决方法
在VS2010中使用opencv时,有时会出现如下类似的连接错误: 解决方法:根据头文件手动指定lib文件 #ifdef _DEBUG #pragma comment(lib,"*.lib& ...
- Eclipse 运行配置(Run Configuration)
Eclipse 运行配置(Run Configuration) 创建和使用 Eclipse 运行配置 在运行配置(Run Configuration)对话框中可以创建多个运行配置.每个配置可以在应用中 ...
- 微信小程序 view 布局
刚看到这个效果的时候还真是和ReactNative的效果一致,属性也基本的一样. view这个组件就是一个视图组件使用起来非常简单. 主要属性: flex-direction: 主要两个特性”row” ...
- ftp uploadFileAction(重要)
TelnetOUtputStream os = ftpClient.put(filename); File file_in = new File(localPath); FileInputStream ...
- 理解和使用WPF 验证机制(值得推荐)
首先建立一个demo用以学习和实验WPF Data Validation机制.创建一个数据实体类: public class Employee { public string Name { get; ...