python--configparser模块,subprocess模块

时间:2022-12-15 20:29:33

  configparser模块

configparser文件格式

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no
写一个configparser文件
import configparser

config=configparser.ConfigParser()#制作一个ConfigParser object
#"DEFAULT"是固定用词,该key下为共有信息
config["DEFAULT"]={
'ServerAliveInterval': '45',
}
#可以任意取名config['bitbucket.org'] = {'User':'hg'}config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}with open('ss.ini','w') as f:    config.write(f)#固定格式


import configparser

config=configparser.ConfigParser()
config.read('a.cfg')

#查看所有的标题
res=config.sections() #['section1', 'section2']
print(res)

#查看标题section1下所有key=value的key
options=config.options('section1')
print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

#查看标题section1下所有key=value的(key,value)格式
item_list=config.items('section1')
print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

#查看标题section1下user的值=>字符串格式
val=config.get('section1','user')
print(val) #egon

#查看标题section1下age的值=>整数格式
val1=config.getint('section1','age')
print(val1) #18

#查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('section1','is_admin')
print(val2) #True

#查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('section1','salary')
print(val3) #31.0

http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label9

==================================================================================================

  subprocess模块

import subprocess


# print(subprocess.Popen('dir',shell=True))#在python 中使用window命令#开启新的进程

# s=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)#子进程将执行结果放到管道中
# print(s.stdout.read().decode('gbk'))#父进程从管道中取出数据并打印