模块
常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀。
1 使用python编写的代码(.py文件)
3 包好一组模块的包
4 使用C编写并链接到python解释器的内置模块
模块的使用
1.import
2.from ... improt ...
模块导入查找顺序:内存->内置->sys.path (sys.path是执行文件的路径,sys.path是python的搜索模块的路径集,是一个list)
import sys import os print(sys.path) res=os.path.dirname(os.path.dirname(__file__)) sys.path.insert(0,res) #添加文件的搜索路径 print(sys.path) '''['E:\\study\\code\\day5\\模块\\模块的搜索路径', 'E:\\study\\code', 'D:\\python36\\python36.zip', 'D:\\python36\\DLLs', 'D:\\python36\\lib', 'D:\\python36', 'D:\\python36\\lib\\site-packages'] ['E:/study/code/day5/模块', 'E:\\study\\code\\day5\\模块\\模块的搜索路径', 'E:\\study\\code', 'D:\\python36\\python36.zip', 'D:\\python36\\DLLs', 'D:\\python36\\lib', 'D:\\python36', 'D:\\python36\\lib\\site-packages'] '''
sys.modules 和 限制from import导入变量的方法
#spam.py print('from the spam.py') __all__=['money','x'] #对from spam import * 有用,可以导入__all__中的变量 # _money=1000 #对from spam import * 有用,_开头的变量不可以被导入 money=0 x=1 def read1(): print('spam->read1->money',money) def read2(): print('spam->read2 calling read') read1() def change(): global money money=10 #run.py import sys print('spam' in sys.modules) # sys.modules存放的是已经加载到内的模块,sys.module是一个字典,内部包含模块名与模块对象的映射,该字典决定了导入模块时是否需要重新导入 import spam #模块只在第一次导入时才会执行,之后的导入都是直接引用内存已经存在的结果 print('spam' in sys.modules) import spam
'''
False
from the spam.py
True
'''
python编译文件
为了提高加载模块的速度,强调强调强调:提高的是加载速度而绝非运行速度。python解释器会在__pycache__目录中下缓存每个模块编译后的版本,格式为:module.version.pyc。通常会包含python的版本号。例如,在CPython3.3版本下,spam.py模块会被缓存成__pycache__/spam.cpython-33.pyc。这种命名规范保证了编译后的结果多版本共存。
Python检查源文件的修改时间与编译的版本进行对比,如果过期就需要重新编译。这是完全自动的过程。并且编译的模块是平台独立的,所以相同的库可以在不同的架构的系统之间共享,即pyc使一种跨平台的字节码,类似于JAVA火.NET,是由python虚拟机来执行的,但是pyc的内容跟python的版本相关,不同的版本编译后的pyc文件不同,2.5编译的pyc文件不能到3.5上执行,并且pyc文件是可以反编译的,因而它的出现仅仅是用来提升模块的加载速度的。
python文件的两种用途
1.文件当做脚本运行时__name__等于__main__
2.文件当做模块被加载运行时__name__等于模块名
#m1.py def func1(): print('from m1') print('file name is:%s' % __name__) #__name__ if __name__ == '__main__': #当做脚本使用 func1() ''' file name is:__main__ from m1 ''' #run.py import m1 m1.func1() '''file name is:m1 from m1'''
常用模块
1.logging
1 """ 2 logging配置 3 """ 4 5 import os 6 import logging.config 7 8 # 定义三种日志输出格式 开始 9 10 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ 11 '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字 12 13 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 14 15 id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' 16 17 # 定义日志输出格式 结束 18 19 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目录 20 21 logfile_name = 'all2.log' # log文件名 22 23 # 如果不存在定义的日志目录就创建一个 24 if not os.path.isdir(logfile_dir): 25 os.mkdir(logfile_dir) 26 27 # log文件的全路径 28 logfile_path = os.path.join(logfile_dir, logfile_name) 29 30 # log配置字典 31 LOGGING_DIC = { 32 'version': 1, 33 'disable_existing_loggers': False, 34 'formatters': { 35 'standard': { 36 'format': standard_format 37 }, 38 'simple': { 39 'format': simple_format 40 }, 41 'id_simple': { 42 'format': id_simple_format 43 }, 44 }, 45 'filters': {}, 46 'handlers': { 47 #打印到终端的日志 48 'console': { 49 'level': 'DEBUG', 50 'class': 'logging.StreamHandler', # 打印到屏幕 51 'formatter': 'simple' 52 }, 53 #打印到文件的日志,收集info及以上的日志 54 'default': { 55 'level': 'DEBUG', 56 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 57 'formatter': 'standard', 58 'filename': logfile_path, # 日志文件 59 'maxBytes': 1024*1024*5, # 日志大小 5M 60 'backupCount': 5, 61 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 62 }, 63 'boss': { 64 'level': 'DEBUG', 65 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 66 'formatter': 'standard', 67 'filename': 'boss.log', # 日志文件 68 'maxBytes': 1024 * 1024 * 5, # 日志大小 5M 69 'backupCount': 5, 70 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 71 }, 72 }, 73 'loggers': { 74 #logger1=logging.getLogger(__name__)拿到的logger配置 75 '': { 76 'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 77 'level': 'DEBUG', 78 'propagate': True, # 向上(更高level的logger)传递 79 }, 80 #logger1=logging.getLogger('collect')拿到的logger配置 81 'collect': { 82 'handlers': ['boss',], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 83 'level': 'DEBUG', 84 'propagate': True, # 向上(更高level的logger)传递 85 }, 86 }, 87 } 88 89 90 def load_my_logging_cfg(): 91 logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置 92 # logger = logging.getLogger(__name__) # 生成一个log实例 93 # logger.info('It works!') # 记录该文件的运行状态 94 95 if __name__ == '__main__': 96 load_my_logging_cfg()
2.re模块
1 # =================================匹配模式================================= 2 #一对一的匹配 3 # 'hello'.replace(old,new) 4 # 'hello'.find('pattern') 5 6 #正则匹配 7 import re 8 #\w与\W 9 print(re.findall('\w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 10 print(re.findall('\W','hello egon 123')) #[' ', ' '] 11 12 #\s与\S 13 print(re.findall('\s','hello egon 123')) #[' ', ' ', ' ', ' '] 14 print(re.findall('\S','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 15 16 #\n \t都是空,都可以被\s匹配 17 print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' '] 18 19 #\n与\t 20 print(re.findall(r'\n','hello egon \n123')) #['\n'] 21 print(re.findall(r'\t','hello egon\t123')) #['\n'] 22 23 #\d与\D 24 print(re.findall('\d','hello egon 123')) #['1', '2', '3'] 25 print(re.findall('\D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' '] 26 27 #\A与\Z 28 print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^ 29 print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$ 30 31 #^与$ 32 print(re.findall('^h','hello egon 123')) #['h'] 33 print(re.findall('3$','hello egon 123')) #['3'] 34 35 # 重复匹配:| . | * | ? | .* | .*? | + | {n,m} | 36 #. 37 print(re.findall('a.b','a1b')) #['a1b'] 38 print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab'] 39 print(re.findall('a.b','a\nb')) #[] 40 print(re.findall('a.b','a\nb',re.S)) #['a\nb'] 41 print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一条意思一样 42 43 #* 44 print(re.findall('ab*','bbbbbbb')) #[] 45 print(re.findall('ab*','a')) #['a'] 46 print(re.findall('ab*','abbbb')) #['abbbb'] 47 48 #? 49 print(re.findall('ab?','a')) #['a'] 50 print(re.findall('ab?','abbb')) #['ab'] 51 #匹配所有包含小数在内的数字 52 print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3'] 53 54 #.*默认为贪婪匹配 55 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] 56 57 #.*?为非贪婪匹配:推荐使用 58 print(re.findall('a.*?b','a1b22222222b')) #['a1b'] 59 60 #+ 61 print(re.findall('ab+','a')) #[] 62 print(re.findall('ab+','abbb')) #['abbb'] 63 64 #{n,m} 65 print(re.findall('ab{2}','abbb')) #['abb'] 66 print(re.findall('ab{2,4}','abbb')) #['abb'] 67 print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+' 68 print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*' 69 70 #[] 71 print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾 72 print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 73 print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 74 print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 75 print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 76 77 #\# print(re.findall('a\\c','a\c')) #对于正则来说a\\c确实可以匹配到a\c,但是在python解释器读取a\\c时,会发生转义,然后交给re去执行,所以抛出异常 78 print(re.findall(r'a\\c','a\c')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义 79 print(re.findall('a\\\\c','a\c')) #同上面的意思一样,和上面的结果一样都是['a\\c'] 80 81 #():分组 82 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] 83 print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab 84 print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容 85 86 #| 87 print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
re常用方法
1 # ===========================re模块提供的方法介绍=========================== 2 import re 3 #findall 4 s = "zyltest zyl1 mynamezyl lingling" 5 print(re.findall('zyl',s)) #['zyl', 'zyl', 'zyl'],返回所有满足匹配条件的结果,放到列表中;没有匹配时,返回空列表 6 7 #2 search 8 print(re.search('zyl',s)) #<_sre.SRE_Match object; span=(1, 4), match='zyl'> 9 print(re.search('zyl',s).group()) #zyl 只找到第一个匹配的然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串。如果字符串没有匹配,则返回None 10 print(re.search('ling',s).group()) #ling 11 12 #3 matche 13 print(re.match('zyl',s)) #<_sre.SRE_Match object; span=(0, 3), match='zyl'> 14 print(re.match('ling',s)) #None 同search,不同点是match是从字符串开始处进行匹配,完全可以用search+^代替match 15 16 #4 split 17 print(re.split('zyl',s)) #['', 'test ', '1 myname', ' lingling'] 按照'zyl'分割,输出列表 18 print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割 19 20 #5 sub 21 print(re.sub('zyl','zhaoyanling',s)) #zhaoyanlingtest zhaoyanling1 mynamezhaoyanling lingling,不指定N,默认替换所有 22 print(re.sub('zyl','zhaoyanling',s,1)) #zhaoyanlingtest zyl1 mynamezyl lingling 替换n指定的个数 23 24 print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make love')) #===> love make alex 25 26 print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数 27 28 29 #6 compile 可以复用需要查找对象 30 obj = re.compile('zyl') 31 print(obj,type(obj)) #re.compile('zyl') <class '_sre.SRE_Pattern'> 32 print(obj.search(s).group()) 33 print(obj.findall(s)) 34 35 obj1=re.compile('\d{2}') 36 print(obj1.search('abc123eeee').group()) #12 37 print(obj1.findall('abc123eeee')) #['12'],重用了obj
补充一
1 import re 2 print(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>")) #['h1'] 3 print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").group()) #<h1>hello</h1> 4 print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").groupdict()) #<h1>hello</h1> 5 6 print(re.search(r"<(\w+)>\w+</(\w+)>","<h1>hello</h1>").group()) 7 print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>").group()) 8 9 '''['h1'] 10 <h1>hello</h1> 11 {'tag_name': 'h1'} 12 <h1>hello</h1> 13 <h1>hello</h1>''' 14 15 # | 先匹配左边的,如果左边满足就不会再匹配右边 16 print(re.findall(r'-?\d+\.?\d*',"1-12*(60+(-40.35/5)-(-4*3))")) #找出所有数字['1', '-12', '60', '-40.35', '5', '-4', '3'] 17 #使用|,先匹配的先生效,|左边是匹配小数,而findall最终结果是查看分组,所有即使匹配成功小数也不会存入结果 18 #而不是小数时,就去匹配(-?\d+),匹配到的自然就是,非小数的数,在此处即整数 19 print(re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")) #找出所有整数['1', '-2', '60', '', '5', '-4', '3'] 20 print(re.findall(r"-?\d+","1-2*(60+(-40.35/5)-(-4*3))")) #['1', '-2', '60', '-40', '35', '5', '-4', '3'] 21 22 #search与findall 23 #为何同样的表达式search与findall却有不同结果: 24 print(re.search('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))").group()) #(-40.35/5) 25 print(re.findall('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))")) #['/5', '*3'] 26 27 #看这个例子:(\d)+相当于(\d)(\d)(\d)(\d)...,是一系列分组 28 print(re.search('(\d)+','123').group()) #group的作用是将所有组拼接到一起显示出来 29 print(re.findall('(\d)+','123')) #findall结果是组内的结果,且是最后一个组的结果
补充二
1 #_*_coding:utf-8_*_ 2 #在线调试工具:tool.oschina.net/regex/# 3 import re 4 5 s=''' 6 http://www.baidu.com 7 egon@oldboyedu.com 8 你好 9 010-3141 10 ''' 11 12 #最常规匹配 13 # content='Hello 123 456 World_This is a Regex Demo' 14 # res=re.match('Hello\s\d\d\d\s\d{3}\s\w{10}.*Demo',content) 15 # print(res) 16 # print(res.group()) 17 # print(res.span()) 18 19 #泛匹配 20 # content='Hello 123 456 World_This is a Regex Demo' 21 # res=re.match('^Hello.*Demo',content) 22 # print(res.group()) 23 24 25 #匹配目标,获得指定数据 26 27 # content='Hello 123 456 World_This is a Regex Demo' 28 # res=re.match('^Hello\s(\d+)\s(\d+)\s.*Demo',content) 29 # print(res.group()) #取所有匹配的内容 30 # print(res.group(1)) #取匹配的第一个括号内的内容 31 # print(res.group(2)) #去陪陪的第二个括号内的内容 32 33 34 35 #贪婪匹配:.*代表匹配尽可能多的字符 36 # import re 37 # content='Hello 123 456 World_This is a Regex Demo' 38 # 39 # res=re.match('^He.*(\d+).*Demo$',content) 40 # print(res.group(1)) #只打印6,因为.*会尽可能多的匹配,然后后面跟至少一个数字 41 42 43 #非贪婪匹配:?匹配尽可能少的字符 44 # import re 45 # content='Hello 123 456 World_This is a Regex Demo' 46 # 47 # res=re.match('^He.*?(\d+).*Demo$',content) 48 # print(res.group(1)) #只打印6,因为.*会尽可能多的匹配,然后后面跟至少一个数字 49 50 51 #匹配模式:.不能匹配换行符 52 content='''Hello 123456 World_This 53 is a Regex Demo 54 ''' 55 # res=re.match('He.*?(\d+).*?Demo$',content) 56 # print(res) #输出None 57 58 # res=re.match('He.*?(\d+).*?Demo$',content,re.S) #re.S让.可以匹配换行符 59 # print(res) 60 # print(res.group(1)) 61 62 63 #转义:\ 64 65 # content='price is $5.00' 66 # res=re.match('price is $5.00',content) 67 # print(res) 68 # 69 # res=re.match('price is \$5\.00',content) 70 # print(res) 71 72 73 #总结:尽量精简,详细的如下 74 # 尽量使用泛匹配模式.* 75 # 尽量使用非贪婪模式:.*? 76 # 使用括号得到匹配目标:用group(n)去取得结果 77 # 有换行符就用re.S:修改模式 78 79 80 81 #re.search:会扫描整个字符串,不会从头开始,找到第一个匹配的结果就会返回 82 83 # import re 84 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 85 # 86 # res=re.match('Hello.*?(\d+).*?Demo',content) 87 # print(res) #输出结果为None 88 89 # 90 # import re 91 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 92 # 93 # res=re.search('Hello.*?(\d+).*?Demo',content) # 94 # print(res.group(1)) #输出结果为 95 96 97 98 #re.search:只要一个结果,匹配演练, 99 import re 100 content=''' 101 <tbody> 102 <tr id="4766303201494371851675" class="even "><td><div class="hd"><span class="num">1</span><div class="rk "><span class="u-icn u-icn-75"></span></div></div></td><td class="rank"><div class="f-cb"><div class="tt"><a href="/song?id=476630320"><img class="rpic" src="http://p1.music.126.net/Wl7T1LBRhZFg0O26nnR2iQ==/19217264230385030.jpg?param=50y50&quality=100"></a><span data-res-id="476630320" " 103 # res=re.search('<a\shref=.*?<b\stitle="(.*?)".*?b>',content) 104 # print(res.group(1)) 105 106 107 #re.findall:找到符合条件的所有结果 108 # res=re.findall('<a\shref=.*?<b\stitle="(.*?)".*?b>',content) 109 # for i in res: 110 # print(i) 111 112 113 114 #re.sub:字符串替换 115 import re 116 content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 117 118 # content=re.sub('\d+','',content) 119 # print(content) 120 121 122 #用\1取得第一个括号的内容 123 #用法:将123与456换位置 124 # import re 125 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 126 # 127 # # content=re.sub('(Extra.*?)(\d+)(\s)(\d+)(.*?strings)',r'\1\4\3\2\5',content) 128 # content=re.sub('(\d+)(\s)(\d+)',r'\3\2\1',content) 129 # print(content) 130 131 132 133 134 # import re 135 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 136 # 137 # res=re.search('Extra.*?(\d+).*strings',content) 138 # print(res.group(1)) 139 140 141 # import requests,re 142 # respone=requests.get('https://book.douban.com/').text 143 144 # print(respone) 145 # print('======'*1000) 146 # print('======'*1000) 147 # print('======'*1000) 148 # print('======'*1000) 149 # res=re.findall('<li.*?cover.*?href="(.*?)".*?title="(.*?)">.*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span.*?</li>',respone,re.S) 150 # # res=re.findall('<li.*?cover.*?href="(.*?)".*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span>.*?</li>',respone,re.S) 151 # 152 # 153 # for i in res: 154 # print('%s %s %s %s' %(i[0].strip(),i[1].strip(),i[2].strip(),i[3].strip()))
3.os模块
os是与操作系统交互的接口
1 import os 2 print(os.getcwd()) #获取当前工作目录,即当前python脚本工作的目录路径 3 print(os.chdir('/Users/yanlingzhao/Documents/python')) #改变当前脚本工作目录;相当于shell下cd 4 print(os.getcwd()) 5 '''/Users/yanlingzhao/Documents/python/code/code/6 6 None 7 /Users/yanlingzhao/Documents/python''' 8 9 # os.pardir 获取当前目录的父目录字符串名:('..') 10 # os.makedirs('dirname1/dirname2') 可生成多层递归目录 11 # os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 12 # os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname 13 # os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname 14 # os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 15 # os.remove() 删除一个文件 16 # os.rename("oldname","newname") 重命名文件/目录 17 # os.stat('path/filename') 获取文件/目录信息 18 # os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/" 19 # os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" 20 # os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为: 21 # os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix' 22 # os.system("bash command") 运行shell命令,直接显示 23 # os.environ 获取系统环境变量 24 25 print(os.path.abspath(__file__)) 26 print(os.path.split(__file__)) 27 print(os.path.dirname(__file__)) 28 print(os.path.basename(__file__)) 29 print(os.path.exists(__file__)) 30 print(os.path.isabs(__file__)) 31 print(os.path.isfile(__file__)) 32 print(os.path.isdir) 33 print(os.path.join(os.path.dirname(__file__),os.path.basename(__file__))) 34 print(os.path.getatime(__file__)) 35 print(os.path.getctime(__file__)) 36 print(os.path.getsize(__file__)) 37 '''/Users/yanlingzhao/Documents/python/code/code/6/osmodule.py 38 ('/Users/yanlingzhao/Documents/python/code/code/6', 'osmodule.py') 39 /Users/yanlingzhao/Documents/python/code/code/6 40 osmodule.py 41 True 42 True 43 True 44 <function isdir at 0x1005d9bf8> 45 /Users/yanlingzhao/Documents/python/code/code/6/osmodule.py 46 1502596131.0 47 1502596131.0 48 3169''' 49 # os.path.abspath(path) 返回path规范化的绝对路径 50 # os.path.split(path) 将path分割成目录和文件名二元组返回 51 # os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 52 # os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 53 # os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False 54 # os.path.isabs(path) 如果path是绝对路径,返回True 55 # os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False 56 # os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False 57 # os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 58 # os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间 59 # os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间 60 # os.path.getsize(path) 返回path的大小
补充
在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠。 >>> os.path.normcase('c:/windows\\system32\\') 'c:\\windows\\system32\\' 规范化路径,如..和/ >>> os.path.normpath('c://windows\\System32\\../Temp/') 'c:\\windows\\Temp' >>> a='/Users/jieli/test1/\\\a1/\\\\aa.py/../..' >>> print(os.path.normpath(a)) /Users/jieli/test1 在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠。 >>> os.path.normcase('c:/windows\\system32\\') 'c:\\windows\\system32\\' 规范化路径,如..和/ >>> os.path.normpath('c://windows\\System32\\../Temp/') 'c:\\windows\\Temp' >>> a='/Users/jieli/test1/\\\a1/\\\\aa.py/../..' >>> print(os.path.normpath(a)) /Users/jieli/test1
4.sys模块
1 sys.argv 命令行参数List,第一个元素是程序本身路径 2 sys.exit(n) 退出程序,正常退出时exit(0) 3 sys.version 获取Python解释程序的版本信息 4 sys.maxint 最大的Int值 5 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 6 sys.platform 返回操作系统平台名称 7 sys.stdout.write('please:') 8 val = sys.stdin.readline()[:-1]
1 import sys,time 2 3 print(sys.argv) #运行文件时,输入参数以列表格式输出;第一个是文件名 4 '''python sysmodule.py 1 5 ['sysmodule.py', '1'] 6 ''' 7 8 print(sys.modules) #查看运行文件已经加载模块 9 print(sys.path) #模块索索路径,输出的是列表格式;可以通过append或insert添加所需搜索路径 10 print(sys.platform) 11 print(sys.version) 12 13 #在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n') 14 #以下两行是等价的 15 print('test') 16 sys.stdout.write('test'+'\n') 17 18 for i in range(1,100): 19 sys.stdout.write('\r%s' %('#'*i)) #'\r%s' \r可以是下次还从头打印 20 sys.stdout.flush() #实时刷新数据到终端 21 time.sleep(0.5) 22 23 print(sys.stdin) 24 in1 = input('test:') 25 26 print('test:') 27 in2 = sys.stdin.readline()[:-1] 28 29 print(in1) 30 print(in2) 31 '''<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> 32 test:123 33 test: 34 456 35 123 36 456'''
模拟进度条
1 import sys 2 import time 3 def progress(percent,width=50): 4 if percent >= 100: 5 print('\r[%s] %d%%' %('#'*width,100)) #print的end默认为回车 6 return 7 8 show_str=('[%%-%ds]' %width) %(int(width * percent / 100) * "#") #字符串拼接的嵌套使用 9 print("\r%s %d%%" %(show_str, percent),end='',file=sys.stdout,flush=True) 10 11 #=========应用========== 12 data_size=100 13 recv_size=0 14 while recv_size < data_size: 15 time.sleep(1) #模拟数据的传输延迟 16 recv_size+=10 #每次收10 17 18 recv_per=int(100*(recv_size/data_size)) #接收的比例 19 progress(recv_per,width=30) #进度条的宽度30
5.json和pickle模块
什么是序列化:把对象(变量)从内存中变成可存储或传输的过程成为序列化。
序列化的目的:持久存储状态;跨平台数据交换
json.dumps. json.loads. json.dump. json.load
1 import json 2 3 dic = {'a': 1, 'b': 2, 'c': 3} 4 5 print(type(dic)) 6 with open('a.txt','w') as f: 7 f.write(str(dic)) 8 9 ''' 10 <class 'dict'> 11 a.txt: 12 {'a': 1, 'b': 2, 'c': 3} 13 ''' 14 15 json_dic = json.dumps(dic) 16 print(type(json_dic)) 17 with open('b.txt','w') as f: 18 f.write(json_dic) 19 ''' 20 <class 'str'> 21 b.txt: 22 {"a": 1, "b": 2, "c": 3} 23 ''' 24 25 with open('a.txt','r') as f: 26 res = f.read() 27 print(res,type(res)) 28 ''' 29 {'a': 1, 'b': 2, 'c': 3} 30 <class 'str'> 31 ''' 32 33 with open('b.txt','r') as f: 34 res = f.read() 35 print(res,type(res)) 36 json_res = json.loads(res) 37 print(json_res,type(json_res)) 38 ''' 39 {"a": 1, "b": 2, "c": 3} <class 'str'> 40 {'a': 1, 'b': 2, 'c': 3} <class 'dict'> 41 ''' 42 43 json.dump(dic,open('c.json','w')) #dump 直接把json序列化的数据写入文件 44 with open('c.json','r') as f: 45 print(f.read()) 46 json_pare = json.load(open('c.json','r')) #load直接把json反序列化的数据读出 47 print(json_pare,type(json_pare)) 48 ''' 49 {"a": 1, "b": 2, "c": 3} 50 {'a': 1, 'b': 2, 'c': 3} <class 'dict'> 51 '''
1 import json 2 #dct="{'1':111}"#json 不认单引号 3 #dct=str({"1":111})#报错,因为生成的数据还是单引号:{'one': 1} 4 5 dct='{"1":"111"}' 6 print(json.loads(dct)) 7 8 #conclusion: 9 # 无论数据是怎样创建的,只要满足json格式,就可以json.loads出来,不一定非要dumps的数据才能loads 10 11 注意点
json和python内置数据类型对应关系:
json | python |
{} | dict |
[] | list |
"string" | str |
123.33 | int/float |
true/false | True/False |
null | None |
pickle模块用法
import pickle dic_pik = pickle.dumps(dic) print(type(dic_pik)) #<class 'bytes'> #注意是w是写入str,wb是写入bytes,dic_pik是'bytes' with open('a.plk','wb') as f: #必须以b的方式打开 f.write(dic_pik) with open('a.plk','rb') as f: res = pickle.loads(f.read()) print(res,type(res)) #{'a': 1, 'b': 2, 'c': 3} <class 'dict'> pickle.dump(dic,open('b.pkl','wb')) red = pickle.load(open('b.pkl','rb')) print(res,type(res)) #{'a': 1, 'b': 2, 'c': 3} <class 'dict'>
class Foo: pass obj1=Foo() obj2=Foo() pickle.dump(obj1,open('class.pkl','wb')) print(obj1) import pickle res = pickle.load(open('class.pkl','rb')) print(res) ''' <__main__.Foo object at 0x102260518> <__main__.Foo object at 0x102260ac8> '''
6.time模块
在Python中,通常有这几种方式来表示时间:
- 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
- 格式化的时间字符串(Format String)
- 结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时
1 import time 2 3 print(time.time()) 4 print(time.strftime('%Y-%m-5d %X')) 5 print(time.localtime()) 6 print(time.gmtime()) 7 '''1501988136.581536 8 2017-08-5d 10:55:36 9 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=10, tm_min=55, tm_sec=36, tm_wday=6, tm_yday=218, tm_isdst=0) 10 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=2, tm_min=55, tm_sec=36, tm_wday=6, tm_yday=218, tm_isdst=0) 11 '''
1 # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 2 print(time.localtime().tm_hour) #加参数获取相应字段值 3 print(time.localtime(1473525444)) 4 '''11 5 time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0, tm_min=37, tm_sec=24, tm_wday=6, tm_yday=255, tm_isdst=0)''' 6 7 # gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。 8 9 # mktime(t) : 将一个struct_time转化为时间戳。 10 print(time.mktime(time.localtime())) 11 '''1501989047.0''' 12 13 # strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和 14 # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个 15 # 元素越界,ValueError的错误将会被抛出。 16 print(time.strftime('%Y-%m-%d %X',time.localtime())) 17 '''2017-08-06 11:14:35 18 ''' 19 20 # time.strptime(string[, format]) 21 # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。 22 print(time.strptime('2017-08-06 11:14:35', '%Y-%m-%d %X')) 23 '''time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=14, tm_sec=35, tm_wday=6, tm_yday=218, tm_isdst=-1) 24 ''' 25 #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"
1 # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。 2 # 如果没有参数,将会将time.localtime()作为参数传入。 3 print(time.asctime()) 4 print(time.asctime(time.gmtime())) 5 '''Sun Aug 6 11:26:43 2017 6 Sun Aug 6 03:26:43 2017''' 7 8 # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为 9 # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。 10 print(time.ctime()) 11 print(time.ctime(time.time())) 12 '''Sun Aug 6 11:26:43 2017 13 Sun Aug 6 11:26:43 2017'''
time.sleep(0.6) # 线程推迟指定的时间运行,单位为秒。
datetime用法
import datetime print(datetime.datetime.now()) #获取当前时间 print(datetime.date.fromtimestamp(time.time())) #当前时间转化成日期 '''2017-08-06 11:37:39.447878 2017-08-06''' print(datetime.datetime.now()+datetime.timedelta(3)) #当前时间加3天 print(datetime.datetime.now()+datetime.timedelta(-3)) '''2017-08-09 11:39:11.772055 2017-08-03 11:39:11.772063''' print(datetime.datetime.now()+datetime.timedelta(hours=3)) #当前时间加3小时 print(datetime.datetime.now()+datetime.timedelta(hours=-3)) '''2017-08-06 14:41:34.166602 2017-08-06 08:41:34.166614''' c_time = datetime.datetime.now() print(c_time) print(c_time.replace(minute=3,hour=2)) #时间替换 '''2017-08-06 11:42:11.178857 2017-08-06 02:03:11.178857'''
7.random模块
1 import random 2 3 print(random.choice([0,1,7])) #随机选择 4 ''' 5 def choice(self, seq): 6 """Choose a random element from a non-empty sequence.""" 7 try: 8 i = self._randbelow(len(seq)) 9 except ValueError: 10 raise IndexError('Cannot choose from an empty sequence') from None 11 return seq[i] 12 ''' 13 14 print(random.randrange(1,10)) #1<=x<10的随机整数 15 16 print(random.randint(1,10)) #1<=x<=10的随机整数 17 ''' 18 def randint(self, a, b): 19 """Return random integer in range [a, b], including both end points. 20 """ 21 22 return self.randrange(a, b+1) 23 ''' 24 print(random.random()) #0-1的随机小数 25 26 print(random.uniform(10,16)) #10-16之间的随机小数 27 28 print(random.sample([1,2,3,4,5],3)) #列表或元祖中随机组合3个,3必须小于等于seq的长度 29 30 item=[1,2,3,6,5,43] 31 print(random.shuffle(item)) #打乱原来列表的顺序 32 print(item) 33 '''None 34 [6, 5, 3, 2, 43, 1]'''
生成随机验证码
import random def make_code(n): res = '' for i in range(n): s1 = str(random.randint(0, 9)) s2 = chr(random.randint(65, 90)) s = random.choice([s1,s2]) res = res + s return res print(make_code(4)) '''R384''' ''' def chr(*args, **kwargs): # real signature unknown """ Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. """ pass '''
8.xml模块
xml实现不同语言或程序之前数据交换的格式
xml查找元素的三种方式;查看标签、属性和内容的方法:
1 import xml.etree.ElementTree as ET 2 3 tree=ET.parse('a') 4 root=tree.getroot() 5 6 for child in root: 7 print('tags>>>:',child.tag) 8 for i in child: 9 print('tag>>:{} attrid>>:{} text>>:{}'.format(i.tag,i.attrib,i.text)) 10 '''tags>>>: country 11 tag>>:rank attrid>>:{'updated': 'yes'} text>>:2 12 tag>>:year attrid>>:{} text>>:2008 13 tag>>:gdppc attrid>>:{} text>>:141100 14 tag>>:neighbor attrid>>:{'name': 'Austria', 'direction': 'E'} text>>:None 15 tag>>:neighbor attrid>>:{'name': 'Switzerland', 'direction': 'W'} text>>:None 16 tags>>>: country.......''' 17 18 #查找element元素的三种方式 19 years=root.iter('year') #扫描整个xml文档树,找到所有 20 for i in years: 21 print(i,i.text) 22 '''<Element 'year' at 0x1022987c8> 2008 23 <Element 'year' at 0x1022b4098> 2011 24 <Element 'year' at 0x1022b4228> 2011''' 25 26 res1 = root.find('year') 27 print(res1) 28 res2=root.find('country') #谁来调,就从谁下一层开始找,只找一个 29 print(res2) 30 '''None 31 <Element 'country' at 0x1005b49a8>''' 32 33 res3=root.findall('country') #谁来调,就从谁下一层开始找,只找一个 34 print(res3) 35 '''[<Element 'country' at 0x1005b49a8>, <Element 'country' at 0x1022aaf98>, <Element 'country' at 0x1022b4188>] 36 '''
xml的增删改操作:
1 #修改 2 years = root.iter('year') #扫描整个xml文档述,找到所有years的标签 3 for year in years: 4 year.text = str(int(year.text)+1) #修改标签内容 5 year.set('updated','yes') 6 year.set('version','1.0') #增加标签属性 7 tree.write('a') #写入文件,刚才只是在内存做的修改 8 9 #删除 10 for country in root.iter('country'): 11 rank = country.find('rank') 12 if int(rank.text) > 10: 13 country.remove(rank) 14 tree.write('a') 15 16 17 #增加节点 18 #在country内添加(append)节点year2 19 import xml.etree.ElementTree as ET 20 tree = ET.parse("a") 21 root=tree.getroot() 22 for country in root.findall('country'): 23 for year in country.findall('year'): 24 if int(year.text) > 2000: 25 year2=ET.Element('year2') 26 year2.text='新年' 27 year2.attrib={'update':'yes'} 28 country.append(year2) #往country节点下添加子节点 29 30 tree.write('a.xml.swap')
9.configparser
解析处理configparser文件
1 import configparser 2 3 config = configparser.ConfigParser() 4 config.read('p4p') 5 6 #取配置 7 print(config.sections()) 8 '''['application', 'ui-p4p-user', 'qa-p4p-user', 'qa-agent-user', 'qa-p4p-redis', 'qa-engine-redis', 'qa-p4p-antiattack', 'qa-omp-mysql', 'qa-p4p-mysql'] 9 以列表的形式返回标题 10 ''' 11 print(config.options(config.sections()[3])) 12 '''['mail', 'password'] 13 查看某个标题下的配置项 14 ''' 15 res = config.get('qa-p4p-redis','redis_host') 16 '''10.165.124.28 <class 'str'> 17 查看某个标题下的某个配置项的值 18 ''' 19 res1 = config.getint('qa-p4p-redis','redis_port') 20 print(res1,type(res1)) 21 '''8801 <class 'int'> 22 #查看某个标题下的某个配置项的值''' 23 24 25 res2=config.getboolean('qa-p4p-redis','redis_database')#查看某个标题下的某个配置项的值 26 print(res2,type(res2)) 27 '''False <class 'bool'>''' 28 29 # res1=config.getfloat('egon','salary')#查看某个标题下的某个配置项的值 30 # print(type(res1)) 31 32 #修改 33 config.remove_section('qa-p4p-mysql') 34 config.remove_option('qa-omp-mysql','mysql_charset') 35 36 config.add_section('qa-p4p-zyl') 37 config.set('qa-p4p-zyl','name','zyl') 38 39 config.write(open('p4p','w')) #写入文件
#获取标题下的所有键值对 res4 = config.items('qa-omp-mysql') print(res4) '''[('mysql_host', '10.165.124.46'), ('mysql_port', '3306'), ('mysql_user', 'omptest'), ('mysql_password', 'omptest!@#$'), ('mysql_database', 'omp')] ''' #检查 has_sec=config.has_section('qa-omp-mysql') print(has_sec) #打印True
10.hashlib
hash:一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
三个特点:
1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
2.不可逆推
3.相同算法:无论校验多长的数据,得到的哈希值长度固定。
1 import hashlib 2 3 m=hashlib.md5()# m=hashlib.sha256() 4 5 m.update('hello'.encode('utf8')) 6 print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592 7 8 m.update('alvin'.encode('utf8')) 9 10 print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 11 12 m2=hashlib.md5() 13 m2.update('helloalvin'.encode('utf8')) 14 print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 15 16 ''' 17 注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样 18 但是update多次为校验大文件提供了可能。 19 ''' 20 21 #以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。 22 import hashlib 23 24 # ######## 256 ######## 25 26 hash = hashlib.sha256('898oaFs09f'.encode('utf8')) 27 hash.update('alvin'.encode('utf8')) 28 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7 29 30 31 import hashlib 32 33 # ######## 256 ######## 34 35 hash = hashlib.sha256('898oaFs09f'.encode('utf8')) 36 hash.update('alvin'.encode('utf8')) 37 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7 38 39 #python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密 40 import hmac 41 h = hmac.new('alvin'.encode('utf8')) 42 h.update('hello'.encode('utf8')) 43 print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940 44 45 #要想保证hmac最终结果一致,必须保证: 46 #1:hmac.new括号内指定的初始key一样 47 #2:无论update多少次,校验的内容累加到一起是一样的内容 48 49 import hmac 50 51 h1=hmac.new(b'egon') 52 h1.update(b'hello') 53 h1.update(b'world') 54 print(h1.hexdigest()) 55 56 h2=hmac.new(b'egon') 57 h2.update(b'helloworld') 58 print(h2.hexdigest()) 59 60 h3=hmac.new(b'egonhelloworld') 61 print(h3.hexdigest()) 62 63 ''' 64 f1bf38d054691688f89dcd34ac3c27f2 65 f1bf38d054691688f89dcd34ac3c27f2 66 bcca84edd9eeb86f30539922b28f3981 67 '''
11.subprocess
生成子进程
1 import subprocess 2 3 ''' 4 sh-3.2# ls /Users/egon/Desktop |grep txt$ 5 mysql.txt 6 tt.txt 7 事物.txt 8 ''' 9 10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE) 11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout, 12 stdout=subprocess.PIPE) 13 14 print(res.stdout.read().decode('utf-8')) 15 16 17 #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep 18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE) 19 print(res1.stdout.read().decode('utf-8')) 20 21 22 #windows下: 23 # dir | findstr 'test*' 24 # dir | findstr 'txt$' 25 import subprocess 26 res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函数备课',shell=True,stdout=subprocess.PIPE) 27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout, 28 stdout=subprocess.PIPE) 29 30 print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码