【Python自动化运维之路Day7】

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)

xml模块

1、打开文件的俩种方式

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)

 2、节点功能

xml格式类型是节点里嵌套节点 ,所以对于每一个节点都有如下的功能:

class Element:
    """An XML element.

    This class is the reference implementation of the Element interface.

    An element's length is its number of subelements.  That means if you
    want to check if an element is truly empty, you should check BOTH
    its length AND its text attribute.

    The element tag, attribute names, and attribute values can be either
    bytes or strings.

    *tag* is the element name.  *attrib* is an optional dictionary containing
    element attributes. *extra* are additional element attributes given as
    keyword arguments.

    Example form:
        <tag attrib>text<child/>...</tag>tail

    """

    当前节点的标签名
    tag = None
    """The element's name."""

    当前节点的属性

    attrib = None
    """Dictionary of the element's attributes."""

    当前节点的内容
    text = None
    """
    Text before first subelement. This is either a string or the value None.
    Note that if there is no text, this attribute may be either
    None or the empty string, depending on the parser.

    """

    tail = None
    """
    Text after this element's end tag, but before the next sibling element's
    start tag.  This is either a string or the value None.  Note that if there
    was no text, this attribute may be either None or an empty string,
    depending on the parser.

    """

    def __init__(self, tag, attrib={}, **extra):
        if not isinstance(attrib, dict):
            raise TypeError("attrib must be dict, not %s" % (
                attrib.__class__.__name__,))
        attrib = attrib.copy()
        attrib.update(extra)
        self.tag = tag
        self.attrib = attrib
        self._children = []

    def __repr__(self):
        return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))

    def makeelement(self, tag, attrib):
        创建一个新节点
        """Create a new element with the same type.

        *tag* is a string containing the element name.
        *attrib* is a dictionary containing the element attributes.

        Do not call this method, use the SubElement factory function instead.

        """
        return self.__class__(tag, attrib)

    def copy(self):
        """Return copy of current element.

        This creates a shallow copy. Subelements will be shared with the
        original tree.

        """
        elem = self.makeelement(self.tag, self.attrib)
        elem.text = self.text
        elem.tail = self.tail
        elem[:] = self
        return elem

    def __len__(self):
        return len(self._children)

    def __bool__(self):
        warnings.warn(
            "The behavior of this method will change in future versions.  "
            "Use specific 'len(elem)' or 'elem is not None' test instead.",
            FutureWarning, stacklevel=2
            )
        return len(self._children) != 0 # emulate old behaviour, for now

    def __getitem__(self, index):
        return self._children[index]

    def __setitem__(self, index, element):
        # if isinstance(index, slice):
        #     for elt in element:
        #         assert iselement(elt)
        # else:
        #     assert iselement(element)
        self._children[index] = element

    def __delitem__(self, index):
        del self._children[index]

    def append(self, subelement):
        为当前节点追加一个子节点
        """Add *subelement* to the end of this element.

        The new element will appear in document order after the last existing
        subelement (or directly after the text, if it's the first subelement),
        but before the end tag for this element.

        """
        self._assert_is_element(subelement)
        self._children.append(subelement)

    def extend(self, elements):
        为当前节点扩展 n 个子节点
        """Append subelements from a sequence.

        *elements* is a sequence with zero or more elements.

        """
        for element in elements:
            self._assert_is_element(element)
        self._children.extend(elements)

    def insert(self, index, subelement):
        在当前节点的子节点中插入某个节点,即:为当前节点创建子节点,然后插入指定位置
        """Insert *subelement* at position *index*."""
        self._assert_is_element(subelement)
        self._children.insert(index, subelement)

    def _assert_is_element(self, e):
        # Need to refer to the actual Python implementation, not the
        # shadowing C implementation.
        if not isinstance(e, _Element_Py):
            raise TypeError('expected an Element, not %s' % type(e).__name__)

    def remove(self, subelement):
        在当前节点在子节点中删除某个节点
        """Remove matching subelement.

        Unlike the find methods, this method compares elements based on
        identity, NOT ON tag value or contents.  To remove subelements by
        other means, the easiest way is to use a list comprehension to
        select what elements to keep, and then use slice assignment to update
        the parent element.

        ValueError is raised if a matching element could not be found.

        """
        # assert iselement(element)
        self._children.remove(subelement)

    def getchildren(self):
        获取所有的子节点(废弃)
        """(Deprecated) Return all subelements.

        Elements are returned in document order.

        """
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'list(elem)' or iteration over elem instead.",
            DeprecationWarning, stacklevel=2
            )
        return self._children

    def find(self, path, namespaces=None):
        获取第一个寻找到的子节点
        """Find first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return the first matching element, or None if no element was found.

        """
        return ElementPath.find(self, path, namespaces)

    def findtext(self, path, default=None, namespaces=None):
        获取第一个寻找到的子节点的内容
        """Find text for first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *default* is the value to return if the element was not found,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return text content of first matching element, or default value if
        none was found.  Note that if an element is found having no text
        content, the empty string is returned.

        """
        return ElementPath.findtext(self, path, default, namespaces)

    def findall(self, path, namespaces=None):
        获取所有的子节点
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Returns list containing all matching elements in document order.

        """
        return ElementPath.findall(self, path, namespaces)

    def iterfind(self, path, namespaces=None):
        获取所有指定的节点,并创建一个迭代器(可以被for循环)
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return an iterable yielding all matching elements in document order.

        """
        return ElementPath.iterfind(self, path, namespaces)

    def clear(self):
        清空节点
        """Reset element.

        This function removes all subelements, clears all attributes, and sets
        the text and tail attributes to None.

        """
        self.attrib.clear()
        self._children = []
        self.text = self.tail = None

    def get(self, key, default=None):
        获取当前节点的属性值
        """Get element attribute.

        Equivalent to attrib.get, but some implementations may handle this a
        bit more efficiently.  *key* is what attribute to look for, and
        *default* is what to return if the attribute was not found.

        Returns a string containing the attribute value, or the default if
        attribute was not found.

        """
        return self.attrib.get(key, default)

    def set(self, key, value):
        为当前节点设置属性值
        """Set element attribute.

        Equivalent to attrib[key] = value, but some implementations may handle
        this a bit more efficiently.  *key* is what attribute to set, and
        *value* is the attribute value to set it to.

        """
        self.attrib[key] = value

    def keys(self):
        获取当前节点的所有属性的 key

        """Get list of attribute names.

        Names are returned in an arbitrary order, just like an ordinary
        Python dict.  Equivalent to attrib.keys()

        """
        return self.attrib.keys()

    def items(self):
        获取当前节点的所有属性值,每个属性都是一个键值对
        """Get element attributes as a sequence.

        The attributes are returned in arbitrary order.  Equivalent to
        attrib.items().

        Return a list of (name, value) tuples.

        """
        return self.attrib.items()

    def iter(self, tag=None):
        在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。
        """Create tree iterator.

        The iterator loops over the element and all subelements in document
        order, returning all elements with a matching tag.

        If the tree structure is modified during iteration, new or removed
        elements may or may not be included.  To get a stable set, use the
        list() function on the iterator, and loop over the resulting list.

        *tag* is what tags to look for (default is to return all elements)

        Return an iterator containing all the matching elements.

        """
        if tag == "*":
            tag = None
        if tag is None or self.tag == tag:
            yield self
        for e in self._children:
            yield from e.iter(tag)

    # compatibility
    def getiterator(self, tag=None):
        # Change for a DeprecationWarning in 1.4
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'elem.iter()' or 'list(elem.iter())' instead.",
            PendingDeprecationWarning, stacklevel=2
        )
        return list(self.iter(tag))

    def itertext(self):
        在当前节点的子孙中根据节点名称寻找所有指定的节点的内容,并返回一个迭代器(可以被for循环)。
        """Create text iterator.

        The iterator loops over the element and all subelements in document
        order, returning all inner text.

        """
        tag = self.tag
        if not isinstance(tag, str) and tag is not None:
            return
        if self.text:
            yield self.text
        for e in self:
            yield from e.itertext()
            if e.tail:
                yield e.tail

节点功能
节点功能

遍历xml文档的所有内容:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Alex Li
from xml.etree import ElementTree as ET
tree = ET.parse('xo.xml')
root = tree.getroot()
print(root.attrib)
for child in root:
    print(child.tag,child.attrib)
    for gradechild in child:
        print(gradechild.tag, gradechild.text,gradechild.attrib)
遍历xml文档的所有内容

遍历xml文档中指定的节点:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Alex Li
# 打开文件,读取XML内容
from xml.etree import ElementTree as ET
str_xml = open('xo.xml', 'r').read()
# 将字符串解析成xml特殊对象,root代指xml文件的根节点
root = ET.XML(str_xml)
# 顶层标签
print(root.tag)

# 循环所有的year节点
for node in root.iter('year'):
    # 将year节点中的内容自增一
    new_year = int(node.text) + 1
    node.text =''
    # 设置属性
    node.set('name', 'alex')
    node.set('age', '18')
    # 删除属性
    #del node.text
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
遍历xml文档中指定的节点

修改节点的内容

和上面configparser一样,所做的修改是在内存中进行,并不会对xml原始文档做修改,如果需要保存修改的配置,需要将内存里的数据写入到xml文件中。

也是分两种方式,解析字符串方式的修改保存, 和直接解析文件方式的修改,保存

 解析字符串方式的修改保存

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang)

from xml.etree import ElementTree as ET

# 解析xml字符串
result_xml = open('example.xml','r').read()
#找到根节点
root = ET.XML(result_xml)

#顶级标签
print('顶级标签: %s'%root.tag)

#循环所有的'link'节点

for i in root:
    for j in i.iter('item'):
        #修改原有属性:
        j.text = str('http://www.jd.com')

        #添加属性:
        j.set('linkA','https://www.dbq168.com')
        j.set('linkB','http://www.sina.com.cn')

        #删除原有属性:
        del j.attrib['linkB']
        print(type(j.attrib))
        print(j.attrib)
#保存内存中的修改到一个新文件,example_new.xml
tree = ET.ElementTree(root)
tree.write('example_new.xlm',encoding='utf-8')

解析字符串方式修改保存
解析字符串方式修改保存
# #直接对解析xml文档
tree = ET.parse('example.xml')
#获取根节点
root = tree.getroot()

#顶级标签
print('顶级标签: %s'%root.tag)

for i in root:
    for j in i.iter('item'):
        #修改原有属性:
        j.text = str('http://www.jd.com')

        #添加属性:
        j.set('linkA','https://www.dbq168.com')
        j.set('linkB','http://www.sina.com.cn')

        #删除原有属性:
        del j.attrib['linkB']
        print(type(j.attrib))
        print(j.attrib)

#保存文件到example_new2.xml
tree.write('example_new2.xml',encoding='utf-8')

解析xml文件方式修改保存
解析XML文件方式修改保存

删除节点内容

for i in root:
    for j in i.findall('language'):
        i.remove(j)
解析字符串方式删除节点

(3) 创建xml文档

方式一

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang)

from xml.etree import ElementTree as ET

#创建根节点
root = ET.Element('company')

#创建节点的部门
ceo = ET.Element('manager',{'ceo':'xiaomage'})

#创建coo
coo = ET.Element('manager',{'coo':'xiaoyang'})

#创建cto
cto = ET.Element('manager',{'cto':'laowang'})

#给cto创建部门
dev = ET.Element('tech',{'dev':'xiaoli'})
ops = ET.Element('tech',{'ops':'xiaoqiang'})

cto.append(dev)
cto.append(ops)

#将manager添加到root中
root.append(ceo)
root.append(coo)
root.append(cto)

tree = ET.ElementTree(root)
tree.write('company.xml',encoding='utf-8',short_empty_elements=False)

#默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company>

方式一
方式一
from xml.etree import ElementTree as ET

#创建根节点
root = ET.Element('company')

#创建高管
ceo = root.makeelement('manager',{'ceo':'xiaomage'})
coo = root.makeelement('manager',{'coo':'xiaoyang'})
cto = root.makeelement('manager',{'cto':'laowang'})

#创建技术部门
dev = cto.makeelement('tech',{'dev':'xiaoli'})
ops = cto.makeelement('tech',{'ops':'xiaoqiang'})

cto.append(dev)
cto.append(ops)

#添加部门到根节点
root.append(ceo)
root.append(coo)
root.append(cto)

tree = ET.ElementTree(root)
tree.write('company_v2.xml',encoding='utf-8',short_empty_elements=False)

#默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company>

方式二创建
方式二
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang)

from xml.etree import ElementTree as ET

#创建根节点
root = ET.Element('company')

#创建高管
ceo = ET.SubElement(root,'manager',attrib={'ceo':'xiaomage'})
coo = ET.SubElement(root,'manager',attrib={'coo':'xiaoyang'})
cto = ET.SubElement(root,'manager',attrib={'cto':'laowang'})
#创建技术部门
dev = ET.SubElement(cto,'tech',{'dev':'xiaoli'})
ops = ET.SubElement(cto,'tech',{'ops':'xiaoqiang'})

cto.append(dev)
cto.append(ops)


tree = ET.ElementTree(root)  #生成文档对象
tree.write('company_v3.xml',encoding='utf-8',short_empty_elements=False)






# #创建根节点
# root = ET.Element('company')
#
# #创建高管
# ceo = root.makeelement('manager',{'ceo':'xiaomage'})
# coo = root.makeelement('manager',{'coo':'xiaoyang'})
# cto = root.makeelement('manager',{'cto':'laowang'})
#
# #创建技术部门
# dev = cto.makeelement('tech',{'dev':'xiaoli'})
# ops = cto.makeelement('tech',{'ops':'xiaoqiang'})
#
# cto.append(dev)
# cto.append(ops)
#
# #添加部门到根节点
# root.append(ceo)
# root.append(coo)
# root.append(cto)
#
# tree = ET.ElementTree(root)
# tree.write('company_v2.xml',encoding='utf-8',short_empty_elements=False)



# #创建根节点
# root = ET.Element('company')
#
# #创建节点的部门
# ceo = ET.Element('manager',{'ceo':'xiaomage'})
#
# #创建coo
# coo = ET.Element('manager',{'coo':'xiaoyang'})
#
# #创建cto
# cto = ET.Element('manager',{'cto':'laowang'})
#
# #给cto创建部门
# dev = ET.Element('tech',{'dev':'xiaoli'})
# ops = ET.Element('tech',{'ops':'xiaoqiang'})
#
# cto.append(dev)
# cto.append(ops)
#
# #将manager添加到root中
# root.append(ceo)
# root.append(coo)
# root.append(cto)
#
# tree = ET.ElementTree(root)
# tree.write('company.xml',encoding='utf-8',short_empty_elements=False)

#默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company>

方式三创建
方式三

默认保存的xml没有缩进,特别的难看,可以按照下面方式添加缩进:

from xml.etree import ElementTree as ET
from xml.dom import minidom

def prettify(string):
    '''
    将节点转换成字符串,并添加缩进
    :param string:
    :return:
    '''
    rough_string = ET.tostring(string,'utf-8')
    reparesd = minidom.parseString(rough_string)
    return reparesd.toprettyxml(indent='	')

#创建根节点
root = ET.Element('company')

#创建节点的部门
ceo = ET.Element('manager',{'ceo':'xiaomage'})

#创建coo
coo = ET.Element('manager',{'coo':'xiaoyang'})

#创建cto
cto = ET.Element('manager',{'cto':'laowang'})

#给cto创建部门
dev = ET.Element('tech',{'dev':'xiaoli'})
ops = ET.Element('tech',{'ops':'xiaoqiang'})

cto.append(dev)
cto.append(ops)

#将manager添加到root中
root.append(ceo)
root.append(coo)
root.append(cto)

string = prettify(root)

f = open('company.xml','w',encoding='utf-8')
f.write(string)
f.close()

#执行结果:
<?xml version="1.0" ?>
<company>
    <manager ceo="xiaomage"/>
    <manager coo="xiaoyang"/>
    <manager cto="laowang">
        <tech dev="xiaoli"/>
        <tech ops="xiaoqiang"/>
    </manager>
</company>

缩进
三种创建方式都一样

shutil模块

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

 4  压缩打包模块

ZipFile模块是处理zip压缩包的模块,用于压缩和解压,添加和列出压缩包的内容。ZipFile是主要的类,其过程是讲每个文件以及相关的信息作为ZipInfo对象操作到ZipFile的成员,包括文件头信息和文件内容。ZipFile模块详细介绍,点我

tarfile模块可以用于压缩/解压tar类型文件,并且支持进一步压缩tar.gz和tar.bz2文件,主要是调用open函数打开文件,并返回TarFile实例。Tarfile模块详细介绍,点我

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