zoukankan      html  css  js  c++  java
  • PYthon-configparser/ subprocess模块

    configparser

    import configparser
    
    config = configparser.ConfigParser()
    config.read('test.ini')
    
    import configparser
    
    config = configparser.ConfigParser()
    config.read('test.ini')
    
    # 1.获取sections
    print(config.sections())    # ['section1', 'section2']
    
    # 2.获取某一sections下的所有的option
    print(config.options('section1'))   # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
    
    # 3.获取items
    print(config.items('section1'))     # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
    
    # 4.获取某个section单独的元素值
    res = config.get('section1', 'user')
    print(res, type(res))   # egon <class 'str'>
    
    res1 = config.getint('section1', 'age')
    print(res1, type(res1)) # 18 <class 'int'>
    
    res2 = config.getboolean('section1', 'is_admin')
    print(res2, type(res2)) # True <class 'bool'>
    
    res3 = config.getfloat('section1', 'salary')
    print(res3, type(res3)) # 31.0 <class 'float'>

    一:subprocess

    一、subprocess以及常用的封装函数
    运行python的时候,我们都是在创建并运行一个进程。像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序。
    subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具,从而在进程间使用文本通信。

    subprocess.call()
    父进程等待子进程完成
    返回退出信息(returncode,相当于Linux exit code)

    subprocess.check_call()
    父进程等待子进程完成
    返回0
    检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性,可用try…except…来检查

    subprocess.check_output()
    父进程等待子进程完成
    返回子进程向标准输出的输出结果
    检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性和output属性,output属性为标准输出的输出结果,可用try…except…来检查。

    这三个函数的使用方法相类似,下面来以subprocess.call()举例说明:

    在Windows平台和Linux平台不同

    Windows平台

    import subprocess
    
    obj = subprocess.Popen(r'E:Python学习相关我的博客文件Python正课内容',
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           )
    print(obj)  # <subprocess.Popen object at 0x02FB2FE8>
    res = obj.stdout.read()
    print(res)  # b''
    
    err_res = obj.stderr.read() #'E:Python学习相关我的博客文件Python正课内容' 不是内部或外部命令,也不是可运行的程序或批处理文件。
    print(err_res.decode('gbk'))  # b"'E:\Pythonxd1xa7xcfxb0xcfxe0xb9xd8\xcexd2xb5xc4xb2xa9xbfxcdxcexc4xbcxfe\Pythonxd5xfdxbfxcexc4xdaxc8xdd' xb2xbbxcaxc7xc4xdaxb2xbfxbbxf2xcdxe2xb2xbfxc3xfcxc1xeexa3xacxd2xb2xb2xbbxcaxc7xbfxc9xd4xcbxd0xd0xb5xc4xb3xccxd0xf2
    xbbxf2xc5xfaxb4xa6xc0xedxcexc4xbcxfexa1xa3
    "
    print(err_res)  # b"'E:\Pythonxd1xa7xcfxb0xcfxe0xb9xd8\xcexd2xb5xc4xb2xa9xbfxcdxcexc4xbcxfe\Pythonxd5xfdxbfxcexc4xdaxc8xdd' xb2xbbxcaxc7xc4xdaxb2xbfxbbxf2xcdxe2xb2xbfxc3xfcxc1xeexa3xacxd2xb2xb2xbbxcaxc7xbfxc9xd4xcbxd0xd0xb5xc4xb3xccxd0xf2
    xbbxf2xc5xfaxb4xa6xc0xedxcexc4xbcxfexa1xa3
    "
    

    Linux平台

    import  subprocess
    
    '''
    sh-3.2# ls /Users/egon/Desktop |grep txt$
    mysql.txt
    tt.txt
    事物.txt
    '''
    
    res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
    res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
                     stdout=subprocess.PIPE)
    
    print(res.stdout.read().decode('utf-8'))
    
    #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
    res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
    print(res1.stdout.read().decode('utf-8'))



  • 相关阅读:
    To My Girlfriend (DP)
    A Simple Nim (SG打表找规律)
    A Boring Question (打表)
    A Simple Chess (Lucas组合数 + 容斥)
    World is Exploding (容斥 + 统计)
    Two (DP)
    K-wolf Number (数位DP)
    Interesting (manacher + 前缀和处理)
    How Many Triangles (极角排序 + 尺取法)
    Divide the Sequence (贪心)
  • 原文地址:https://www.cnblogs.com/lijunc/p/12607736.html
Copyright © 2011-2022 走看看