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:\\Python\xd1\xa7\xcf\xb0\xcf\xe0\xb9\xd8\\\xce\xd2\xb5\xc4\xb2\xa9\xbf\xcd\xce\xc4\xbc\xfe\\Python\xd5\xfd\xbf\xce\xc4\xda\xc8\xdd' \xb2\xbb\xca\xc7\xc4\xda\xb2\xbf\xbb\xf2\xcd\xe2\xb2\xbf\xc3\xfc\xc1\xee\xa3\xac\xd2\xb2\xb2\xbb\xca\xc7\xbf\xc9\xd4\xcb\xd0\xd0\xb5\xc4\xb3\xcc\xd0\xf2\r\n\xbb\xf2\xc5\xfa\xb4\xa6\xc0\xed\xce\xc4\xbc\xfe\xa1\xa3\r\n"
print(err_res)  # b"'E:\\Python\xd1\xa7\xcf\xb0\xcf\xe0\xb9\xd8\\\xce\xd2\xb5\xc4\xb2\xa9\xbf\xcd\xce\xc4\xbc\xfe\\Python\xd5\xfd\xbf\xce\xc4\xda\xc8\xdd' \xb2\xbb\xca\xc7\xc4\xda\xb2\xbf\xbb\xf2\xcd\xe2\xb2\xbf\xc3\xfc\xc1\xee\xa3\xac\xd2\xb2\xb2\xbb\xca\xc7\xbf\xc9\xd4\xcb\xd0\xd0\xb5\xc4\xb3\xcc\xd0\xf2\r\n\xbb\xf2\xc5\xfa\xb4\xa6\xc0\xed\xce\xc4\xbc\xfe\xa1\xa3\r\n"

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'))
posted @ 2020-03-31 19:39  小小码农梦还家  阅读(15)  评论(0编辑  收藏  举报