1. 计算当前月的1号的时间戳时间
import time def first_day(): str_t = time.strftime('%Y-%m') #得到本月格式化字符串时间 2019-03 tup_t = time.strptime(str_t,'%Y-%m') #得到结构化时间元组(struct_time) time.struct_time(tm_year=2019, tm_mon=3, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=60, tm_isdst=-1) stamp_t = time.mktime(tup_t) #得到本月第一天时间戳时间 1551369600.0 return stamp_t f = first_day() print(f)
当前年月日的凌晨12点对应的时间戳时间是多少?
当前年月日的凌晨12点对应现在的时间差了多少秒?
2.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 register(): #注册 flag = True while flag: username = input('user :') password = input('passwd :') with open('day162',encoding='utf-8')as f: for line in f: user ,pwd = line.strip().split(',') if user == username: #判断注册的用户名是否已经存在 print('您输入的用户名已经存在') break #如果存在跳出for循环 停止文件的读 else: #用户名不存在,停止while循环 flag = False password = md5(username,password) with open('day162',encoding='utf-8',mode='a') as f: f.write('%s,%s '% (username,password)) #将用户名和加盐的密码密文写入文件 register()
#将读取文件拿出来,加入了生成器 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('day162', encoding='utf-8')as f: 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 #如果存在跳出for循环 停止文件的读 else: #用户名不存在,停止while循环 flag = False password = md5(username,password) with open('day162',encoding='utf-8',mode='a') as f: f.write('%s,%s '% (username,password)) #将用户名和加盐的密码密文写入文件 register()
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('day162', encoding='utf-8')as f: 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 #如果存在跳出for循环 停止文件的读 else: #用户名不存在,停止while循环 flag = False password = md5(username,password) with open('day162',encoding='utf-8',mode='a') as f: f.write('%s,%s '% (username,password)) #将用户名和加盐的密码密文写入文件 register() #调用注册函数 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 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('day162', encoding='utf-8')as f: 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 #如果存在跳出for循环 停止文件的读 else: #用户名不存在,停止while循环 flag = False password = md5(username,password) with open('day162',encoding='utf-8',mode='a') as f: f.write('%s,%s '% (username,password)) #将用户名和加盐的密码密文写入文件 register() #调用注册函数 def login(): count = 3 while count > 0: count -=1 username = input('user :') password = input('passwd :') for user,pwd in get_line(): if username == user and pwd == md5(username,password): return True else: print('用户名或密码错误') else: print('三次登陆失败,退出程序') ret = login() if ret: print('登陆成功 ')
3.校验大文件的一致性
一个大文件 进行多次校验
import os import hashlib path = r'G:feiqRecv Filesday18课上视频作业题讲解.mp4' filesize = os.path.getsize(path) #获得文件大小,字节的形式 md5 = hashlib.md5() with open(path,'rb')as f: #rb读取文件 while filesize >= 4096: content = f.read(4096) #4096字节读取 md5.update(content) #4096字节加密 filesize -= 4096 else: content = f.read(filesize) filesize = 0 if content: md5.update(content) print(md5.hexdigest()) #0786280d6b501d8b6b1861d100047d42
import os import hashlib def file_md5(path): filesize = os.path.getsize(path) #获得文件大小,字节的形式 md5 = hashlib.md5() with open(path,'rb')as f: #rb读取文件 while filesize >= 4096: content = f.read(4096) #4096字节读取 md5.update(content) #4096字节加密 filesize -= 4096 else: content = f.read(filesize) if content: md5.update(content) return md5.hexdigest() #0786280d6b501d8b6b1861d100047d42 def cmp_file(path1,path2): return file_md5(path1) == file_md5(path2) path1 = r'G:feiqRecv Filesday18课上视频作业题讲解.mp4' path2 = r'G:feiqRecv Filesday18课上视频 mp.mp4' ret = cmp_file(path1,path2) print(ret)
4.发红包
让第一个人和最后一个人能取到多少钱是相同概率
import random def red_pac(money,num): ret = random.sample(range(1,money*100 ),num-1) #金额*100 ,按分取值,先取num-1个值 ret.sort() #排序,避免前面数值大于后面数值,造成发红包金额为负 ret.insert(0,0) #最前边添加0 ret.append(money*100) #最后面金额为money*100 for i in range(len(ret)-1): #取索引 value = ret[i+1] - ret[i] #红包金额为随机取出的后面的值减去前面的值 print(value/100) #除以100取金额 red_pac(200,10)
5. os模块 计算文件夹的总大小
1.这个文件夹里都是文件
2.这个文件夹里还有文件夹
import os def get_size(path): size = 0 l = [path] while l: path = l.pop() lst = os.listdir(path) for name in lst: son_path = os.path.join(path,name) if os.path.isfile(son_path): size += os.path.getsize(son_path) else: l.append(son_path) return size size = get_size(r'G:feiqRecv Files') print(size) # 12925942411 (12,925,942,411 字节)