import random(模块)node
import random 取随机小数 : 数学计算 print(random.random()) # 取0-1之间的小数 (大于0小于1之间的数) print(random.uniform(1,2)) # 取1-2之间的小数 (大于1小于2之间的数) 取随机整数 : 彩票 抽奖 print(random.randint(1,2)) # [1,2] print(random.randrange(1,2)) # [1,2) print(random.randrange(1,200,2)) # [1,2)
随机整数
>>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> random.randrange(1,10,2) # 大于等于1且小于10之间的奇数
从一个列表中随机抽取值 : 抽奖 l = ['a','b',(1,2),123] print(random.choice(l)) print(random.sample(l,2))
随机选择一个返回>>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5]
#随机选择多个返回,返回的个数为函数的第二个参数 >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合
[[4, 5], '23']
打乱一个列表的顺序,在原列表的基础上直接进行修改,节省空间 洗牌 random.shuffle(l) print(l)
打乱列表顺序
>>> item=[1,3,5,7,9] >>> random.shuffle(item) # 打乱次序 >>> item [5, 1, 3, 7, 9] >>> random.shuffle(item) #每一次顺序不同 >>> item [5, 9, 7, 1, 3]
4位数字验证码(随机数字)python
# s = '' # for i in range(4): # num = random.randint(0,9) # s += str(num) # print(s)
6位数字验证码(随机数字)shell
# s = '' # for i in range(6): # num = random.randint(0,9) # s += str(num) # print(s)
函数版的6位4位验证码(随机数字)bash
def code(n=6): s = '' for i in range(n): num = random.randint(0,9) s += str(num) return s print(code(4)) print(code())
6位数字+字母验证码(随机数字)app
一个位置上 出现的是数字仍是字母 是随机数字 随机字母dom
s = '' for i in range(6): # 生成随机的大写字母,小写字母,数字各一个 num = str(random.randint(0,9)) alpha_upper = chr(random.randint(65,90)) alpha_lower = chr(random.randint(97,122)) res = random.choice([num,alpha_upper,alpha_lower]) s += res print(s)
函数版4位6位 数字字母混合验证码(随机数字字母)ide
def code(n = 6): s = '' for i in range(n): # 生成随机的大写字母,小写字母,数字各一个 num = str(random.randint(0,9)) alpha_upper = chr(random.randint(65,90)) alpha_lower = chr(random.randint(97,122)) res = random.choice([num,alpha_upper,alpha_lower]) s += res return s print(code(4)) print(code())
函数版4位 6位数字字母大小写混合验证码(随机数字字母)函数
def code(n = 6,alpha = True): s = '' for i in range(n): # 生成随机的大写字母,小写字母,数字各一个 if alpha: num = str(random.randint(0,9)) alpha_upper = chr(random.randint(65,90)) alpha_lower = chr(random.randint(97,122)) res = random.choice([num,alpha_upper,alpha_lower]) s += res else: num = random.randint(0, 9) s += str(num) return s print(code(4)) print(code())
函数版4位 6位 数字验证码(随机数字)ui
def code(n = 6,alpha = True): s = '' for i in range(n): num = str(random.randint(0,9)) if alpha: alpha_upper = chr(random.randint(65,90)) alpha_lower = chr(random.randint(97,122)) num = random.choice([num,alpha_upper,alpha_lower]) s += num return s print(code(4,False)) print(code(alpha=False))
时间模块 import timespa
和时间有关系的咱们就要用到时间模块. 在使用模块以前, 先导入这个模块
#经常使用方法 1.time.sleep(secs) (线程)推迟指定的时间运行。单位为秒。 2.time.time() 获取当前时间戳
表示时间的三种方式
在python中, 一般有三种方式来表示时间:时间戳 , 元祖(struct_time) , 格式化的时间字符串:
(1)时间戳(timestamp) : 一般来讲 , 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.咱们运行"type(time.time())",返回的是float类型.
(2)格式化的时间字符串(Format String): '1999-12-06'
1 %y 两位数的年份表示(00-99) 2 %Y 四位数的年份表示(000-9999) 3 %m 月份(01-12) 4 %d 月内中的一天(0-31) 5 %H 24小时制小时数(0-23) 6 %I 12小时制小时数(01-12) 7 %M 分钟数(00=59) 8 %S 秒(00-59) 9 %a 本地简化星期名称 10 %A 本地完整星期名称 11 %b 本地简化的月份名称 12 %B 本地完整的月份名称 13 %c 本地相应的日期表示和时间表示 14 %j 年内的一天(001-366) 15 %p 本地A.M.或P.M.的等价符 16 %U 一年中的星期数(00-53)星期天为星期的开始 17 %w 星期(0-6),星期天为星期的开始 18 %W 一年中的星期数(00-53)星期一为星期的开始 19 %x 本地相应的日期表示 20 %X 本地相应的时间表示 21 %Z 当前时区的名称 22 %% %号自己
(3)元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
首先, 咱们先导入time模块, 来认识一下python中表示的几种格式:
impo time 进入模块 # 时间戳时间 # print(time.time()) # 格式化时间 # print(time.strftime('%Y-%m-%d %H:%M:%S')) # str format time # print(time.strftime('%y-%m-%d %H:%M:%S')) # str format time # print(time.strftime('%c')) # 结构化时间 # struct_time = time.localtime() # 北京时间 # print(struct_time) # print(struct_time.tm_mon) # 时间戳换成字符串时间 # print(time.time()) # struct_time = time.localtime(1500000000) # # print(time.gmtime(1500000000)) # ret = time.strftime('%y-%m-%d %H:%M:%S',struct_time) # print(ret) # 字符串时间 转 时间戳 # struct_time = time.strptime('2018-8-8','%Y-%m-%d') # print(struct_time) # res = time.mktime(struct_time) # print(res)
小结:时间戳是计算机可以识别的时间 ; 时间字符串是人能看懂的时间 ; 元祖则是用来操做时间的
几种格式之间的转换:
#时间戳-->结构化时间 #time.gmtime(时间戳) #UTC时间,与英国伦敦当地时间一致 #time.localtime(时间戳) #当地时间。例如咱们如今在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间 >>>time.gmtime(1500000000) time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0) >>>time.localtime(1500000000) time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0) #结构化时间-->时间戳 #time.mktime(结构化时间) >>>time_tuple = time.localtime(1500000000) >>>time.mktime(time_tuple) 1500000000.0 复制代码
#结构化时间-->字符串时间 #time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间 >>>time.strftime("%Y-%m-%d %X") '2017-07-24 14:55:36' >>>time.strftime("%Y-%m-%d",time.localtime(1500000000)) '2017-07-14' #字符串时间-->结构化时间 #time.strptime(时间字符串,字符串对应格式) >>>time.strptime("2017-03-16","%Y-%m-%d") time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1) >>>time.strptime("07/24/2017","%m/%d/%Y") time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)
#结构化时间 --> %a %b %d %H:%M:%S %Y串 #time.asctime(结构化时间) 若是不传参数,直接返回当前时间的格式化串 >>>time.asctime(time.localtime(1500000000)) 'Fri Jul 14 10:40:00 2017' >>>time.asctime() 'Mon Jul 24 15:18:33 2017' #时间戳 --> %a %b %d %H:%M:%S %Y串 #time.ctime(时间戳) 若是不传参数,直接返回当前时间的格式化串 >>>time.ctime() 'Mon Jul 24 15:19:07 2017' >>>time.ctime(1500000000) 'Fri Jul 14 10:40:00 2017'
练习题:
# 1.查看一下2000000000时间戳时间表示的年月日 # 时间戳 - 结构化 - 格式化 # struct_t = time.localtime(2000000000) # print(struct_t) # print(time.strftime('%y-%m-%d',struct_t)) # 2.将2008-8-8转换成时间戳时间 # t = time.strptime('2008-8-8','%Y-%m-%d') # print(time.mktime(t)) # 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数 # 2018-8-1 # def get_time(): # st = time.localtime() # st2 = time.strptime('%s-%s-1'%(st.tm_year,st.tm_mon),'%Y-%m-%d') # return time.mktime(st2) # print(get_time()) # 4.计算时间差 # 2018-8-19 22:10:8 2018-8-20 11:07:3 # 通过了多少时分秒 # str_time1 = '2018-8-19 22:10:8' # str_time2 = '2018-8-20 11:07:3' # struct_t1 = time.strptime(str_time1,'%Y-%m-%d %H:%M:%S') # struct_t2 = time.strptime(str_time2,'%Y-%m-%d %H:%M:%S') # timestamp1 = time.mktime(struct_t1) # timestamp2 = time.mktime(struct_t2) # sub_time = timestamp2 - timestamp1 # gm_time = time.gmtime(sub_time) # # 1970-1-1 00:00:00 # print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1, # gm_time.tm_mday-1,gm_time.tm_hour, # gm_time.tm_min,gm_time.tm_sec)) # # print(time.strftime("%y-%m-%d ")) # print(time.strftime("%y-%m-%d %H-%M-%S"))
sys模块 (impor sys)
sys模块是与python解释器交互的一个接口
sys.argv 命令行参数List,第一个元素是程序自己路径 sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit(1) sys.version 获取Python解释程序的版本信息 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 sys.platform 返回操做系统平台名称
模块地址的导入
import re # sys.modules # print(sys.modules) # 是咱们导入到内存中的全部模块的名字 : 这个模块的内存地址 # print(sys.modules['re'].findall('\d','abc126'))
os模块(import os)
os模块是与操做系统交互的一个接口
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.system("bash command") 运行shell命令,直接显示 os.popen("bash command).read() 运行shell命令,获取执行结果
os.getcwd() 获取当前工做目录,即当前python脚本工做的目录路径 os.chdir("dirname") 改变当前脚本工做目录;至关于shell下cd os.path 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.stat('path/filename')获取文件/目录信息的结构说明
import os # 把路径中不符合规范的/改为操做系统默认的格式 # path = os.path.abspath('D:/sylar/s15/day19/4.os模块.py') # print(path) # 可以给能找到的相对路径改为绝对路径 # path = os.path.abspath('4.os模块.py') # print(path) # 就是把一个路径分红两段,第二段是一个文件/文件夹 # path= os.path.split('D:/sylar/s15/day19/4.os模块.py') # print(path) # path= os.path.split('D:/sylar/s15/day19') # print(path) # ret1 = os.path.dirname('D:/sylar/s15/day19/4.os模块.py') # ret2 = os.path.basename('D:/sylar/s15/day19/4.os模块.py') # print(ret1) # print(ret2) # 若是你两个值都须要 os.path.split # 若是你只要一个值 os.path.dirname/os.path.basename # 判断文件/文件夹是否存在 # res = os.path.exists(r'D:\sylar\s15\day19\4.os模块.py') # print(res) # res1 = os.path.isabs('4.os模块.py') # res2 = os.path.isabs(r'D:\sylar\s15\day19\4.os模块.py') # print(res1) # print(res2) # print(os.listdir('D:\sylar\s15')) # print(os.path.isdir(r'D:\sylar\s15\aaa')) # print(os.path.isfile(r'D:\sylar\s15\aaa')) # print(os.path.isfile('D:\sylar\s15\day01')) # print(os.path.isdir('D:\sylar\s15\day01')) # path = os.path.join('D:\sylar\s15','bbb') # print(path) # size= os.path.getsize(r'D:\sylar\s15\day19\4.os模块.py') # 查看文件大小 # print(size) # ret1 = os.path.getsize('D:\sylar\s15\day19') # ret2 = os.path.getsize('D:\sylar\s15\day18') # ret3 = os.path.getsize('D:\sylar\s15\day17') # ret4 = os.path.getsize('D:\sylar\s15') # print(ret1,ret2,ret3,ret4) # 全部的文件夹 都至少是4096个字节 # 8192 # 64字节 + 32字节/新文件
stat 结构: st_mode: inode 保护模式 st_ino: inode 节点号。 st_dev: inode 驻留的设备。 st_nlink: inode 的连接数。 st_uid: 全部者的用户ID。 st_gid: 全部者的组ID。 st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。 st_atime: 上次访问的时间。 st_mtime: 最后一次修改的时间。 st_ctime: 由操做系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是建立时间(详细信息参见平台的文档)。
os.sep 输出操做系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为: os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
# 使用python代码统计一个文件夹中全部文件的总大小 # 你须要统计文件夹大小 # D:\sylar\s15\ 文件夹的大小 # 拿到这个文件夹下全部的文件夹 和 文件 # 是文件就取大小 # 是文件夹 再打开这个文件夹 : 文件/文件夹 # 递归 # def func(path): # r'D:\sylar\s15' # size_sum = 0 # name_lst = os.listdir(path) # for name in name_lst: # path_abs = os.path.join(path,name) # if os.path.isdir(path_abs): # size = func(path_abs) # size_sum += size # else: # size_sum += os.path.getsize(path_abs) # return size_sum # # ret = func(r'D:\sylar\s15') # print(ret) # def func(path): # D:\sylar\s15 # size_sum = 0 # 0 # name_lst = os.listdir(path) # ['day01','day02',...] # for name in name_lst: # 'day01' 'day02' # path = os.path.join(path,name) # 'D:\sylar\s15\day01' # if os.path.isdir(path): # True # size = func(path) # func('D:\sylar\s15\day01') # size_sum += size # else: # size_sum += os.path.getsize(path) # return size_sum # # def func(path): # 'D:\sylar\s15\day02' # size_sum = 0 # sum = 0 # name_lst = os.listdir(path) # [first.py...] # for name in name_lst: # first.py 100 # path = os.path.join(path,name) # if os.path.isdir(path): # func(path) # else: # 文件 # size_sum += os.path.getsize(path) # sizesum += 100 = 100+100+100+100 = 500 # return size_sum # return 500 # 循环 # 堆栈思想 # 列表 知足一个顺序 先进来的后出去 # lst = [r'D:\sylar\s15',] # 列表的第一个目录就是我要统计的目录 # size_sum = 0 # while lst: # [r'D:\sylar\s15',] lst = ['D:\sylar\s15\day01','D:\sylar\s15\day01'..] # path = lst.pop() # path = 'D:\sylar\s15' lst = [] # path_list = os.listdir(path) # path_list = ['day01',day02',aaa,day15.py] # for name in path_list: # name = day01 # abs_path = os.path.join(path,name) # if os.path.isdir(abs_path): # 文件夹的逻辑 # lst.append(abs_path) # lst.append('D:\sylar\s15\day01') lst = ['D:\sylar\s15\day01'] # else: # size_sum += os.path.getsize(abs_path) # print(size_sum)