一、常用模块
shutil
xml
configparser
hashlib
subprocess
1、shutil模块
用途:高级的文件、文件夹、压缩包处理模块
shutil.copyfileobj(fsrc,fdst[,length])
将文件内容拷贝到另一个文件中
1 import shutil 2 shutil.copyfileobj(open('old.xml','r'),open('new.xml','w'))
shutil.copyfile(src,dst)
拷贝文件
til.copyfile('haha.log','xixi.log') #目标文件无需存在
shutil.copymode(src,dst)
仅拷贝权限。内容、组、用户均不变
shutil.copymode('haha.log','xixi.log') #目标文件必须存在
shutil.copystat(src,dst)
仅拷贝状态的信息,包括:mode bits,atime,mtime,flags
shutil.copystat('haha.log','xixi.log') #目标文件必须存在
shutil.copy(src,dst)
拷贝文件和权限
shutil.copy('haha.log','xixi.log')
shutil.copy2(src,dst)
拷贝文件和状态信息
shutil.copy2('haha.log','xixi.log')
shutil.ignore_patterns(*patterns)
shutil.copytree(src,dst,symlinks=False,ignore=None)
递归的去拷贝文件夹
shutil.copytree('old_dir','new_dir',ignore=shutil.ignore_patterns('*.pyc','tmp*')) #目标目录不能存在,注意对new_dir目录父目录要有可写权限,ignore的意思是排除

shutil.copytree('f1','f2',symlinks=True,ignore=shutil.ignore_patterns('*.pyc','tmp*')) #通常对的拷贝都把软链接拷贝成硬链接,即对待软链接来说,创建新的文件
shutil.rmtree(path[,ignore_errors[,onerror]])
递归的去删除文件
shutil.rmtree('old_dir')
shutil.move(src,dst)
递归的去移动文件,它类似mv命令,其实就是重命名
shutil.move('old_file','new_file')
shutil.make_archive(base_name,format,...)
创建压缩包并返回文件路径,例如:zip、tar
- base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如 data_bak =>保存至当前路径
如:/tmp/data_bak =>保存至/tmp/ - format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要压缩的文件夹路径(默认当前目录)
- owner: 用户,默认当前用户
- group: 组,默认当前组
- logger: 用于记录日志,通常是logging.Logger对象
#将 /data下的文件打包放置当前程序目录 ret = shutil.make_archive('data_bak','gztar',root_dir='/data') #将 /data下的文件打包放置 /tmp/目录 ret = shutil.make_archive('/tmp/data_bak','gztar',root_dir='/data')

import zipfile #压缩 z = zipfile.ZipFile('access_log.zip','w') z.write('a.log') z.write('data.data') z.close() #解压 z = zipfile.ZipFile('access_log.zip','r') z.extractall(path='.') z.close()

import tarfile #压缩 t = tarfile.open('/tmp/data.tar','w') t.add('/test1/a.py',arcname='a.bak') t.add('/test1/b.py',arcname='b.bak') t.close() #解压 t = tarfile.open('/tmp/data.tar','r') t.extractall('/data') t.close()
2、xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在json还没诞生的黑暗年代,大家只能选择使用xml,至今很多传统公司如金融行业的很多系统的接口还主要是xml
xml的格式如下,就是通过<>节点来区别数据结构的

<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> <egon age="18">hello</egon></country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> <egon age="18">hello</egon></country> <country name="Panama"> <year>2011</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> <egon age="18">hello</egon></country> </data>
xml协议在各个语言里都是支持的,在python中可以用以下模块操作xml:
print(root.iter('year')) #全文搜索 print(root.find('country')) #在root的子节点找,只找一个 print(root.findall('country')) #在root的子节点找,找所有

import xml.etree.ElementTree as ET tree = ET.parse('a.xml') root = tree.getroot() print(root.tag) #遍历xml文档 for child in root: print('====>',child.tag,child.attrib,child.attrib['name']) for i in child: print(i.tag,i.attrib,i.text) #只遍历year节点 for node in root.iter('year'): print(node.tag,node.text) #增加node for country in root.iter('country'): e = ET.Element('xiaoyu') e.text = 'hello' e.attrib = {'age':'18'} country.append(e) tree.write('a.xml') #删除node for country in root.iter('country'): print(country.tag) rank = country.find('rank') if int(rank.text) > 10: country.remove(rank) tree.write('a.xml') #修改node for node in root.iter('year'): new_year = int(node.text) + 1 node.text = str(new_year) node.set('updated','yes') node.set('version','1.0') tree.write('a.xml')

import xml.etree.ElementTree as ET new_xml = ET.Element("namelist") name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"}) age = ET.SubElement(name,"age",attrib={"checked":"no"}) sex = ET.SubElement(name,"sex") sex.text = '33' name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"}) age = ET.SubElement(name2,"age") age.text = '19' et = ET.ElementTree(new_xml) #生成文档对象 et.write("test.xml", encoding="utf-8",xml_declaration=True) ET.dump(new_xml) #打印生成的格式
3、configparser模块

# 注释1 ; 注释2 [section1] k1 = v1 k2:v2 user=egon age=18 is_admin=true salary=31 [section2] k1 = v1
读操作
import configparser config=configparser.ConfigParser() config.read('a.cfg') #查看所有的标题 res=config.sections() #['section1', 'section2'] print(res) #查看标题section1下所有key=value的key options=config.options('section1') print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary'] #查看标题section1下所有key=value的(key,value)格式 item_list=config.items('section1') print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')] #查看标题section1下user的值=>字符串格式 val=config.get('section1','user') print(val) #egon #查看标题section1下age的值=>整数格式 val1=config.getint('section1','age') print(val1) #18 #查看标题section1下is_admin的值=>布尔值格式 val2=config.getboolean('section1','is_admin') print(val2) #True #查看标题section1下salary的值=>浮点型格式 val3=config.getfloat('section1','salary') print(val3) #31.0
修改操作
import configparser config=configparser.ConfigParser() config.read('a.cfg') #删除整个标题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','')) #添加一个标题 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'))
4、hashlib模块
hash:一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
三个特点:
1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
2.不可逆推
3.相同算法:无论校验多长的数据,得到的哈希值长度固定。
import hashlib m = hashlib.md5() m.update('hello'.encode('utf-8')) print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592 m.update('haha'.encode('utf-8')) print(m.hexdigest()) #ab540ca02784f1e2f0fb8e1d2d1d92a9 m2 = hashlib.md5() m2.update('hellohaha'.encode('utf-8')) print(m2.hexdigest()) #ab540ca02784f1e2f0fb8e1d2d1d92a9 ''' 注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样 但是update多次为校验大文件提供了可能。 '''
以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。
import hashlib # ######## 256 ######## hash = hashlib.sha256('898oaFs09f'.encode('utf8')) hash.update('alvin'.encode('utf8')) print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7

import hashlib passwds=[ 'alex3714', 'alex1313', 'alex94139413', 'alex123456', '123456alex', 'a123lex', ] def make_passwd_dic(passwds): dic={} for passwd in passwds: m=hashlib.md5() m.update(passwd.encode('utf-8')) dic[passwd]=m.hexdigest() return dic def break_code(cryptograph,passwd_dic): for k,v in passwd_dic.items(): if v == cryptograph: print('密码是===>