码迷,mamicode.com
首页 > 编程语言 > 详细

【Python自动化运维之路Day6】

时间:2016-06-24 20:27:07      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1. configparser模块

  

import configparser
config = configparser.ConfigParser()   #先把config应用一下configparser,个人感觉有点像logging模块中的logger一样
config.read(‘db‘,encoding=‘utf-8‘)  #读取配置文件,编码类型为utf-8
#使用sections()方法读取所有section,以列表形式返回
result = config.sections()
print(result)
#获取指定section下所有键值对
result = config.items(‘section1‘)
print(result)
#获取指定节点下所有的键
result = config.options(‘section1‘)
print(result)
#获取指定节点下键的值
result = config.get(‘section1‘,‘k2‘)
print(result)
####检查、删除、添加特定section######
#检查指定的section是否存在,返回一个布尔值,存在为True
result = config.has_section(‘mysqld‘)
print(result)
#结果: True
#添加节点 mysqldump
config.add_section(‘mysqldump‘)
config.write(open(‘db‘,‘w‘))  #需要使用write方法写入内存数据到配置文件中,不然是不能持久化到文件的
result = config.sections()
print(result)
#删除节点,mysqldump
config.remove_section(‘mysqldump‘)  #使用remove()方法
config.write(open(‘my.cnf‘,‘w‘))  #同样默认是在内存中操作,需要调用write方法,将内存数据写入到文件来持久化存储
result = config.sections()
print(result)

#########检查、删除设置section内的key-value########
#使用has_option方法,返回一个布尔值,存在为True
result = config.has_option(‘mysqldump‘,‘socket‘)
print(result)
#在mysqld中添加 键 innodb_file_per_table 值为1
#使用set方法
config.set(‘mysqldump‘,‘innodb_file_per_table‘,‘1‘)
config.write(open(‘my.cnf‘,‘w‘))  #同样需要写入内存数据到文件,使用write方法
result = config.options(‘mysqld‘)
print(result)
#删除mysqld下的socket键
#使用remove_option()方法
config.remove_option(‘mysqldump‘,‘innodb_file_per_table‘)
config.write(open(‘my.cnf‘,‘w‘))  #写入内存数据到文件
result = config.options(‘mysqld‘)
print(result)

 

2 xml模块

from xml.etree import ElementTree as ET

#打开文件的俩种方式
result_xml = open(‘page.xml‘,‘r‘).read()
#将字符串解析成xml特殊对象,root指的是xml文件的根节点
root = ET.XML(result_xml)
print(root)
#使用xml.etree.ElementTree.parse()函数解析整个xml文件并将其转换成一个文档对象。
tree = ET.parse(‘example.xml‘)
root = tree.getroot()
print(root)

 

 

3 shutil模块

  shutil是一种高级的文件操作工具,可操作的包括:文件、文件夹、压缩文件的处理

 

【Python自动化运维之路Day6】

标签:

原文地址:http://www.cnblogs.com/renyb/p/5615221.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!