模块2

# # 一  random:随机数
# # (0,1)小数:random.random()
# import random
# r=random.random()#后面括号里面没有参数,是(0,1)之间的随即小数
# print(r)#(0,1)
# n=random.randint(1,10)
# print(n)#[1,10],需要两个参数
# x=random.randrange(1,10)
# print(x)#[1,10)
# c=random.uniform(1,10)
# print(c)#是(1,10)之间的小数
# a=random.choice('sfsf')#只需要一个支持索引的单列集合list,tuple,str
# print(a,type(a))#只取一个,出来后是字符串
# b=random.choices([12,34,56,78])
# print(b,type(b))#只取一个,出来后是一个列表
# c=random.sample([1,2,3,45,5],2)#参数是一个单列集合,和需要的数值
# print(c)#出来后是一个列表,第一个参数是单列集合
# r=[0,9,8,7]
# random.shuffle(r)#需要先定义一个r,再将r当作参数
# print(r)#出来后是一个列表
# r=[1,2,3,4,5]
# random.shuffle(r)#应用场景,斗地主洗牌
# print(r)#只可以针对列表
# 产生指定位数的验证码
# import random
# def random_code(count):
# code=''
# for i in range(count):#根据用户输入的数字选择随机数产生次数
# # 因为是随机,所以用到了数字的整数随机randint
# num=random.randint(1,3)#用户选择1,2,3分别对应的数字,大写,小写等
# if num==1:#注意与前面的数字对应,所以不是字符串
# tag=str(random.randint(0,9))#生成[0,9]的随机数
# elif num==2:
# tag=chr(random.randint(65,90))#ASCII码对应的小写字母
# else:
# tag=chr(random.randint(97,122))#ASCII码对应的大写字母
# code+=tag
# return code
# print(random_code(7))
# 二 shutil:可以操作权限的处理文件模板#被copy的文件必须先存在
# 基于路径的文件复制
# import shutil
# e=shutil.copyfile('source_file.py','target_file.py')
# print(e)
# # 基于流的文件复制:
# with open('source_file','rb') as r,open('target_file','wb') as w:
# shutil.copyfileobj(r,w)
# # 递归删除目标目录
# shutil.rmtree('target_folder')
# # 文件移动
# shutil.remove('old_file','new_file')#也可以当作重命名
# # 文件夹压缩
# shutil.make_archive('file_name','format','archive_path')#压缩后的文件名,对应的压缩格式,对应的环境变量
# # 文件夹解压
# shutil.unpack_archive('unpack_file','unpack_name','format')#解压后的文件名,解压后的名字,对应的解压缩格式

# # 三 shevle:可以用字典存取数据到文件的序列化模块
# # 将序列化文件操作dump与load进行封装
# import shelve
# s_dic=shelve.open('target_file',writeback=True)#write允许序列化的可变类型,可以直接修改值
# # 序列化:存
# s_dic['key1']='value1'
# s_dic['key2']='value2'
# s_dic['key3']='value3'
# # 反序列化:取
# print(s_dic['key1'])
# # 文件释放
# s_dic.close()

# # 四 三流:标准输入输出错误流
# import sys
# sys.stdout.write('msg')
# sys.stderr.write('msg')#上面两行输出结果是不换行打印
# msg=sys.stdin.readline()#
# # print默认是对sys.stout.write('msg)+sys.stout.write('\n')的封装
# # 格式化结束符print:print(’msg',end='')

# 五 logging:日志模块
# import logging
# 1) root logging 的基本使用:五个级别
# 2)root logging 的基本配置:logging.basicconfig()
# 3) logging 模块四个核心:logger|filter(过滤,没讲)|handler|formater
# 4)logging模块的配置与使用
# --配置文件:logging_dic={}
# --加载配置文件:logging.config.dictconfig(logging_dic)=>logging.getlogger('log_name')
posted @ 2019-04-11 22:19  mcc61  阅读(212)  评论(1编辑  收藏  举报