python基础之内置模块(二)

时间:2021-01-07 22:27:00

configparser

configparser用来对特定格式的文件进行解析处理,比如ha-proxy,rsync,samba配置文件等等均可.来个简单的文件先看下:

[section1]  #节点
k1 = 2345.5 #键值对
k2:v2           #键值对
k3=123

[section2]
k1 = v1

格式类似于上面的,基本格式是一个节点对应下面的一批键值对.

查看配置文件中存在的节点信息:

import configparser


config=configparser.ConfigParser()
print(config)
config.read("111",encoding='utf8')

sects=config.sections()         #查看所有节点
print(sects)

res=config.options('section1')      #查看节点下的数据(键值对)
print(res)

item_list=config.items('section1')  #查看所有键值对组成的列表
print(item_list)

val=config.get('section1','k2')     #获取节点下某键对应的值
print(val)

val2=config.getint('section1','k3')
print(val2)

out:


<configparser.ConfigParser object at 0x101278a90>
['section1', 'section2']
['k1', 'k2', 'k3']
[('k1', '2345.5'), ('k2', 'v2'), ('k3', '123')]
v2
123

对config文件中的节点进行增删改查操作:

import configparser

config=configparser.ConfigParser()  #其实就是类实例化
config.read('111',encoding='utf-8')     #导入指定文件

print(config.has_section('section1'))   #检查实例化后的对象中是否有指定的节点名称
print(config.has_section('sectionx'))


config.add_section('binmom')    #添加节点,并将内存中的数据写入文件111
config.write(open('111','w'))


config.remove_section('section1')   #删除节点,写入文件
config.write(open('111','w'))

输出结果:

True
False

文件111中的数据结果如下:

[section2]
k1 = v1

[binmom]

对指定节点下的键值对(option)进行增删改查:


import configparser

config=configparser.ConfigParser()  #其实就是类实例化
config.read('111',encoding='utf-8')     #导入指定文件


print(config.has_option('section1','k1'))   #查询节点section1中是否有k1的key
print(config.has_option('section1','k5'))


config.remove_option('section1','k1')   #删除节点section1中的k1的键值对
config.write(open('111','w'))

config.set('section1','k5','ace')   #设置节点section1中的k5的键,对应value为ace
config.write(open('111','w'))

对应输出:

True
False

文件111的内容结果:

[section1]
k2 = v2
k3 = 123
k5 = ace

[section2]
k1 = v1

总结一下吧:

操作对象 对应含义
section 节点
option 节点下的key

写到这里我们发现,其实大多数的模块,其实就是面向对象中的使用class方法先将对象实例化后再进行操作!

XML文件及其操作

xml文件也是实现不同语言或者程序之间进行数据交换的协议,它的特点是尖括号开头,尖括号结尾.使用范围就更为广泛了,tomcat resin kvm 等等,使用了大量的xml文件来进行相关配置.先来看一个典型的xml文件吧:

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2023</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2026</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2026</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

xml文件一般可分为两种使用类型:

  1. 页面展示(字符串类型)
  2. 配置文件(已写入文件中)

xml文件中一般有一些分类,根节点、子节点、属性(attrib)、内容(text)、标签(tag)
。特点是可以节点嵌套节点,子节点可以积蓄嵌套,下面是一些从老师博客copy来的操作方法 ,也可以python dir出来:

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的解析

之前说过,xml有两种方式,一种字符串类型,一种文件类型,所以有两种解析方式:

第一种,利用ElementTree.XML将字符串解析为xml:

from xml.etree import ElementTree as ET


##########打开文件,读取XML内容
str_xml = open('xo.xml', 'r').read()

###############将字符串解析成xml特殊对象,root代指xml文件的根节点
root = ET.XML(str_xml)

利用ElementTree.XML将字符串解析成xml对象

第二种,利用ElementTree.parse将文件解析为xml:

from xml.etree import ElementTree as ET

###直接解析xml文件
tree = ET.parse("xo.xml")

###获取xml文件的根节点
root = tree.getroot()

利用ElementTree.parse将文件直接解析成xml对象

遍历xml内容

from xml.etree import ElementTree as et

tree=et.parse('test1.xml')
root=tree.getroot()
print(root.tag)     #顶层的标签

for child in root:  #遍历xml的第二层的标签,属性和内容
    print(child.tag,child.attrib,child.text)
    for sub_child in child:     #遍历第三层内容
        print(sub_child.tag,sub_child.attrib,sub_child.text)

out:

data
country {'name': 'Liechtenstein'} 
        
rank {'updated': 'yes'} 2
year {} 2023
gdppc {} 141100
neighbor {'direction': 'E', 'name': 'Austria'} None
neighbor {'direction': 'W', 'name': 'Switzerland'} None
country {'name': 'Singapore'} 
        
rank {'updated': 'yes'} 5
year {} 2026
gdppc {} 59900
neighbor {'direction': 'N', 'name': 'Malaysia'} None
country {'name': 'Panama'} 
        
rank {'updated': 'yes'} 69
year {} 2026
gdppc {} 13600
neighbor {'direction': 'W', 'name': 'Costa Rica'} None
neighbor {'direction': 'E', 'name': 'Colombia'} None

遍历指定的节点

print(root.tag)


#########遍历XML中所有的year节点
for node in root.iter('year'):
    # 节点的标签名称和内容
    print(node.tag, node.text)

修改xml文件中的内容

from xml.etree import ElementTree as et

str_xml=open('test1.xml','r').read()
root=et.XML(str_xml)
print(root.tag)

for node in root.iter('year'):
    year=int(node.text)+1
    node.text=str(year)

    node.set('name','alex')
    node.set('age','19')

    del node.attrib['name']

# print(root.tag,root.text)
tree=et.ElementTree(root)
tree.write('new_test2.xml',encoding='utf8',xml_declaration=True,short_empty_elements=False)

new_test2.xml中的内容:

<?xml version='1.0' encoding='utf8'?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year age="19">2024</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria"></neighbor>
        <neighbor direction="W" name="Switzerland"></neighbor>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year age="19">2027</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia"></neighbor>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year age="19">2027</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica"></neighbor>
        <neighbor direction="E" name="Colombia"></neighbor>
    </country>
</data>

删除节点

  1. 字符串方式解析打开,删除/保存:
from xml.etree import ElementTree as ET

############ 解析字符串方式打开 ############

# 打开文件,读取XML内容
str_xml = open('xo.xml', 'r').read()

# 将字符串解析成xml特殊对象,root代指xml文件的根节点
root = ET.XML(str_xml)

############ 操作 ############

# 顶层标签
print(root.tag)

# 遍历data下的所有country节点
for country in root.findall('country'):
    # 获取每一个country节点下rank节点的内容
    rank = int(country.find('rank').text)

    if rank > 50:
        # 删除指定country节点
        root.remove(country)

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
  1. 文件方式解析打开,删除/保存:
from xml.etree import ElementTree as ET

############ 解析文件方式 ############

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 获取xml文件的根节点
root = tree.getroot()

############ 操作 ############

# 顶层标签
print(root.tag)

# 遍历data下的所有country节点
for country in root.findall('country'):
    # 获取每一个country节点下rank节点的内容
    rank = int(country.find('rank').text)

    if rank > 50:
        # 删除指定country节点
        root.remove(country)

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

解析文件方式打开,删除,保存

创建xml文件

方式1:

from xml.etree import ElementTree as ET


# 创建根节点
root = ET.Element("famliy")


# 创建节点大儿子
son1 = ET.Element('son', {'name': '儿1'})
# 创建小儿子
son2 = ET.Element('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson2 = ET.Element('grandson', {'name': '儿12'})
son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)

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

方式2:

from xml.etree import ElementTree as ET

# 创建根节点
root = ET.Element("famliy")


# 创建大儿子
# son1 = ET.Element('son', {'name': '儿1'})
son1 = root.makeelement('son', {'name': '儿1'})
# 创建小儿子
# son2 = ET.Element('son', {"name": '儿2'})
son2 = root.makeelement('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
# grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson1 = son1.makeelement('grandson', {'name': '儿11'})
# grandson2 = ET.Element('grandson', {'name': '儿12'})
grandson2 = son1.makeelement('grandson', {'name': '儿12'})

son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)

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

方式3:

from xml.etree import ElementTree as ET


# 创建根节点
root = ET.Element("famliy")


# 创建节点大儿子
son1 = ET.SubElement(root, "son", attrib={'name': '儿1'})
# 创建小儿子
son2 = ET.SubElement(root, "son", attrib={"name": "儿2"})

# 在大儿子中创建一个孙子
grandson1 = ET.SubElement(son1, "age", attrib={'name': '儿11'})
grandson1.text = '孙子'


et = ET.ElementTree(root)  #生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True, short_empty_elements=False)

xml创建时候的缩进

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


def prettify(elem):
    """将节点转换成字符串,并添加缩进。
 """
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")

# 创建根节点
root = ET.Element("famliy")


# 创建大儿子
# son1 = ET.Element('son', {'name': '儿1'})
son1 = root.makeelement('son', {'name': '儿1'})
# 创建小儿子
# son2 = ET.Element('son', {"name": '儿2'})
son2 = root.makeelement('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
# grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson1 = son1.makeelement('grandson', {'name': '儿11'})
# grandson2 = ET.Element('grandson', {'name': '儿12'})
grandson2 = son1.makeelement('grandson', {'name': '儿12'})

son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)


raw_str = prettify(root)

f = open("xxxoo.xml",'w',encoding='utf-8')
f.write(raw_str)
f.close()

xml文件开头空间的命名

直接有个链接讲的比较清楚:
点我点我点我

from xml.etree import ElementTree as ET

ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF", attrib={"{http://www.company.com}hhh": "123"})
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,
           encoding='utf-8',
           method="xml")

shutil

shutil是python中高级不low B的文件操作方式.主要功能为目录和文件操作,以及文件压缩操作等.注意即便是更高级别的文件复制函数(shutil.copy(),shutil.copy2())也不能复制所有文件的元数据。这意味着在POSIX平台上,文件的所有者和组以及访问控制列表都将丢失。在Mac OS中资源fork和其他元数据无法使用。这意味着资源将丢失,文件类型和创建者代码将不正确。在Windows上,文件所有者,ACL和备用数据流不会被复制。

shutil.copyfileobj(fsrc, fdst[, length])

拷贝文件内容

shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

shutil.copyfile(src, dst)

拷贝文件

shutil.copyfile('f1.log', 'f2.log')

shutil.copymode(src, dst)

仅拷贝权限,内容 用户 用户组均不变

shutil.copymode('f1.log', 'f2.log')

shutil.copystat(src, dst)

仅拷贝状态信息,包括mode bits atime mtime flags

shutil.copystat('f1.log', 'f2.log')

shutil.copy(src, dst)

拷贝文件和权限

shutil.copy('f1.log', 'f2.log')

shutil.copy2(src, dst)

拷贝文件和状态信息

shutil.copy2('f1.log', 'f2.log')

shutil.ignore_patterns(*patterns)
忽略指定文件

shutil.copytree(src, dst, symlinks=False, ignore=None)

拷贝递归文件夹

一般是两个一起用,生产中总有一些要排除拷贝的东西

import shutil

shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

其中symlinks是链接也拷贝

shutil.rmtree(path[, ignore_errors[, onerror]])

递归删除文件

shutil.rmtree('folder1')

shutil.move(src, dst)

递归移动文件,也可重命名文件

shutil.move('folder1', 'folder3')

shutil.make_archive(base_name, format,...)

创建并压缩文件,并返回文件路径

参数:

  • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径
  • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
  • root_dir: 要压缩的文件夹路径,默认为当前目录
  • owner: 用户,默认当前用户
  • group: 组,默认当前组
  • logger: 用于记录日志,通常是logging.Logger对象

来个例子吧,可能跟清楚些:

#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
  
  
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

其实shutil中的压缩,是使用ZipFile 和 TarFile这两个模块来执行的.

import zipfile,tarfile

#zipfile操作

zip=zipfile.ZipFile('test.zip','w')
zip.write('111')
zip.write('new_test1.xml')
zip.close()

zip=zipfile.ZipFile('test.zip','r')
for i in zip.namelist():
    print(i)
zip.extract('111')

#tarfile操作

tar=tarfile.open('ttt.tar','w')
tar.add('111',arcname='11111')
tar.add('test.py',arcname='test111111.py')

tar=tarfile.open('ttt.tar','r')
for i in tar.getmembers():
    print(i)

obj=tar.getmember('11111')
# tar.getmember('1111')
tar.extract(obj)

subprocess系统命令

python3中可执行shell命令的有

  • os.system()
  • os.spawn()

包括以前python版本中的os.popen() popen2() commands.* ,目前这些版本均已废除.

现在在用处理的是subporcess模块,subprocess来统一处理,并提供了更为丰富的功能.

call

执行命令,返回状态码,需要注意的是,shell的参数和命令中的配合

命令形式 shell参数值
命令和参数按照列表元组组成列表 False
命令和参数按照字符串形式 True
import subprocess

ret=subprocess.call(['ls','-l'],shell=False)
print('ret:%s'%ret)

res=subprocess.call('ls -l',shell=True)
print('res:%s'%res)

out:

ret:0
res:0

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则报错,与call返回的结果一致

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则报错

import subprocess

ret=subprocess.check_output('echo hello,workd',shell=True)
print(ret)

out:

b'hello,workd\n'

注意返回结果为字符串类型

subprocess.Popen(...)

上面的call check_call check_output 都是调用popen的,popen本身可以执行较为复杂的系统命令.

强大之处在于系统与shell交互之间有管道:subprocess.PIPE,可分别赋值于stdin stderr stdout

参数:

  • args,shell命令,可以是字符串或者序列类型(如:list,元组)
  • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
  • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
  • cwd:用于设置子进程的当前目录
  • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
  • universal_newlines:不同系统的换行符不同,True -> 同意使用 \n

来个例子:

import subprocess

obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)

进入/home/dev 创建了一个t3的目录

在linux系统中如果遇到需要交互的系统命令怎么办呢?看下面:

import subprocess

obj = subprocess.Popen(["python"],
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       universal_newlines=True)


obj.stdin.write("print(1)\n") #1
obj.stdin.write("print(2)") #1
obj.stdin.close()   #1

cmd_out = obj.stdout.read() #1
obj.stdout.close()
cmd_error = obj.stderr.read() #1
obj.stderr.close()              #1

print(cmd_out)              #1
print(cmd_error)                #1

上面的例子中,#1标识的部分,可以由communicate()来代替,这么看来比较简单,其实就是把stdout stderr全部交由communicate去处理,看例子:


import subprocess

obj = subprocess.Popen(["python"],
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       universal_newlines=True)


obj.stdin.write("print(1)\n")
obj.stdin.write("print2")


out_error_list = obj.communicate()
print(out_error_list)
print(type(out_error_list))

out:

('1\n', 'Traceback (most recent call last):\n File "<stdin>", line 2, in <module>\nNameError: name \'print2\' is not defined\n')
<class 'tuple'>

注意它返回的是一个元组类型的数据

并且,我们可以使用communicate来执行一些较为简单的系统命令:

import subprocess

obj = subprocess.Popen(["python"],
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       universal_newlines=True)

out_error_list = obj.communicate('print("hello,world")')
print(out_error_list)
print(type(out_error_list))

out:

('hello,world\n', '')
<class 'tuple'>