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')

拼手气发红包函数

Day16作业及默写-LMLPHP

#怎么能第一个人和最后一个人能取到多少钱是平均概率
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模块,计算文件夹的总大小

  1. 这个文件夹里都是文件
  2. 这个文件夹里还有文件夹
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)

正则表达式练习

  1. 匹配整数或者小数(包括正数和负数)
  2. 匹配年月日日期 格式2018-12-6
  3. 匹配qq号
  4. 长度为8-10位的用户密码 : 包含数字字母下划线
  5. 匹配验证码:4位数字字母组成的
  6. 匹配邮箱地址
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}
05-11 07:59