[TOC]
模块就是py文件
注意文件的命名不能和模块的名字相同,正则在寻找的时候首先从当前路径寻找,然后从内置查找
1 时间模块time
Python中有三种表示时间的方法:
1.时间戳
- 时间戳timestap(浮点型数字):(计算机)
time.time()
1970年1月1日的是0
2.元组(struct time)结构化时间
- 结构化时间:(操作时间)
>>> time.localtime() # 默认放的是time.time()
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=9, tm_min=15, tm_se
c=53, tm_wday=2, tm_yday=116, tm_isdst=0)
>>>
c=time.localtime()
拿到时间:
c.tm_year
3.格式化的时间字符串
- 格式化的时间字符串:(给人看)
time.strftime()
>>> time.strftime("%Y-%m-%d")
'2017-04-26'
>>> time.strftime("%Y-%m-%d %X")
'2017-04-26 09:13:02'
字符串时间和时间戳是不能直接转换的,通过结构化时间转换
1.1 时间戳
通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
1 2
| import time print(time.time())
|
1.2 结构化时间
struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等
结果:
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=16, tm_min=0, tm_sec=5, tm_wday=2, tm_yday=116, tm_isdst=0)
1.3 格式化的时间
格式化的时间需要参数,可以用help查看
1
| print(time.strftime('%Y-%m-%d %X'))
|
使用help(time.strftime)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.
|
1.4 三种时间形式的转换

1.4.1 时间戳转换结构化时间
1 2 3 4
| import time print(time.localtime(time.time())) print(time.localtime(time.time()+86400)) print(time.gmtime(time.time()))
|
1.4.2 结构化时间转 时间戳
1
| print(time.mktime(time.localtime()))
|
1.4.2 格式化字符串时间转换成结构化时间
1 2 3
| import time res=time.strftime('%Y-%m-%d %X',time.localtime()) print(time.strptime(res,'%Y-%m-%d %X'))
|
先获取格式化的时间,赋值给res,res作为参数 后面是格式
1.4.4 结构化时间转化成格式化时间
1 2
| print(time.strftime('%Y-%m-%d %X',time.localtime())) print(time.strftime('%Y-%m-%d %X'))
|
2 random模块
1 2 3 4 5 6 7 8 9 10
| random.random() random.randint(1,4) random.randrange(1,4) random.choice([11,44,55]) random.sample([11,44,55],2) random.uniform(1,3) random.shuffle([11,22,33]) item=[1,2,3,4] print(random.shuffle(item)) print(item)
|
生成验证码:
需求是生成5位的验证码,是随机的字母数字,字母有大写小写
分析:关键是通过chr能够把数字转换成相应的字母,这里用的是ASCII码表的知识
1 2 3 4 5 6 7 8 9 10 11
| import random def Verification(): s='' for i in range(5): rnum = random.randint(0,9) rL = chr(random.randint(65,90)) rl = chr(random.randint(97,122)) res = random.choice([rnum,rL,rl]) s+=str(res) return s print(Verification())
|
ASCII维基百科
3 hashlib 模块
把任意长度的数据转换成固定长度的数据(通常用16进制的字符串表示)
严格来说,这个并不是加密,加密的是能够解密的,hash是通过算法把数据转换,例如密码保存在数据库中是MD5,用户输入密码后,会把密码用md5转换,用转换后的和数据库中的比对
摘要算法:MD5 SHA…
3.1 MD5
ftp传文件的时候可以通过吧文件的每一行进行update
1 2 3 4 5 6 7 8 9 10
| import hashlib a=hashlib.md5() a.update('hello'.encode('utf-8')) print(a.hexdigest()) a.update('hello'.encode('utf-8')) print(a.hexdigest()) b=hashlib.md5() b.update('hellohello'.encode('utf-8')) print(a.hexdigest())
|
结果:
5d41402abc4b2a76b9719d911017c592
23b431acfeb41e15d466d75de822307c
23b431acfeb41e15d466d75de822307c
3.2 SH1、SH…
调用的方法是一样的,只是名字不一样
1 2 3 4 5 6 7
| import hashlib m=hashlib.md5() m.update('hello'.encode('utf-8')) print(m.hexdigest()) n=hashlib.sha1() n.update('hello'.encode('utf-8')) print(n.hexdigest())
|
结果:位数不一样
5d41402abc4b2a76b9719d911017c592
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
3.2 加盐 slat
加盐的应用,适用于系统添加固定的字符强化加密
1 2 3 4 5 6 7 8 9
| import hashlib a = hashlib.md5('hello'.encode('utf-8')) a.update('hello'.encode('utf-8')) print(a.hexdigest()) b=hashlib.md5() b.update('hellohello'.encode('utf-8')) print(b.hexdigest())
|
4 os模块
os模块是与操作系统交互的接口
记住功能
1.os.getcwd() 当前绝对路径
2.os.chdir(r’绝对路径’) 切换目录
3.os.curdir 返回当前目录 就是.
4.os.pardir ..
5.os.makedirs(“aaa/bbb”) 在当前目录生成多层递归
6.os.removedirs(‘aaa’) 如果目录为空,则删除
7.os.rmdir()
8.os.listdir(r””) 就是ls功能 把当前的路径下的所有的内容
9.os.rename(“old,new”)
10.os.stat(r””) 文件信息 返回的是文件的结构化信息(文件大小、访问时间、)
11.os.sep win是\,linux是/
12.os.linesep 行终止符号 win是\t\n linux – \n
13.os.pathsep 文件路径拼接 win; linux
14.os.name win – nt linux– posix 可以判断平台
15.os.system(“dir”) # 类似bash 可以输入ls cd
16.os.path.abspath(“test.txt”) basepath+filename
17.os.dirname(“test.txt”) 文件路径
18.os.bsename(“test.txt”) 文件名
19.os.path.exits()
20.isabs
21.isdir
22.==os.path.join(s1,s2)==路径的拼接 在不同的平台的情况能够自动
23.os.getatime(path)
24.os.getmtime(path)
25.os.getsize(path) 获取文件的大小
1 2
| import os print(os.getcwd()) 获得当前的绝对路径
|
1 2 3
| print(os.getcwd()) os.chdir(r"D:\Python_fullstack_s4\32") print(os.getcwd())
|
结果:
D:\Python_fullstack_s4\33
D:\Python_fullstack_s4\32
1 2
| print(os.curdir) print(os.pardir)
|
结果:
.
..
1
| os.removedirs("aaaa/bbb")
|
1 2
| res=os.stat('D:\Python_fullstack_s4\day33/bbb') print(res)
|
结果:
os.stat_result(st_mode=33206, st_ino=1970324837071832, st_dev=374768, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1493205622, st_mtime=1493205622, st_ctime=1493205622)
1
| os.remove(r'D:\Python_fullstack_s4\day33\bbb')
|
1 2 3 4 5 6
| print(os.name) ```结果 win---nt linux---posix ```python os.system('dir')
|
1
| print(os.path.abspath('test.txt'))
|
1 2 3
| abs=os.path.abspath('test.txt') print(os.path.basename(abs)) print(os.path.dirname(abs))
|
test.txt
D:\Python_fullstack_s4\day33
1
| print(os.path.exists('test.txt') )
|
1
| print(os.path.isabs('test.txt'))
|
1
| print(os.path.isdir('aa'))
|
1 2 3 4 5
| s1=r"C:\Users\Administrator\PycharmProjects" s2=r"py_fullstack_s4\day33" ret=os.path.join(s1,s2) print(ret)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| ''' 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的大小 '''
|
5 sys模块
sys是与解释器有关的
1.sys.exit() 程序退出
2.sys.version Python版本
3.sys.platform 返回操作系统的平台
4.sys.argv 程序执行前输入参数,返回值是一个列表 可以去到
5.==sys.path== sys的寻找路径的优先级 大一点的程序是在不同的目录中的、模块化
1 2 3 4 5 6
| sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sys.maxint 最大的Int值 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 sys.platform 返回操作系统平台名称
|