Day16作业及默写
hashlib模块,写函数校验两个文件是否内容相同(如果这两个文件很大)
import hashlib
md5 = hashlib.md5()
md5.update(b'hello')
md5.update(b'world')
ret = md5.hexdigest()
print(ret)
md5 = hashlib.md5()
md5.update(b'helloworld')
ret = md5.hexdigest()
print(ret)
#对比文件一致性的时候还需要加盐?
# 一个文件的md5值和另一个文件的md5值是不是一样
#两个文件 为什么要加密去对比
#文件的一致性校验
#视频软件
# 下载完成之后 转转转 正在检测当前文件是否可用
# 这是一个损坏的文件 不能使用
import os
import hashlib
def file_md5(path):
filesize = os.path.getsize(path)
md5 = hashlib.md5()
with open(path,'rb') as f:
while filesize >= 4096:
content = f.read(4096)
md5.update(content)
filesize -= 4096
else:
content = f.read(filesize)
if content:
md5.update(content)
return md5.hexdigest()
def cmp_file(path1,path2):
return file_md5(path1) == file_md5(path2)
path1 = r'D:\s20\day18\视频\4.面向对象整理.mp4'
path2 = r'D:\s20\day18\视频\tmp.mp4'
ret = cmp_file(path1,path2)
print(ret)
import hashlib
def Check_Diff(file_pathI,file_pathII):
with open(file_pathI,mode='r',encoding='utf-8') as f1,\
open(file_pathII,mode='r',encoding='utf-8') as f2:
for lineI,lineII in zip(f1,f2):
SHAI = hashlib.sha256(lineI.encode('utf-8'))
SHAII = hashlib.sha256(lineII.encode('utf-8'))
if SHAI.hexdigest() != SHAII.hexdigest():
return '这两个文件不是同一个文件'
else:
return '这两个文件是同一个文件'
ret = Check_Diff('list.txt','list.txt')
print(ret)
hashlib做一个密文存储密码的注册登陆程序
import hashlib
def md5(username,password):
md5 = hashlib.md5(username[::-1].encode('utf-8'))
md5.update(password.encode('utf-8'))
return md5.hexdigest()
def get_line():
with open('userinfo', encoding='utf-8') as
for line in f:
user, pwd = line.strip().split(',')
yield user,pwd
def register():
flag = True
while flag:
username = input('user :')
password = input('passwd :')
for user,pwd in get_line():
if user == username:
print('您输入的用户名已经存在')
break
else:
flag = False
password = md5(username,password)
with open('userinfo',encoding='utf-8',mode='a') as f:
f.write('%s,%s\n'%(username,password))
def login():
username = input('user :')
password = input('passwd :')
for user,pwd in get_line():
if username == user and pwd == md5(username,password):
return True
ret = login()
if ret:
print('登陆成功')
import hashlib
def encrypt_action(username,password,action='login',encrypt_file='encrypt_file.txt'):
with open(encrypt_file,mode='a+',encoding='utf-8') as file:
userencrypt = hashlib.sha256(username.encode('utf-8'))
userencrypt.update(password.encode('utf-8'))
if action == 'register':
file.seek(0,0)
for line in file:
if line.strip() == userencrypt.hexdigest():
return '账号已经注册,请更换其他账号登陆.'
else:
file.write(str(userencrypt.hexdigest())+'\n')
return '用户注册成功'
elif action == 'login':
file.seek(0,0)
for line in file:
if line.strip() == userencrypt.hexdigest():
return '用户登陆成功'
else:
return '用户名或密码不对.'
else:
return 'Warning'
menu = '加密注册登陆程序'.center(20,'-') + '\n' +'''
1. 登陆系统
2. 注册账户
3. 退出程序
'''
exit_flag = 'False'
while exit_flag == 'False':
print(menu)
Choice = input('>>>').strip()
if Choice == '1':
username,password = input('UserName:').strip(),input('Password:').strip()
ret = encrypt_action(username,password)
print(ret)
elif Choice == '2':
username,password = input('UserName:').strip(),input('Password:').strip()
ret = encrypt_action(username,password,'register')
print(ret)
else:
exit_flag = 'True'
print('\n')
拼手气发红包函数

#怎么能第一个人和最后一个人能取到多少钱是平均概率
import random
def red_pac(money,num):
ret = random.sample(range(1,money*100),num-1)
ret.sort()
ret.insert(0,0)
ret.append(money*100)
for i in range(len(ret)-1):
value = ret[i+1] - ret[i]
yield value/100
g = red_pac(200,10)
for i in g:
print(i)
os模块,计算文件夹的总大小
- 这个文件夹里都是文件
- 这个文件夹里还有文件夹
import os
def GetFileSize(dirpath):
count_size = 0
for file in os.listdir(dirpath):
count_size += os.path.getsize(file)
return count_size
def bytes2human(n):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
print(prefix)
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value,s)
return '%sB' % n
Size = GetFileSize('.')
ret = bytes2human(Size)
print(ret)
import os
def size(path):
num = 0
list_name = os.listdir(path)
for i in list_name:
file_name = os.path.join(path,i)
if os.path.isdir(file_name):
num1 = size(file_name)
num += num1
else:
num += os.path.getsize(file_name)
return num
print(size(os.path.abspath('.')))
计算当前月的1号的时间戳时间
import time
def MonthOne():
NowTime = time.strftime('%Y-%m',time.localtime(time.time()))
DayOne = time.strptime(NowTime+'-1','%Y-%m-%d')
return time.mktime(DayOne)
ret = MonthOne()
print(ret)
正则表达式练习
- 匹配整数或者小数(包括正数和负数)
- 匹配年月日日期 格式2018-12-6
- 匹配qq号
- 长度为8-10位的用户密码 : 包含数字字母下划线
- 匹配验证码:4位数字字母组成的
- 匹配邮箱地址
1. -?\d+(\.\d+)?
2. \d{1,4}-(1[0-2]|0?[1-9])-(3[01]|[12]\d|0?[1-9])
3. [1-9]\d{4,11}
4. \w{8,10}
5. [\dA-Za-z]{4}
6. [\w\-\.]+@([a-zA-Z\d\-]\.)+[a-zA-Z\d]{2,6}
Day16作业及默写的更多相关文章
- Day29作业及默写
作业: 1\ 默写 黏包协议 2\ 上传大文件(文件\视频\图片) 3\ 和你的同桌调通 从你的计算机上传一个视频到你同桌的电脑上 4\ 进阶 : 带上登录 Server #Server #!/usr ...
- Day20作业及默写
1.请使用C3算法计算出链接图中的继承顺序-Link 一 graph BT id1[A]-->id2[B] id2[B]-->id6[F] id6[F]-->id7[G] id1[A ...
- Day11作业及默写
1.写函数,传入n个数,返回字典{'max':最大值,'min':最小值} 例如:min_max(2,5,7,8,4) 返回:{'max':8,'min':2}(此题用到max(),min()内置函数 ...
- Day10作业及默写
1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参) def func(*number): sum=0 for num in number: sum+=nu ...
- Day19作业及默写
三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车 ...
- Day14作业及默写
1.整理今天所学内容,整理知识点,整理博客. pass 2.画好流程图. pass 3.都完成的做一下作业(下面题都是用内置函数或者和匿名函数结合做出): pass 4.用map来处理字符串列表,把列 ...
- Day13作业及默写
1. 整理今天的博客,写课上代码,整理流程图. 博客链接--博客园 2. 写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用 ...
- Day9作业及默写
1,整理函数相关知识点,写博客. 2,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def func(obj): return obj[1::2] 3, ...
- Day8作业及默写
1,有如下文件,a1.txt,里面的内容为: 老男孩是最好的培训机构, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: 将原文件全部读出来并打印. with ...
随机推荐
- spring boot(二)web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它特 ...
- cf-914D-线段树
http://codeforces.com/contest/914/problem/D 题目大意是给出一个数列,进行两种操作,一个是将位置i的数置为x,另一个操作是询问[l,r]内的数的gcd是不是x ...
- docker 系列之 docker安装
Docker支持以下的CentOS版本 CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发行版本中的内核支持 Docker. ...
- 线程池 execute 和 submit 的区别
代码示例: public class ThreadPool_Test { public static void main(String[] args) throws InterruptedExcept ...
- Python3 configparser值为多行时配置文件书写格式
一.说明 一般而言ini配置文件键值对都是一行就完事了,但有时候我们想配置的值就是由多行组成,这里说明此时配置格式该如何书写. 二.书写格式 如果值为多行,那么在第一行外的后续所有行前加入至少一个空格 ...
- Windows设置.txt文件默认打开程序
一.配置某个程序默认打开哪些类型的文件(以firefox为例) 依次打开”控制面板\程序\默认程序“,点击”设置默认程序“ 在右侧列表找到firefox,选中 以firefox为例,”将此程序设置为默 ...
- AvalonJS+MVVM实战部分源码
轻量级前端MVVM框架avalon,它兼容到 IE6 (其他MVVM框架,KnockoutJS(IE6), AngularJS(IE9), EmberJS(IE8), WinJS(IE9) ),它可以 ...
- echo * 打印当前目录列表
所以在脚本中 类似 echo $a* 如果$a为空 则会打印 目录列表.
- Linux系统从零到高手的进阶心得
初次了解到Linux系统还是在我初中的时候,那时候正是在一个中二年龄,喜欢看小说,对于小说中出现的明显的非现实场景感到十分钦佩.羡慕,并常常幻想自己也有小说主人公那样的本领.那正是在这样一个充满幻想的 ...
- js两种打开新窗口
1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...