DAY18 常用模块(二)
一.随机数:RANDOM
1.(0,1)小数:random.random()
2.[1,10]整数:random.randint(1,10)
3.[1,10)整数:random.randrang(1,10)
4.(1,10)小数:random.uniform(1,10)
5.单列集合随机选择一个:random,choice(item)
6.单列集合随机选择n个:random.sample(item,n)
7.洗牌单列集合:random.shuffle(item)
import random
def random_code(count):
code = ''
for i in range(count):
num = random.randint(1, 3)
if num == 1:
tag = str(random.randint(0, 9))
elif num == 2:
tag = chr(random.randint(65, 90))
else:
tag = chr(random.randint(97, 122))
code += tag
return code
print(random_code(6))
产生指定位数的验证码
二.可以操作权限的处理文件模块
1.基于路径的文件复制:shutil.copyfile('原文件','新文件')
2.基于流的文件复制:
with open('source_file','rb') as r,
open('target_file','rb') as w
shutil.copyfileeobj(r,w)
3.递归删除目标目录
shutil.rmtree('target_folder')
4.文件移动:
shutile.remove('old_file','new_file')
5.文件夹压缩
shutil.make_archive('file_name','format','achive_path')
6.文件夹解压
shutil.unpack_archive('unpack_file','unpack_name','format')
三.可以用字典存取数据到文件的序列化模块:shevle
1.将序列化文件操作dump与load进行封装
s_dic = shelve.open("target_file", writeback=True) # 注:writeback允许序列化的可变类型,可以直接修改值
2.序列化::存
s_dic['key1'] = 'value1'
s_dic['key2'] = 'value2'
3.反序列化:取
print(s_dic['key1'])
4.文件释放
s_dic.close()
四.标准输入输出错误流
sys.stdout.write('msg')
sys.stderr.write('msg')
msg = sys.stdin.readline()
注:print默认是对sys.stdout.write('msg') + sys.stdout.write('\n')的封装
格式化结束符print:print('msg', end='')
五.日志模块:logging
DAY18 常用模块(二)的更多相关文章
- 常用模块二(hashlib、configparser、logging)
阅读目录 常用模块二 hashlib模块 configparse模块 logging模块 常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...
- python之常用模块二(hashlib logging configparser)
摘要:hashlib ***** logging ***** configparser * 一.hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法 ...
- python14 常用模块 二
一.json模块 强大:不同语言之间可以进行数据交换 序列化:把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serializati ...
- Python自动化开发 - 常用模块(二)
本节内容 1.shutil模块 2.shelve模块 3.xml处理模块 4.configparser模块 5.hashlib模块 6.subprocess模块 7.re模块 一.shutil模块 高 ...
- Python常用模块二
一.time & datetime #_*_coding:utf-8_*_ import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了t ...
- Python常用模块(二)
一.json与pickle json与pickle模块是为了完成数据的序列化. 序列化是指把对象(变量)从内存中变成可存储或传输的过程,在Python中叫picking,在其他语言中也由其他的叫法,但 ...
- python之路----常用模块二
collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...
- Python中常用模块二
一.hashlib (加密) hashlib:提供摘要算法的模块 1.正常的md5算法 import hashlib # 提供摘要算法的模块 md5 = hashlib.md5() md5.upd ...
- day18常用模块之re模块
简单认识正则表达式 正则测试工具:http://tool.chinaz.com/regex/ 元字符,单个字符匹配 . 匹配除换行符以外的任意字符 \w 匹配字母数字下划线(word) \s 匹配任意 ...
随机推荐
- tensorflow training result
- Oracle程序备份
--使用root用户备份 su - root mkdir -p /oracle/data_dump/backup_soft nohup tar -cvf /oracle/data_dump/backu ...
- Percona-Toolkit 之 pt-table-checksum 总结
pt-table-checksum - Verify MySQL replication integrity. pt-table-checksum performs an online replica ...
- TerminateProcess实现关闭任意程序
#include <Windows.h> #include <tchar.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTA ...
- TTL集成门电路
一.TTL集成门电路的结构1.总体结构 所谓TTL就是transistor transistor logic,就是说是由晶体管和晶体管之间构成电路. 2. TTL集成门电路典型输入级形式 1)二 ...
- vue 脚手架关于路由的一点理解
https://router.vuejs.org/zh/ 可以先翻翻文档看看介绍啊,一般我不怎么喜欢看文档,都是直接看人家案例,在回头看文档的,所以学习速度慢很多,希望我以后成为一个爱学习的妹子,比较 ...
- Linux环境下部署开源版“禅道”方法
1.开源版安装包下载(Linux系统版本查看命令 uname -a) 32位 [root@iZbp~]# wget http://dl.cnezsoft.com/zentao/9.0.1/ZenTao ...
- CodeForces - 1102A
You are given an integer sequence 1,2,-,n1,2,-,n. You have to divide it into two sets AA and BB in s ...
- MacOS修改用户名后变为普通用户,无法创建管理员账号
摘要:有的时候用户修改原电脑用户名,会把该用户降为普通用户,点击下方的锁会弹出下方图示,导致无法修改任何设置 解决方案: 重启电脑Restart按Command+S进入终端terminal输入以下命令 ...
- 与TIME_WAIT相关的几个内核参数
问题 公司用浏览器访问线上服务一会失败一会成功,通过ssh连接服务器排查时发现ssh也是这样: 检查 抓包后发现建立连接的请求已经到了服务器,但它没有响应: 纠结了好久,后来在腾讯云技术支持及查了相关 ...