目录python
1、软件开发目录规范mysql
2、模块git
3、包程序员
4、经常使用模块之——logging模块web
5、经常使用模块之——re模块正则表达式
6、经常使用模块之——json模块与pickle模块算法
7、经常使用模块之——hashlib模块sql
8、经常使用模块之——time模块与datatime模块shell
9、经常使用模块之——os模块与sys模块json
10、经常使用模块之——random模块与suprocess模块
11、其余模块——shutil模块、shelve模块、xml模块、configparser模块
1、软件开发目录规范(以ATM+购物车程序为例)
2、 模块
什么是模块(what):
模块是一系列功能的集合体。
常见的模块形式(自定义模块、第三方模块、内置模块)
①一个module.py文件就是一个模块,文件名是module.py,而模块名是module。
②一个含有__init__.py文件的文件夹也是模块。
③已被编译为共享库或DLL的C或者C++扩展。
④使用C语言编写并链接到Python解释器的内置模块。
为何要用模块(why):
①用第三方或内置的模块是一种拿来主义,能够极大的提高开发效率。
②自定义模块即将咱们本身程序中须要用到的公共的功能写入一个Python文件,而后程序的各部分组件能够经过导入的方式来引用/重用自定义模块中的功能。
如何使用模块(how):
导入的方式有两种:
#方式一 import 模块名 #方式二 from 模块名 import 具体功能
方式一:import 模块名
首次import m1导入模块都发生三件事:
一、先建立一个模块的名称空间
二、执行m1.py,将执行过程当中产生的名称都放入模块的名称空间中
三、在当前执行文件中拿到一个名字m1,该名字是指向模块的名称空间的
使用方法:指名道姓地访问m1名称空间中的名字func,优势是不会与当前名称空间中的名字冲突,缺点是每次访问都须要加上前缀m1.func
方式二:from 模块名import 具体功能
首次from m1 import func导入模块都发生三件事:
一、先建立一个模块的名称空间
二、执行m1.py,将执行过程当中产生的名称都放入模块的名称空间中
三、在当前执行文件中直接拿到一个功能名func,该名字是直接指向模块名称空间中的某一个功能的
使用方法:直接使用功能便可,优势是无需加任何前缀,缺点是容易与当前名称空间中的名字冲突
def func():
pass
func()
Tips:
在被当作执行文件执行时,__name__='__main__'
在被当作模块导入时,__name__='aaa'
模块的搜索路径:
一个py文件就是一个模块,在导入时,必须从某一个文件夹下找到该py文件。
模块的搜索路径值得是在导入模块时须要检索的文件夹们。
导入模块的查找顺序是:
①先从内存中已经导入的模块查找
②内置的模块
③环境变量中sys.path中找
强调:sys.path的第一个值是当前执行文件所在的文件夹
3、包
什么是包(what):
包是模块的一种形式,包的本质就是一个含有__init__.py的文件夹。
为何要有包(why):
方便管理模块调用。
如何使用包(how):
导入包就是在导入包下的__init__.py
import ...
from ... import ...
【注意点】
一、包内全部的文件都是被导入使用的,而不是被直接运行的
二、包内部模块之间的导入可使用绝对导入(以包的根目录为基准)与相对导入(以当前被导入的模块所在的目录为基准)
推荐使用相对导入
三、当文件是执行文件时,没法在该文件内用相对导入的语法
只有在文件时被看成模块导入时,该文件内才能使用相对导入的语法
四、凡是在导入时带点的,点的左边都必须是一个包
import aaa.bbb.m3.f3 # 错误
4、logging模块(日志模块)
1.日志级别
CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INFO = 20 DEBUG = 10 NOTSET = 0 #不设置
2.默认级别为warning,默认打印到终端
import logging logging.debug('调试debug') logging.info('消息info') logging.warning('警告warn') logging.error('错误error') logging.critical('严重critical') ''' WARNING:root:警告warn ERROR:root:错误error CRITICAL:root:严重critical '''
3.logging模块的Formatter,Handler,Logger,Filter对象
#logger:产生日志的对象 #Filter:过滤日志的对象 #Handler:接收日志而后控制打印到不一样的地方,FileHandler用来打印到文件中,StreamHandler用来打印到终端 #Formatter对象:能够定制不一样的日志格式对象,而后绑定给不一样的Handler对象使用,以此来控制不一样的Handler的日志格式
4.logging配置文件
""" logging配置 """ import os import logging.config # 定义三种日志输出格式 开始 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' # 定义日志输出格式 结束 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目录 logfile_name = 'all2.log' # log文件名 # 若是不存在定义的日志目录就建立一个 if not os.path.isdir(logfile_dir): os.mkdir(logfile_dir) # log文件的全路径 logfile_path = os.path.join(logfile_dir, logfile_name) # log配置字典 LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': simple_format }, }, 'filters': {}, 'handlers': { #打印到终端的日志 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', # 打印到屏幕 'formatter': 'simple' }, #打印到文件的日志,收集info及以上的日志 'default': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 'formatter': 'standard', 'filename': logfile_path, # 日志文件 'maxBytes': 1024*1024*5, # 日志大小 5M 'backupCount': 5, 'encoding': 'utf-8', # 日志文件的编码,不再用担忧中文log乱码了 }, }, 'loggers': { #logging.getLogger(__name__)拿到的logger配置 '': { 'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)传递 }, }, } def load_my_logging_cfg(): logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置 logger = logging.getLogger(__name__) # 生成一个log实例 logger.info('It works!') # 记录该文件的运行状态 if __name__ == '__main__': load_my_logging_cfg() logging配置文件
5、经常使用模块之——re模块
什么是正则表达式(what):
正则就是用一些具备特殊含义的符号组合到一块儿(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。
为何要用正则表达式(why):
用来描述一类事物的规则。
①经常使用匹配模式(元字符)
# =================================匹配模式================================= #一对一的匹配 # 'hello'.replace(old,new) # 'hello'.find('pattern') #正则匹配 import re #\w与\W print(re.findall('\w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] print(re.findall('\W','hello egon 123')) #[' ', ' '] #\s与\S print(re.findall('\s','hello egon 123')) #[' ', ' ', ' ', ' '] print(re.findall('\S','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] #\n \t都是空,均可以被\s匹配 print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' '] #\n与\t print(re.findall(r'\n','hello egon \n123')) #['\n'] print(re.findall(r'\t','hello egon\t123')) #['\n'] #\d与\D print(re.findall('\d','hello egon 123')) #['1', '2', '3'] print(re.findall('\D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' '] #\A与\Z print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^ print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$ #^与$ print(re.findall('^h','hello egon 123')) #['h'] print(re.findall('3$','hello egon 123')) #['3'] # 重复匹配:| . | * | ? | .* | .*? | + | {n,m} | #. print(re.findall('a.b','a1b')) #['a1b'] print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab'] print(re.findall('a.b','a\nb')) #[] print(re.findall('a.b','a\nb',re.S)) #['a\nb'] print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一条意思同样 #* print(re.findall('ab*','bbbbbbb')) #[] print(re.findall('ab*','a')) #['a'] print(re.findall('ab*','abbbb')) #['abbbb'] #? print(re.findall('ab?','a')) #['a'] print(re.findall('ab?','abbb')) #['ab'] #匹配全部包含小数在内的数字 print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3'] #.*默认为贪婪匹配 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] #.*?为非贪婪匹配:推荐使用 print(re.findall('a.*?b','a1b22222222b')) #['a1b'] #+ print(re.findall('ab+','a')) #[] print(re.findall('ab+','abbb')) #['abbb'] #{n,m} print(re.findall('ab{2}','abbb')) #['abb'] print(re.findall('ab{2,4}','abbb')) #['abb'] print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+' print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*' #[] print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且若是-没有被转意的话,应该放到[]的开头或结尾 print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^表明的意思是取反,因此结果为['a=b'] print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^表明的意思是取反,因此结果为['a=b'] print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^表明的意思是取反,因此结果为['a=b'] print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^表明的意思是取反,因此结果为['a=b'] #\# print(re.findall('a\\c','a\c')) #对于正则来讲a\\c确实能够匹配到a\c,可是在python解释器读取a\\c时,会发生转义,而后交给re去执行,因此抛出异常 print(re.findall(r'a\\c','a\c')) #r表明告诉解释器使用rawstring,即原生字符串,把咱们正则内的全部符号都当普通字符处理,不要转义 print(re.findall('a\\\\c','a\c')) #同上面的意思同样,和上面的结果同样都是['a\\c'] #():分组 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的所有内容,而是组内的内容,?:可让结果为匹配的所有内容 print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com'] print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"'] #| print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
②search、match与split
# ===========================re模块提供的方法介绍=========================== import re #1 print(re.findall('e','alex make love') ) #['e', 'e', 'e'],返回全部知足匹配条件的结果,放在列表里 #2 print(re.search('e','alex make love').group()) #e,只到找到第一个匹配而后返回一个包含匹配信息的对象,该对象能够经过调用group()方法获得匹配的字符串,若是字符串没有匹配,则返回None。 #3 print(re.match('e','alex make love')) #None,同search,不过在字符串开始处进行匹配,彻底能够用search+^代替match #4 print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割获得''和'bcd',再对''和'bcd'分别按'b'分割 #5 print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换全部 print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make love')) #===> love make alex print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数 #6 obj=re.compile('\d{2}') print(obj.search('abc123eeee').group()) #12 print(obj.findall('abc123eeee')) #['12'],重用了obj
6、经常使用模块之——json模块与pickle模块
什么是序列化与反序列化(what):
序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输。
为何要有序列化(why):
①能够保存程序运行状态
②能够数据跨平台交互
如何使用序列化(how):
import json
优势:跨平台性强
缺点:只能支持/对应python部分数据类型
import pickle
优势:能够支持/对应全部python的数据类型
缺点:只能被python识别
JSON表示的对象就是标准的JavaScript语言的对象,JSON和Python内置的数据类型对应以下:
【json】
工做原理:
import json dic={'name':'alvin','age':23,'sex':'male'} print(type(dic))#<class 'dict'> j=json.dumps(dic) print(type(j))#<class 'str'> f=open('序列化对象','w') f.write(j) #-------------------等价于json.dump(dic,f) f.close() #-----------------------------反序列化<br> import json f=open('序列化对象') data=json.loads(f.read())# 等价于data=json.load(f)
import json #dct="{'1':111}"#json 不认单引号 #dct=str({"1":111})#报错,由于生成的数据仍是单引号:{'one': 1} dct='{"1":"111"}' print(json.loads(dct)) #conclusion: # 不管数据是怎样建立的,只要知足json格式,就能够json.loads出来,不必定非要dumps的数据才能loads 注意点
【pickle】
工做原理:
import pickle dic={'name':'alvin','age':23,'sex':'male'} print(type(dic))#<class 'dict'> j=pickle.dumps(dic) print(type(j))#<class 'bytes'> f=open('序列化对象_pickle','wb')#注意是w是写入str,wb是写入bytes,j是'bytes' f.write(j) #-------------------等价于pickle.dump(dic,f) f.close() #-------------------------反序列化 import pickle f=open('序列化对象_pickle','rb') data=pickle.loads(f.read())# 等价于data=pickle.load(f) print(data['age'])
7、经常使用模块之——hashlib模块
什么是hash(what):
hash是一种算法,该算法接收传入的内容,通过运算获得一串hash值。
为何要用hash算法(why):
hash值有三大特性:
①只要传入的内容同样,获得的hash值必然同样。
②只要hash算法固定,不管传入的内容有多大,获得的hash值长度是固定的。
③不能够用hash值逆推出原来的内容。
基于①和②在下载文件时能够作文件一致性检测。
基于①和③能够对密码进行加密。
如何用hashlib模块(how):
import hashlib #一、造出hash工厂 m=hashlib.md5() #二、运送原材料 m.update('你好啊美丽的'.encode('utf-8')) m.update('张铭言'.encode('utf-8')) #三、产出hash值 print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9 #一、造出hash工厂 m=hashlib.md5('你'.encode('utf-8')) #二、运送原材料 m.update('好啊美丽的张铭言'.encode('utf-8')) #三、产出hash值 print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9 应用一:文件一致性校验 #一、造出hash工厂 m=hashlib.sha512('你'.encode('utf-8')) #二、运送原材料 m.update('好啊美sadfsadf丽asdfsafdasdasdfsafsdafasdfasdfsadfsadfsadfsadfasdff的张铭言'.encode('utf-8')) #三、产出hash值 print(m.hexdigest()) #2ff39b418bfc084c8f9a237d11b9da6d5c6c0fb6bebcde2ba43a433dc823966c #一、造出hash工厂 m=hashlib.md5() #二、运送原材料 with open(r'E:\01.mp4','rb') as f: for line in f: m.update(line) #三、产出hash值 print(m.hexdigest()) #1273d366d2fe2dc57645cc1031414a05 # 1273d366d2fe2dc57645cc1031414a05 应用一:对明文密码进行加密 password=input('>>>: ') m=hashlib.md5() m.update('天王盖地虎'.encode('utf-8')) m.update(password.encode('utf-8')) print(m.hexdigest()) #95bd6eafefdf51d8b153785f3fb6263d import hmac m=hmac.new('小鸡炖蘑菇'.encode('utf-8')) m.update('hello'.encode('utf-8')) print(m.hexdigest())
8、经常使用模块之——time模块与datatime模块
在Python中,一般有这几种方式来表示时间:
import time #--------------------------咱们先以当前时间为准,让你们快速认识三种形式的时间 print(time.time()) # 时间戳:1487130156.419527 print(time.strftime("%Y-%m-%d %X")) #格式化的时间字符串:'2017-02-15 11:40:53' print(time.localtime()) #本地时区的struct_time print(time.gmtime()) #UTC时区的struct_time
其中计算机认识的时间只能是'时间戳'格式,而程序员可处理的或者说人类能看懂的时间有: '格式化的时间字符串','结构化的时间' ,因而有了下图的转换关系。
#--------------------------按图1转换时间 # localtime([secs]) # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 time.localtime() time.localtime(1473525444.037215) # gmtime([secs]) 和localtime()方法相似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。 # mktime(t) : 将一个struct_time转化为时间戳。 print(time.mktime(time.localtime()))#1473525749.0 # strftime(format[, t]) : 把一个表明时间的元组或者struct_time(如由time.localtime()和 # time.gmtime()返回)转化为格式化的时间字符串。若是t未指定,将传入time.localtime()。若是元组中任何一个 # 元素越界,ValueError的错误将会被抛出。 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56 # time.strptime(string[, format]) # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操做。 print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X')) #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, # tm_wday=3, tm_yday=125, tm_isdst=-1) #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。
#--------------------------按图2转换时间 # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。 # 若是没有参数,将会将time.localtime()做为参数传入。 print(time.asctime())#Sun Sep 11 00:43:43 2016 # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。若是参数未给或者为 # None的时候,将会默认time.time()为参数。它的做用至关于time.asctime(time.localtime(secs))。 print(time.ctime()) # Sun Sep 11 00:46:38 2016 print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016
【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分 # # c_time = datetime.datetime.now() # print(c_time.replace(minute=3,hour=2)) #时间替换
9、经常使用模块之——os模块与sys模块
os.getcwd() 获取当前工做目录,即当前python脚本工做的目录路径 os.chdir("dirname") 改变当前脚本工做目录;至关于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir('dirname') 生成单级目录;至关于shell中mkdir dirname os.rmdir('dirname') 删除单级空目录,若目录不为空则没法删除,报错;至关于shell中rmdir dirname os.listdir('dirname') 列出指定目录下的全部文件和子目录,包括隐藏文件,并以列表方式打印 os.remove() 删除一个文件 os.rename("oldname","newname") 重命名文件/目录 os.stat('path/filename') 获取文件/目录信息 os.sep 输出操做系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为: os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix' os.system("bash command") 运行shell命令,直接显示 os.environ 获取系统环境变量 os.path.abspath(path) 返回path规范化的绝对路径 os.path.split(path) 将path分割成目录和文件名二元组返回 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False os.path.isabs(path) 若是path是绝对路径,返回True os.path.isfile(path) 若是path是一个存在的文件,返回True。不然返回False os.path.isdir(path) 若是path是一个存在的目录,则返回True。不然返回False os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径以前的参数将被忽略 os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间 os.path.getsize(path) 返回path的大小
【路径处理】
os路径处理 #方式一: import os #具体应用 import os,sys possible_topdir = os.path.normpath(os.path.join( os.path.abspath(__file__), os.pardir, #上一级 os.pardir, os.pardir )) sys.path.insert(0,possible_topdir) #方式二: os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10、经常使用模块之——random模块与suprocess模块
【random】
【生成随机验证码】
import random def make_random_code(n): res = '' for i in range(n): alpha = chr(random.randint(65,90)) digital = str(random.randint(0,9)) res += random.choice([alpha,digital]) return res print(make_random_code(6))
【suprocess】
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解码
【打印进度条】
import time def make_process(percent,width=50): if percent>1:percent=1 show_str = ('[%%-%ds]' %width) %(int(percent*width)*'#') print('\r%s %s%%' %(show_str,int(percent*100)),end='') total = 102400 down = 0 while down<total: time.sleep(0.2) down+=1024 percent = down/total make_process(percent)
11、其余模块——shutil模块、shelve模块、xml模块、configparser模块
【shutil模块】
高级的 文件、文件夹、压缩包 处理模块
shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另外一个文件中
1 import shutil 2 3 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 import shutil 2 3 shutil.copy('f1.log', 'f2.log')
shutil.copy2(src, dst)
拷贝文件和状态信息
1 import shutil 2 3 shutil.copy2('f1.log', 'f2.log')
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
1 import shutil 2 3 shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
1 import shutil 2 3 shutil.rmtree('folder1')
shutil.move(src, dst)
递归的去移动文件,它相似mv命令,其实就是重命名。
1 import shutil 2 3 shutil.move('folder1', 'folder3')
shutil.make_archive(base_name, format,...)
建立压缩包并返回文件路径,例如:zip、tar
建立压缩包并返回文件路径,例如:zip、tar
1 #将 /data 下的文件打包放置当前程序目录 2 import shutil 3 ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') 4 5 6 #将 /data下的文件打包放置 /tmp/目录 7 import shutil 8 ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
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(path='.') z.close() zipfile压缩解压缩
【shelve模块】
shelve模块比pickle模块简单,只有一个open函数,返回相似字典的对象,可读可写;key必须为字符串,而值能够是python所支持的数据类型。
import shelve f=shelve.open(r'sheve.txt') # f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']} # f['stu2_info']={'name':'gangdan','age':53} # f['school_info']={'website':'http://www.pypy.org','city':'beijing'} print(f['stu1_info']['hobby']) f.close()
【xml模块】
xml是实现不一样语言或程序之间进行数据交换的协议,跟json差很少,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,你们只能选择用xml呀,至今不少传统公司如金融行业的不少系统的接口还主要是xml。
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
#在country内添加(append)节点year2 import xml.etree.ElementTree as ET tree = ET.parse("a.xml") root=tree.getroot() for country in root.findall('country'): for year in country.findall('year'): if int(year.text) > 2000: year2=ET.Element('year2') year2.text='新年' year2.attrib={'update':'yes'} country.append(year2) #往country节点下添加子节点 tree.write('a.xml.swap')
【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',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','')) #添加一个标题 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'))