Day 09 configparser、hashlib与subprocess模块
1. configparser模块
加载某种特定格式的配置文件
[section1]
option
[section2]
optiion
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import configparser
config=configparser.ConfigParser()
config.read('路径')
config.sections()#获取section
config.options('sections1')#获取某一section下所有options
config.items('sections1')#获取某一section下所有options及其所有值
config.get('sections','user')#某一个具体的配置
2.hashlib模块
2.1 hash
是一类算法,为该算法接收传入的内容,经过一系列运算得到一串的hash值
hash值的特点:
- 内容一样,hash值一样
- 只要hash算法不变,无论内容多大,hash值长度不变
- 不能由hash反解成内容
hash的用途:
- 文件校验(校验hash值)
- 密文密码传输与验证
import hashlib
m=hashlib.md5()
m.update('hello'.encode('utf-8'))
m.update('world'.encode('utf-8'))
res=m.hexdigest()
print(res)
3.subprocess
用于执行系统命令
import subprocess
obj=subprocess.Popen('dir d:\\pyt',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)#out是标准正确输出的管道;err是标准错误输出的管道
err_res=obj.stdout.read()
err_read=obj.stderr.read()
print(err_read)
print(err_res.decode('gbk'))#linux编码utf-8,windows编码gbk