模块简介javascript
1、time模块
2、sys模块
3、datetime模块
4、pickle模块
5、json模块
6、OS模块
7、hashlib加密模块
8、第三方模块的安装方法
9、requests模块
10、XML模块
11、configparser模块
12、shutil
十3、subprocess模块
十4、logging模块php
模块的分类html
一、内置模块 java
二、自定义模块node
三、第三方模块(须要安装)python
模块前言:模块的导入:web
模块的导入有两点须要注意:算法
一、在sys.path中的能够直接进行导入,由于sys.path返回的是列表,因此能够利用append把路径加入到列表当中shell
二、把模块导入后,再次导入模块,python会检测内存中是否存在该模块,若是存在该模块则会忽略导入django
1、time模块
一、time.sleep(5) #等待5秒钟
#!usr/bin/env python # -*- coding:utf-8 -*- import time print('start to sleep.....') time.sleep(5) #等待5秒 print('wake up.....')
#!usr/bin/env python # -*- coding:utf-8 -*- import time print(time.clock()) #返回处理器时间,3.3已经废弃 print(time.process_time()) #返回处理器时间,3.3已经废弃 print(time.time()) #返回时间戳,1970年1月1号0:00至如今一共多少秒,全部系统的时间戳都是从该时间计时的 print(time.ctime(time.time())) #将时间戳转化为字符串,Thu Feb 16 10:10:20 2017 print(time.ctime(time.time()-24*60*60)) #将时间转化为字符串,昨天的这个时间,Thu Feb 15 10:10:20 2017 print(time.gmtime(time.time())) #返回time.struct_time对象,能够分别提取年月日时分秒 print(time.localtime(time.time())) #返回time.struct_time对象,对象显示的是本地时间,能够分别提取年月日时分秒 print(time.mktime(time.localtime(time.time()))) #与localtime功能相反,将time.struct_time对象转化为时间戳 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) #将time.struct_time对象转化为字符串格式
print(time.strftime('%Y-%m-%d %H:%M:%S') #输出当前时间
print(time.strptime('2017-2-16 11:40:27','%Y-%m-%d %H:%M:%S')) #将字符串转化为time.struct_time对象
#!usr/bin/env python # -*- coding:utf-8 -*- #time.gmtime()的用法 #time.localtime()的用法 import time time_object = time.gmtime(time.time()) print(time_object) #time.struct_time(tm_year=2017, tm_mon=2, tm_mday=16, # tm_hour=2, tm_min=35, tm_sec=54, tm_wday=3, tm_yday=47, tm_isdst=0) time_str = '%s-%s-%s %s:%s:%s' %(time_object.tm_year,time_object.tm_mon,time_object.tm_mday,time_object.tm_hour,time_object.tm_min,time_object.tm_sec) print(time_str) #2017-2-16 2:45:26 格林威治时间
2、sys模块
1)sys.argv ----命令参数List,第一个参数是程序自己路径
import sys print(sys.argv) if sys.argv[1] == 'one': print('11111') else: print('.....')
在终端中运行结果以下:
E:\python_code\2\11day>python time2.py one
['time2.py', 'one']
11111
2)sys.path ----返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.path) #打印输出: #['E:\\python_code\\2\\11day', # 'E:\\python_code\\2', # 'D:\\python_lib', # 'D:\\Program Files (x86)\\Python36\\python36.zip', # 'D:\\Program Files (x86)\\Python36\\DLLs', # 'D:\\Program Files (x86)\\Python36\\lib', # 'D:\\Program Files (x86)\\Python36', # 'D:\\Program Files (x86)\\Python36\\lib\\site-packages']
当导入python包时,会从sys.path路径返回的列表中查找包,找到则运行,并返回。
通常第三方安装的路径都是在:
D:\\Program Files (x86)\\Python36\\lib\\site-packages
3)sys.exit(n) ----退出程序,正常退出时exit(0)
#!usr/bin/env python # -*- coding:utf-8 -*- import sys st = input('退出程序:q;输入1打印one;输入2打印two:') if st == 'q': sys.exit('goodbye!') #也能够用:exit('goodbye!'),退出当前程序 elif st == '1': print('one') elif st == '2': print('two') print('thanks!')
4)sys.version ----获取python解释器程序的版本信息
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.version) #打印输出: #3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
5)sys.platform ----返回操做系统平台名称
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.platform) #打印输出win32
6)sys.stdout.wirte("#") #在同一行中输出字符串,能够作进度条原理
pip.exe install django ----从网络下载django并安装,显示安装百分比和进度条
pip uninstall django ----卸载django
import sys import time for i in range(20): sys.stdout.write('#') time.sleep(0.3)
在终端中输入上面的代码,能够像进度条同样不断输出#
7)sys.stdin.readline() #屏幕输入函数
#!usr/bin/env python # -*- coding:utf-8 -*- import sys val = sys.stdin.readline()[:-1] #[:-1]能够用切片截取输入的字符串 print(val) #将输入的内容打印出来,input函数底层实际也调用的这个函数
8) 练习:
读取用户输入的目录,根据用户的输入,建立一个相应的目录
# -*- coding:utf-8 -*- import sys,os os.mkdir(sys.argv[1])
本身写一个简单的脚本,能够在任何路径导入
本身作一个带百分比的进度条
#!usr/bin/env python # -*- coding:utf-8 -*- import sys,time for i in range(11): sys.stdout.write('\r') #回车符 sys.stdout.write('%s%% %s' %(i *10,'#' * i)) sys.stdout.flush() #从显存刷新到屏幕,没打印一次刷新一次屏幕 time.sleep(0.2)
3、datetime模块
#时间加减 import datetime print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19 print(datetime.datetime.now() ) print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分,
参数:days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0
c_time = datetime.datetime.now() print(c_time.replace(minute=3,hour=2)) #时间替换
4、pickle模块
一、把字典转化为字符串写入文件
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } f = open('ac.txt','w',encoding='utf-8') text = str(accounts) print(text) f.write(text) f.close()
但写入文件后,进行没法进行修改,因此能够用pickle模块
二、pickle把字典转为字节类型存入文件
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import pickle f = open('ac.txt','wb') text = pickle.dumps(accounts) #返回的是(bytes)字节类型,将字典类型转化为字节类型,无论以何种方式打开文件,均返回字节型,因此必须用wb方式打开能够直接写入 f.write(text) #写入文件 f.close()
dumps与loads均以字节的形式写入和读取的
#!usr/bin/env python # -*- coding:utf-8 -*- import pickle f = open('ac.txt','rb') f_read = f.read() #把pickle写入的字符串还原为字节形式 print(f_read) dic_text = pickle.loads(f_read) #pickle.dumps存储的字符串转化为字典 print(type(dic_text)) #打印输出:<class 'dict'>
例子:修改文件中ID为1000的balance的值+500
#!usr/bin/env python # -*- coding:utf-8 -*- import pickle f = open('ac.txt','rb') text = f.read() #读取文本内容 dic_text = pickle.loads(text) #利用pickle将文本内容转化为字典类型 dic_text[1000]['balance'] += 500 #修改字典中的值 f.close() f = open('ac.txt','wb') text_write = pickle.dumps(dic_text) #将字典转化为文本内容 f.write(text_write) #把文本内容从新覆盖写入文件 f.close() f = open('ac.txt','rb') text_read = f.read() #读取文件中的文本内容 text_dic = pickle.loads(text_read) #利用pickle将文本内容转化为字典 print(type(text_dic),text_dic) #打印字典的类型和字典内容
三、pickle中的dump和load 与 dumps和loads的区别
dumps能够将字典转为字节,而后以字节方式打开写入文件
dump 能够直接将字典以字节方式打开的文件
loads 能够将字节类型的字符串,直接转化为字典
load能够将以字节方式打开的文件对象直接转化为字典
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import pickle f = open('ac.txt','wb') pickle.dump(accounts,f) #dump能够直接把字典写入文件 f.close() f = open('ac.txt','rb') dic = pickle.load(f) #load能够直接把文件内容读出为字典类型 print(type(dic),dic)
5、json模块
json模块与pickle模块用法一致;
区别:
一、pickle模块已字节的方式进行转化,而json是以字符串的方式进行转化。
二、pickle模块能够将任何数据类型序列化,但json不支持某些数据类型的序列化,如data,json只支持字典、列表、简单的变量、字符串等,json不支持元组,稍微复杂的数据类型就不能处理了
三、pickle只在python语言支持,不一样语言之间、不一样系统之间的接口、数据交互为了通用性,通常选择json。
dumps和loads的用法:
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import json f = open('ac.txt','w',encoding='utf-8') text = json.dumps(accounts) #将字典转化为字符串 f.write(text) #将转化后的字符串写入文件 f.close() f = open('ac.txt','r',encoding='utf-8') text1 = f.read() #读取文件中的字符串 dic = json.loads(text1) #将字符串转化为字典 print(type(dic),dic) f.close()
dump和load的用法:
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import json #dump和load f = open('ac.txt','w',encoding='utf-8') json.dump(accounts,f) #直接将accounts转化为文本,写入f对象 f.close() f = open('ac.txt','r',encoding='utf-8') dic = json.load(f) #直接读取f对象中的文件,并转化为字典 print(type(dic),dic)
注意:python代码中列表、字典等内部尽可能的字符串使用双号,若是将含有单引号的字符串传给json,则会报错,因此之后列表、字典等内部的字符串尽可能使用双引号loads时使用双引号表示字符串,dumps可使用单引号表示字符串,例如:
#!usr/bin/env python # -*- coding:utf-8 -*- import json st = '{"name":"sunshuhai","age":100}' #若是st = "{'name':'sunshuhai','age':100}"则会报错 st_dic = json.loads(st) print(type(st_dic),st_dic)
json不支持元组
tup = (11,22,33) import json r2 = json.dumps(tup) print(r2) #打印输出[11, 22, 33] s = '("11","22","33")' import json r1 = json.loads(s) print(r1) #会报错,由于json不支持元组,只支持列表和字典
6、OS模块
1 os.getcwd() 获取当前工做目录,即当前python脚本工做的目录路径 2 os.chdir("dirname") 改变当前脚本工做目录;至关于shell下cd 3 os.curdir 返回当前目录: ('.') 4 os.pardir 获取当前目录的父目录字符串名:('..') 5 os.makedirs('dir1/dir2') 可生成多层递归目录 6 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 7 os.mkdir('dirname') 生成单级目录;至关于shell中mkdir dirname 8 os.rmdir('dirname') 删除单级空目录,若目录不为空则没法删除,报错;至关于shell中rmdir dirname 9 os.listdir('dirname') 列出指定目录下的全部文件和子目录,包括隐藏文件,并以列表方式打印 10 os.remove() 删除一个文件 11 os.rename("oldname","new") 重命名文件/目录 12 os.stat('path/filename') 获取文件/目录信息 13 os.sep 操做系统特定的路径分隔符,win下为"\\",Linux下为"/" 14 os.linesep 当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" 15 os.pathsep 用于分割文件路径的字符串 16 os.name 字符串指示当前使用平台。win->'nt'; Linux->'posix' 17 os.system("bash command") 运行shell命令,直接显示 18 os.environ 获取系统环境变量 19 os.path.abspath(path) 返回path规范化的绝对路径 20 os.path.split(path) 将path分割成目录和文件名二元组返回 21 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 22 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 23 os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False 24 os.path.isabs(path) 若是path是绝对路径,返回True 25 os.path.isfile(path) 若是path是一个存在的文件,返回True。不然返回False 26 os.path.isdir(path) 若是path是一个存在的目录,则返回True。不然返回False 27 os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径以前的参数将被忽略 28 os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间 29 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
7、hashlib加密模块
用于加密相关的操做,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import hashlib # ######## md5 ######## hash = hashlib.md5() # help(hash.update) hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) print(hash.digest()) ######## sha1 ######## hash = hashlib.sha1() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) 以上加密算法虽然依然很是厉害,但时候存在缺陷,即:经过撞库能够反解。因此,有必要对加密算法中添加自定义key再来作加密。 import hashlib # ######## md5 ######## hash = hashlib.md5(bytes('898oaFs09f',encoding="utf-8")) #加盐 hash.update(bytes('admin',encoding="utf-8")) print(hash.hexdigest()) #python内置还有一个 hmac(哈希信息验证码 Hash Message Authentication Code) 模块,它内部对咱们建立 key 和 内容 进行进一步的处理而后再加密 import hmac h = hmac.new(bytes('898oaFs09f',encoding="utf-8")) h.update(bytes('admin',encoding="utf-8")) print(h.hexdigest())
实例:加密帐号注册及登陆
#!usr/bin/env python # -*- coding:utf-8 -*- import hashlib def register(user,pwd): with open('user_information','a',encoding='utf-8') as f: f.write(user + '|' + md5(pwd) + '\r') def login(user,pwd): with open('user_information','r',encoding='utf-8') as f: for line in f: u,p = line.strip().split('|') if user == u and md5(pwd) == p: return True return False def md5(arg): hash = hashlib.md5(bytes('aaatttbbbccceee',encoding='utf-8')) hash.update(bytes(arg,encoding='utf-8')) return hash.hexdigest() option = input('用户注册请按1,用户登录请按2:') if option == '1': user = input('请输入用户名:') pwd = input('请输入密码:') register(user,pwd) elif option == '2': user = input('请输入用户名:') pwd = input('请输入密码:') if login(user,pwd): print('登录成功!') else: print('登录失败!')
8、第三方模块的安装方法
第三方模块的安装有两种方式:
1)软件工具安装,例如要安装requests
通常python3中默认都安装着pip3,路径为:D:\Program Files (x86)\Python36\Scripts
2)源码安装
9、requests模块
一、requests获取HTML
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.baidu.com') response.encoding = 'utf-8' html = response.text print(html)
二、利用requests模块和xml模块判断QQ是否在线
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=258888289') response.encoding = 'utf-8' result = response.text print(result) # <?xml version="1.0" encoding="utf-8"?> # <string xmlns="http://WebXml.com.cn/">Y</string> #须要拿到上面XML中的Y from xml.etree import ElementTree as ET xml_obj = ET.XML(result) if xml_obj.text == 'Y': print('您要查找的QQ在线!') else: print('您要查找的QQ离线!')
三、利用requests 模块查找列车车次信息
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') response.encoding = 'utf-8' result = response.text from xml.etree import ElementTree as ET root = ET.XML(result) for node in root.iter('TrainDetailInfo'): #print(node.tag,node.attrib) #tag是表示标签名,attrib表示属性 print(node.find('TrainStation').text,node.find('ArriveTime').text,node.find('StartTime').text,node.find('KM').text)
#!/usr/bin/env python # -*- coding:utf-8 -*- import requests import json import xlwings as xw def PostDataA(): wb = xw.Book.caller() mastUrl = wb.sheets["配置表"].range('B1').value #主地址 #wb.sheets["Sheet99"].range('D8').value = "" coo = wb.sheets["Sheet99"].range('D1').value #coo = '{"_c_cookie": "1", "_c_aut": "nm%3FwuskI%60p%2CX%5Ezo3FrAF%60%2CQTNEi_PTp_%2CnN%40By_oSP%3D%2CtnR%5EbpWg%7C%7E","PHPSESSID": "le50ak8oncg4cvv0kr5dum2d17", "g_language": "zh-cn", "_c_chk": "7814UHBHV18GT0FEUw%3D%3D"}' cookie_dic = json.loads(coo) #原cookie字典 heads = { "Accept":"application/json, text/javascript, */*; q=0.01", "Accept-Encoding":"gzip, deflate, sdch", "Accept-Language":"zh-CN,zh;q=0.9", "Connection":"keep-alive", "Host": "ww2671.ws5170.com", "Referer": mastUrl + "op.php?op=member_5h&fp=bet_beforedo&palygroup=r1&gametype=24", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36", "X-Requested-With":"XMLHttpRequest", } data_str = wb.sheets["Sheet99"].range('D8').value data_dic = json.loads(data_str) r = requests.post(mastUrl + 'op.php?op=member_5h&fp=bet_do&palygroup=r1&gametype=24&disk=4',cookies=cookie_dic,headers=heads,data=data_dic) # r.encoding='gb2312' html=r.text if html[0:13]=="<script>paren": wb.sheets["Sheet99"].range('D7').value="Y"
10、XML模块
单行XML解析
response = '<string xmlns="http://WebXml.com.cn/">Y</string>' from xml.etree import ElementTree as ET node = ET.XML(response) print(node.text) #打印输出 Y
创建XML文件代码为例,文件名为first.xml:
<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2026</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country> </data>
读取xml文件为字符串,将字符串转化为XML对象:
#!usr/bin/env python # -*- coding:utf-8 -*- with open('first.xml','r',encoding='utf-8') as f: xml_text = f.read() from xml.etree import ElementTree as ET root = ET.XML(xml_text) #这是<data>层 for node in root: #循环的是data下层,也就是country层 print(node.find('rank').tag) #打印标签 print(node.find('rank').attrib) #打印rank标签的属性 print(node.find('rank').text) #打印rank标签中的内容
from xml.etree import ElementTree as ET root = ET.XML(st) for r in root.iter('country'): #表示跟节点下面的全部子孙标签中country的节点集合 print(r.tag) # country # country # country
#遍历全部标签的内容 from xml.etree import ElementTree as ET root = ET.XML(st) for r2 in root: #root表示country层,那么r2表示data层下的每个country层 for r3 in r2: #r2是country层,那么r3就是country下面的每一个标签层 print(r3.text) #打印r3层的每一个标签的内容
直接将xml文件转化为xml对象,此方式能够修改xml对象并更新文件:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') #将文件读入内存并解析为xml对象 root = tree.getroot() #获取对象中的第一层根目录 print(root.tag) #打印输出data for node in root.iter('year'): #root下面的year标签进行循环 new_year = int(node.text) + 1 #将year标签中的内容转为int类型而且加1 node.text = str(new_year) #将new_year转化为字符串从新复制给year标签中的内容 node.set('name','sea') #给year标签增长一个属性:name='sea' node.set('age','28') #给year标签增长一个属性:age='28' del node.attrib['name'] #把year标签中的name属性删除 tree.write('first.xml') #将内存中修改完毕的xml字符串,从新写进first.xml
删除节点:
#!usr/bin/env python # -*- coding:utf-8 -*- #删除节点 from xml.etree import ElementTree as ET tree = ET.parse('first.xml') print(tree) root = tree.getroot() for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) #删除root根下的country节点 tree.write('first.xml')
打印xml下root的主要功能:
#!usr/bin/env python # -*- coding:utf-8 -*- #打印功能 from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() print(dir(root)) #root功能有: ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'attrib', 'clear', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text']
经常使用的功能:
tag attrib find set get iter
建立XML文档实例:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET #建立跟节点 new_xml = ET.Element('namelist') #建立子节点 name1 = ET.SubElement(new_xml,'name',attrib={"enrolled":"yes"}) age1 = ET.SubElement(name1,'age',attrib={"checked":"no"}) sex1 = ET.SubElement(name1,'sex') sex1.text = '33' #建立根节点的子节点 name2 = ET.SubElement(new_xml,'name',attrib={"enrolled":"no"}) age2 = ET.SubElement(name2,'age') age2.text = '19' #写入硬盘 et = ET.ElementTree(new_xml) #建立ElementTree类的对象,参数是根节点,该对象有write方法 et.write('test.xml',encoding='utf-8',xml_declaration=True)
操做XML
XML格式类型是节点嵌套节点,对于每个节点均有如下功能,以便对当前节点进行操做:
1 class Element: 2 """An XML element. 3 4 This class is the reference implementation of the Element interface. 5 6 An element's length is its number of subelements. That means if you 7 want to check if an element is truly empty, you should check BOTH 8 its length AND its text attribute. 9 10 The element tag, attribute names, and attribute values can be either 11 bytes or strings. 12 13 *tag* is the element name. *attrib* is an optional dictionary containing 14 element attributes. *extra* are additional element attributes given as 15 keyword arguments. 16 17 Example form: 18 <tag attrib>text<child/>...</tag>tail 19 20 """ 21 22 当前节点的标签名 23 tag = None 24 """The element's name.""" 25 26 当前节点的属性 27 28 attrib = None 29 """Dictionary of the element's attributes.""" 30 31 当前节点的内容 32 text = None 33 """ 34 Text before first subelement. This is either a string or the value None. 35 Note that if there is no text, this attribute may be either 36 None or the empty string, depending on the parser. 37 38 """ 39 40 tail = None 41 """ 42 Text after this element's end tag, but before the next sibling element's 43 start tag. This is either a string or the value None. Note that if there 44 was no text, this attribute may be either None or an empty string, 45 depending on the parser. 46 47 """ 48 49 def __init__(self, tag, attrib={}, **extra): 50 if not isinstance(attrib, dict): 51 raise TypeError("attrib must be dict, not %s" % ( 52 attrib.__class__.__name__,)) 53 attrib = attrib.copy() 54 attrib.update(extra) 55 self.tag = tag 56 self.attrib = attrib 57 self._children = [] 58 59 def __repr__(self): 60 return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self)) 61 62 def makeelement(self, tag, attrib): 63 建立一个新节点 64 """Create a new element with the same type. 65 66 *tag* is a string containing the element name. 67 *attrib* is a dictionary containing the element attributes. 68 69 Do not call this method, use the SubElement factory function instead. 70 71 """ 72 return self.__class__(tag, attrib) 73 74 def copy(self): 75 """Return copy of current element. 76 77 This creates a shallow copy. Subelements will be shared with the 78 original tree. 79 80 """ 81 elem = self.makeelement(self.tag, self.attrib) 82 elem.text = self.text 83 elem.tail = self.tail 84 elem[:] = self 85 return elem 86 87 def __len__(self): 88 return len(self._children) 89 90 def __bool__(self): 91 warnings.warn( 92 "The behavior of this method will change in future versions. " 93 "Use specific 'len(elem)' or 'elem is not None' test instead.", 94 FutureWarning, stacklevel=2 95 ) 96 return len(self._children) != 0 # emulate old behaviour, for now 97 98 def __getitem__(self, index): 99 return self._children[index] 100 101 def __setitem__(self, index, element): 102 # if isinstance(index, slice): 103 # for elt in element: 104 # assert iselement(elt) 105 # else: 106 # assert iselement(element) 107 self._children[index] = element 108 109 def __delitem__(self, index): 110 del self._children[index] 111 112 def append(self, subelement): 113 为当前节点追加一个子节点 114 """Add *subelement* to the end of this element. 115 116 The new element will appear in document order after the last existing 117 subelement (or directly after the text, if it's the first subelement), 118 but before the end tag for this element. 119 120 """ 121 self._assert_is_element(subelement) 122 self._children.append(subelement) 123 124 def extend(self, elements): 125 为当前节点扩展 n 个子节点 126 """Append subelements from a sequence. 127 128 *elements* is a sequence with zero or more elements. 129 130 """ 131 for element in elements: 132 self._assert_is_element(element) 133 self._children.extend(elements) 134 135 def insert(self, index, subelement): 136 在当前节点的子节点中插入某个节点,即:为当前节点建立子节点,而后插入指定位置 137 """Insert *subelement* at position *index*.""" 138 self._assert_is_element(subelement) 139 self._children.insert(index, subelement) 140 141 def _assert_is_element(self, e): 142 # Need to refer to the actual Python implementation, not the 143 # shadowing C implementation. 144 if not isinstance(e, _Element_Py): 145 raise TypeError('expected an Element, not %s' % type(e).__name__) 146 147 def remove(self, subelement): 148 在当前节点在子节点中删除某个节点 149 """Remove matching subelement. 150 151 Unlike the find methods, this method compares elements based on 152 identity, NOT ON tag value or contents. To remove subelements by 153 other means, the easiest way is to use a list comprehension to 154 select what elements to keep, and then use slice assignment to update 155 the parent element. 156 157 ValueError is raised if a matching element could not be found. 158 159 """ 160 # assert iselement(element) 161 self._children.remove(subelement) 162 163 def getchildren(self): 164 获取全部的子节点(废弃) 165 """(Deprecated) Return all subelements. 166 167 Elements are returned in document order. 168 169 """ 170 warnings.warn( 171 "This method will be removed in future versions. " 172 "Use 'list(elem)' or iteration over elem instead.", 173 DeprecationWarning, stacklevel=2 174 ) 175 return self._children 176 177 def find(self, path, namespaces=None): 178 获取第一个寻找到的子节点 179 """Find first matching element by tag name or path. 180 181 *path* is a string having either an element tag or an XPath, 182 *namespaces* is an optional mapping from namespace prefix to full name. 183 184 Return the first matching element, or None if no element was found. 185 186 """ 187 return ElementPath.find(self, path, namespaces) 188 189 def findtext(self, path, default=None, namespaces=None): 190 获取第一个寻找到的子节点的内容 191 """Find text for first matching element by tag name or path. 192 193 *path* is a string having either an element tag or an XPath, 194 *default* is the value to return if the element was not found, 195 *namespaces* is an optional mapping from namespace prefix to full name. 196 197 Return text content of first matching element, or default value if 198 none was found. Note that if an element is found having no text 199 content, the empty string is returned. 200 201 """ 202 return ElementPath.findtext(self, path, default, namespaces) 203 204 def findall(self, path, namespaces=None): 205 获取全部的子节点 206 """Find all matching subelements by tag name or path. 207 208 *path* is a string having either an element tag or an XPath, 209 *namespaces* is an optional mapping from namespace prefix to full name. 210 211 Returns list containing all matching elements in document order. 212 213 """ 214 return ElementPath.findall(self, path, namespaces) 215 216 def iterfind(self, path, namespaces=None): 217 获取全部指定的节点,并建立一个迭代器(能够被for循环) 218 """Find all matching subelements by tag name or path. 219 220 *path* is a string having either an element tag or an XPath, 221 *namespaces* is an optional mapping from namespace prefix to full name. 222 223 Return an iterable yielding all matching elements in document order. 224 225 """ 226 return ElementPath.iterfind(self, path, namespaces) 227 228 def clear(self): 229 清空节点 230 """Reset element. 231 232 This function removes all subelements, clears all attributes, and sets 233 the text and tail attributes to None. 234 235 """ 236 self.attrib.clear() 237 self._children = [] 238 self.text = self.tail = None 239 240 def get(self, key, default=None): 241 获取当前节点的属性值 242 """Get element attribute. 243 244 Equivalent to attrib.get, but some implementations may handle this a 245 bit more efficiently. *key* is what attribute to look for, and 246 *default* is what to return if the attribute was not found. 247 248 Returns a string containing the attribute value, or the default if 249 attribute was not found. 250 251 """ 252 return self.attrib.get(key, default) 253 254 def set(self, key, value): 255 为当前节点设置属性值 256 """Set element attribute. 257 258 Equivalent to attrib[key] = value, but some implementations may handle 259 this a bit more efficiently. *key* is what attribute to set, and 260 *value* is the attribute value to set it to. 261 262 """ 263 self.attrib[key] = value 264 265 def keys(self): 266 获取当前节点的全部属性的 key 267 268 """Get list of attribute names. 269 270 Names are returned in an arbitrary order, just like an ordinary 271 Python dict. Equivalent to attrib.keys() 272 273 """ 274 return self.attrib.keys() 275 276 def items(self): 277 获取当前节点的全部属性值,每一个属性都是一个键值对 278 """Get element attributes as a sequence. 279 280 The attributes are returned in arbitrary order. Equivalent to 281 attrib.items(). 282 283 Return a list of (name, value) tuples. 284 285 """ 286 return self.attrib.items() 287 288 def iter(self, tag=None): 289 在当前节点的子孙中根据节点名称寻找全部指定的节点,并返回一个迭代器(能够被for循环)。 290 """Create tree iterator. 291 292 The iterator loops over the element and all subelements in document 293 order, returning all elements with a matching tag. 294 295 If the tree structure is modified during iteration, new or removed 296 elements may or may not be included. To get a stable set, use the 297 list() function on the iterator, and loop over the resulting list. 298 299 *tag* is what tags to look for (default is to return all elements) 300 301 Return an iterator containing all the matching elements. 302 303 """ 304 if tag == "*": 305 tag = None 306 if tag is None or self.tag == tag: 307 yield self 308 for e in self._children: 309 yield from e.iter(tag) 310 311 # compatibility 312 def getiterator(self, tag=None): 313 # Change for a DeprecationWarning in 1.4 314 warnings.warn( 315 "This method will be removed in future versions. " 316 "Use 'elem.iter()' or 'list(elem.iter())' instead.", 317 PendingDeprecationWarning, stacklevel=2 318 ) 319 return list(self.iter(tag)) 320 321 def itertext(self): 322 在当前节点的子孙中根据节点名称寻找全部指定的节点的内容,并返回一个迭代器(能够被for循环)。 323 """Create text iterator. 324 325 The iterator loops over the element and all subelements in document 326 order, returning all inner text. 327 328 """ 329 tag = self.tag 330 if not isinstance(tag, str) and tag is not None: 331 return 332 if self.text: 333 yield self.text 334 for e in self: 335 yield from e.itertext() 336 if e.tail: 337 yield e.tail 338 339 节点功能一览表
建立节点并附加到原文件节点的实例:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() new_root = root.makeelement('name',{'age':'28'}) #在内存建立一个节点,该节点与root没有关系 root.append(new_root) #将建立的节点做为子节点附加到root tree.write('first.xml') #将内存中的节点写入文件
#!usr/bin/env python # -*- coding:utf-8 -*- #!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() new_root = root.makeelement('name',{'age':'28'}) #在内存建立一个节点,该节点与root没有关系 new_root1 = ET.Element('name1',{'age1':'38'}) #利用类建立一个节点,上面的代码实质上是调用了这句 new_root.append(new_root1) #new_root1作为new_root的子节点 root.append(new_root) tree.write('first.xml',encoding='utf-8',xml_declaration=True,short_empty_elements=False) #将内存中的节点写入文件,参数utf-8支持中文, # xml_declaration=True文件头部加上注释, # short_empty_elements=False节点没有值时也全闭合,例如:<country name="Panama"></country>
给xml文件添加缩进:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET root = ET.Element('data') son_root1 = ET.SubElement(root,'country',{'name':'Liechtenstein'}) son_root2 = ET.SubElement(root,'country',{'name':'Singapore'}) son_root3 = ET.SubElement(root,'country',{'name':'Panama'}) son_root1_son1 = ET.SubElement(son_root1,'rank',{'updated':'yes'}) son_root1_son1.text = '2' son_root1_son1 = ET.SubElement(son_root1,'year') son_root1_son1.text = '2025' son_root1_son1 = ET.SubElement(son_root1,'gdppc') son_root1_son1.text = '141100' son_root1_son1 = ET.SubElement(son_root1,'neighbor',attrib = {'direction':'B','name':'Austria'}) son_root1_son1.text = '112233' son_root1_son1 = ET.SubElement(son_root1,'neighbor',attrib = {'direction':'W','name':'Switzerland'}) # et = ET.ElementTree(new_xml) # et.write('test.xml',encoding='utf-8',xml_declaration=True,short_empty_elements=False) #如下为将xml字符串转换为有缩进的字符串 #minidom功能少,效率低,通常咱们用ElementTree,但minidom有美化缩进xml的功能 from xml.dom import minidom root_string = ET.tostring(root,encoding='utf-8') #将ET节点转化为字符串 reparsed = minidom.parseString(root_string) #利用minidom将字符串转化为xml.dom.minidom.Document对象 pretty = reparsed.toprettyxml(indent='\t') #利用minidom函数转化为有缩进格式的xml with open('first.xml','w',encoding='utf-8') as f: f.write(pretty)
XML命名空间:
XML 命名空间提供避免元素命名冲突的方法。 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不一样的文档使用相同的元素名时,就会发生命名冲突。 这个 XML 文档携带着某个表格中的信息: <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> 这个 XML 文档携带有关桌子的信息(一件家具): <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> 假如这两个 XML 文档被一块儿使用,因为两个文档都包含带有不一样内容和定义的 <table> 元素,就会发生命名冲突。 XML 解析器没法肯定如何处理这类冲突。 使用前缀来避免命名冲突 此文档带有某个表格中的信息: <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> 此 XML 文档携带着有关一件家具的信息: <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> 如今,命名冲突不存在了,这是因为两个文档都使用了不一样的名称来命名它们的 <table> 元素 (<h:table> 和 <f:table>)。 经过使用前缀,咱们建立了两种不一样类型的 <table> 元素。 使用命名空间(Namespaces) 这个 XML 文档携带着某个表格中的信息: <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> 此 XML 文档携带着有关一件家具的信息: <f:table xmlns:f="http://www.w3school.com.cn/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> 与仅仅使用前缀不一样,咱们为 <table> 标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。 XML Namespace (xmlns) 属性 XML 命名空间属性被放置于元素的开始标签之中,并使用如下的语法: xmlns:namespace-prefix="namespaceURI" 当命名空间被定义在元素的开始标签中时,全部带有相同前缀的子元素都会与同一个命名空间相关联。 注释:用于标示命名空间的地址不会被解析器用于查找信息。其唯一的做用是赋予命名空间一个唯一的名称。不过,不少公司经常会做为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息。 请访问 http://www.w3.org/TR/html4/。 统一资源标识符(Uniform Resource Identifier (URI)) 统一资源标识符是一串能够标识因特网资源的字符。最经常使用的 URI 是用来标示因特网域名地址的统一资源定位器(URL)。另外一个不那么经常使用的 URI 是统一资源命名(URN)。在咱们的例子中,咱们仅使用 URL。 默认的命名空间(Default Namespaces) 为元素定义默认的命名空间可让咱们省去在全部的子元素中使用前缀的工做。 请使用下面的语法: xmlns="namespaceURI" 这个 XML 文档携带着某个表格中的信息: <table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> 此 XML 文档携带着有关一件家具的信息: <table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> 命名空间的实际应用 当开始使用 XSL 时,您不久就会看到实际使用中的命名空间。XSL 样式表用于将 XML 文档转换为其余格式,好比 HTML。 若是您仔细观察下面的这个 XSL 文档,就会看到大多数的标签是HTML标签。非 HTML 的标签都有前缀 xsl,并由此命名空间标示:"http://www.w3.org/1999/XSL/Transform": <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
命名空间实例:
#!usr/bin/env python # -*- coding:utf-8 -*- #******************************************************************************************* # <f:table xmlns:f="http://www.w3school.com.cn/furniture"> # <f:name>African Coffee Table</f:name> # <f:width hhh="123">80</f:width> # <f:length>120</f:length> # </f:table #******************************************************************************************* from xml.etree import ElementTree as ET ET.register_namespace('f','http://www.w3school.com.cn/furniture') # 建立节点 root = ET.Element('{http://www.w3school.com.cn/furniture}table') root_son1 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}name') root_son1.text = 'African Coffee Table' root_son2 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}width',attrib={'{http://www.w3school.com.cn/furniture}hh':'123'}) root_son2.text = '80' root_son3 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}lenth') root_son3.text = '120' root_str = ET.tostring(root) from xml.dom import minidom reparse = minidom.parseString(root_str) pretty = reparse.toprettyxml(indent='\t') with open('abc.xml','w',encoding='utf-8') as f: f.write(pretty)
XML接口处理实例:
11、configparser模块
configparser用于处理特定格式的文件,其本质上是利用open来操做文件
# 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值
1、获取全部节点 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.sections() print(ret) 2、获取指定节点下全部的键值对 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.items('section1') print(ret) 3、获取指定节点下全部的建 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.options('section1') print(ret) 4、获取指定节点下指定key的值 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') v = config.get('section1', 'k1') # v = config.getint('section1', 'k1') # v = config.getfloat('section1', 'k1') # v = config.getboolean('section1', 'k1') print(v) 5、检查、删除、添加节点 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') # 检查 has_sec = config.has_section('section1') print(has_sec) # 添加节点 config.add_section("SEC_1") config.write(open('xxxooo', 'w')) # 删除节点 config.remove_section("SEC_1") config.write(open('xxxooo', 'w')) 6、检查、删除、设置指定组内的键值对 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') # 检查 has_opt = config.has_option('section1', 'k1') print(has_opt) # 删除 config.remove_option('section1', 'k1') config.write(open('xxxooo', 'w')) # 设置 config.set('section1', 'k10', "123") config.write(open('xxxooo', 'w'))
高级的 文件、文件夹、压缩包 处理模块
shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另外一个文件中
1
2
3
|
import
shutil
shutil.copyfileobj(
open
(
'old.xml'
,
'r'
),
open
(
'new.xml'
,
'w'
))
|
shutil.copyfile(src, dst)
拷贝文件
1
|
shutil.copyfile(
'f1.log'
,
'f2.log'
)
|
shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
1
|
shutil.copymode(
'f1.log'
,
'f2.log'
)
|
shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
1
|
shutil.copystat(
'f1.log'
,
'f2.log'
)
|
shutil.copy(src, dst)
拷贝文件和权限
1
2
3
|
import
shutil
shutil.copy(
'f1.log'
,
'f2.log'
)
|
shutil.copy2(src, dst)
拷贝文件和状态信息
1
2
3
|
import
shutil
shutil.copy2(
'f1.log'
,
'f2.log'
)
|
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
1
2
3
|
import
shutil
shutil.copytree(
'folder1'
,
'folder2'
, ignore
=
shutil.ignore_patterns(
'*.pyc'
,
'tmp*'
))
|
import shutil shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
#symlinks=True 是拷贝快捷方式,仍是拷贝快捷方式对应的原文件
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
1
2
3
|
import
shutil
shutil.rmtree(
'folder1'
)
|
shutil.move(src, dst)
递归的去移动文件,它相似mv命令,其实就是重命名。
1
2
3
|
import
shutil
shutil.move(
'folder1'
,
'folder3'
)
|
shutil.make_archive(base_name, format,...)
建立压缩包并返回文件路径,例如:zip、tar
建立压缩包并返回文件路径,例如:zip、tar
1
2
3
4
5
6
7
8
|
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
import
shutil
ret
=
shutil.make_archive(
"wwwwwwwwww"
,
'gztar'
, root_dir
=
'/Users/wupeiqi/Downloads/test'
)
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import
shutil
ret
=
shutil.make_archive(
"/Users/wupeiqi/wwwwwwwwww"
,
'gztar'
, root_dir
=
'/Users/wupeiqi/Downloads/test'
)
|
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile # 压缩 z = zipfile.ZipFile('laxi.zip', 'w') z.write('a.log') z.write('data.data') z.close() # 解压 z = zipfile.ZipFile('laxi.zip', 'r') z.extractall() z.close()
import tarfile # 压缩 tar = tarfile.open('your.tar','w') tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log') tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log') tar.close() # 解压 tar = tarfile.open('your.tar','r') tar.extractall() # 可设置解压地址 tar.close()
十3、subprocess模块
执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。
call
执行命令,返回状态码
1
2
|
ret
=
subprocess.call([
"ls"
,
"-l"
], shell
=
False
)
ret
=
subprocess.call(
"ls -l"
, shell
=
True
)
|
check_call
执行命令,若是执行状态码是 0 ,则返回0,不然抛异常
1
2
|
subprocess.check_call([
"ls"
,
"-l"
])
subprocess.check_call(
"exit 1"
, shell
=
True
)
|
check_output
执行命令,若是状态码是 0 ,则返回执行结果,不然抛异常
1
2
|
subprocess.check_output([
"echo"
,
"Hello World!"
])
subprocess.check_output(
"exit 1"
, shell
=
True
)
|
subprocess.Popen(...)
用于执行复杂的系统命令
参数:
import subprocess ret1 = subprocess.Popen(["mkdir","t1"]) ret2 = subprocess.Popen("mkdir t2", shell=True)
终端输入的命令分为两种:
import subprocess obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) obj.stdin.write("print(1)\n") obj.stdin.write("print(2)") out_error_list = obj.communicate() print(out_error_list)
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) out_error_list = obj.communicate('print("hello")') print(out_error_list)
十4、logging模块
用于便捷记录日志且线程安全的模块,多人能够同时打开保存,系统会控制好队列
一、写单文件日志
#!usr/bin/env python # -*- coding:utf-8 -*- import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', level=10) #当设置level=10时,下面的级别>=10时才写入日志 logging.debug('debug') logging.info('info') logging.warning('warning') logging.error('erro') logging.critical('critical')
注:只有【当前写等级】大于【日志等级】时,日志文件才被记录。
定义handler的输出格式formatter。输出格式有不少。
format参数中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger建立以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(message)s用户输出的消息
二、多文件日志
对于上述记录日志的功能,只能将日志记录在单文件中,若是想要设置多个日志文件,logging.basicConfig将没法完成,须要自定义文件和日志操做对象。
#!usr/bin/env python # -*- coding:utf-8 -*- import logging #*************定义文件*********************************************************************************** #实例化一个FileHander对象,该对象是将格式化后的日志记录写入硬盘 file_1_1 = logging.FileHandler('11_1.log','a',encoding='utf-8') #实例化一个FileHander对象 #实例化一个Formatter对象,该对象是将日志记录转化为文本 fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_1_1.setFormatter(fmt) #将file_1_1的格式设置为fmt file_1_2 = logging.FileHandler('11_2.log','a',encoding='utf-8') fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_1_2.setFormatter(fmt) #********************************************************************************************************** #*************定义日志************************************************************************************* #实例化一个Logger对象,做用:初始化一个logging对象 logger1 = logging.Logger('s1',level=logging.ERROR) logger1.addHandler(file_1_1) #将规定的handler加入日志 logger1.addHandler(file_1_2) #********************************************************************************************************** #*************写入日志************************************************************************************* logger1.critical('123')
固然也能够用自定义的方法记录到一个日志文件中:
#!usr/bin/env python # -*- coding:utf-8 -*- import logging #定义文件 file_2_1 = logging.FileHandler('21-1.log','a',encoding='utf-8') fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_2_1.setFormatter(fmt) #定义日志 logger2 = logging.Logger('s2',level=10) logger2.addHandler(file_2_1) #写入日志 logger2.info('记录一个info错误')