21-configparser、subprocess模块

一、configparser模块

1.1 configparser用法

读取

import configparser

config=configparser.ConfigParser()
config.read('test.ini')             # ini 格式可区分语法,read什么格式都可以,重要的是里面的数据

# 1、获取sections
print(config.sections())

# 2、获取某一section下的所有options
print(config.options('section1'))

# 3、获取items
print(config.items('section1'))

# 4、获取options对应的值
res=config.get('section1','user')
print(res,type(res))

res=config.getint('section1','age')
print(res,type(res))


res=config.getboolean('section1','is_admin')
print(res,type(res))


res=config.getfloat('section1','salary')
print(res,type(res))

改写

import configparser

config=configparser.ConfigParser()
config.read('a.cfg',encoding='utf-8')


#删除整个标题section2
config.remove_section('section2')

#删除标题section1下的某个k1和k2
config.remove_option('section1','k1')
config.remove_option('section1','k2')

#判断是否存在某个标题
print(config.has_section('section1'))

#判断标题section1下是否有user
print(config.has_option('section1','user'))


#添加一个标题
config.add_section('egon')

#在标题egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
config.set('egon','age',18) #报错,必须是字符串


#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

1.2 configparser读取的文件语法

# 注释1
; 注释2

[section1]     # sections 写在[]中
k1 = v1       # 存储格式为,option对应值,使用k = v 或 k : v 
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2]
k1 = v1

 

二、subprocess 模块

基本用法:

import subprocess

obj=subprocess.Popen('ls',shell=True,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE,
                 )

print(obj)
res=obj.stdout.read()
print(res.decode("gbk"))
#
err_res=obj.stderr.read()
# print(err_res.decode('utf-8'))      # 'utf-8' codec can't decode byte 0xb2 in position 5: invalid start byte
print(err_res.decode('gbk'))        # 'ls' 不是内部或外部命令,也不是可运行的程序或批处理文件。

需要注意的点:

 

import subprocess

obj=subprocess.Popen('dir',shell=True,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE,
                 )

print(obj)
# if obj.stdout:    # 必须加read() 不然即使里面没有内容,obj.stdout仍为真
# if obj.stdout.read():     # stdout中数据也只能被读一次,为求统一,我们也采取赋值给变量的形式
res=obj.stdout.read()
if obj.stdout.read():
    print(res.decode("gbk"))

# if obj.stderr.read() 将无法正常打印错误信息,因为此处只要被读取,管道中便为空。
# 只能被读一次,所以最好直接赋值给变量使用不要采用 if obj.stderr.read()来判断
err_res = obj.stderr.read()
if err_res:
    # print(err_res.decode('utf-8'))      # 'utf-8' codec can't decode byte 0xb2 in position 5: invalid start byte
    print(err_res.decode('gbk'))        # 'ls' 不是内部或外部命令,也不是可运行的程序或批处理文件。

 

 

 

 

 

可运行系统指令,并将正确或错误结果分别存入不同的管道。

且subprocess使用系统默认编码类型,但得到的结果为bytes类型,可进行转换

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'))


#windows下:
# dir | findstr 'test*'
# dir | findstr 'txt$'
import subprocess
res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函数备课',shell=True,stdout=subprocess.PIPE)
res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
                 stdout=subprocess.PIPE)

print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码

 

posted @ 2020-04-01 00:04  Jil-Menzerna  阅读(188)  评论(0)    收藏  举报