总结
# hash摘要(加密)的算法主要有两类:md5和sha-X系列
# sha-X比md5得到密文的摘要长
# hash加密的特点
'''
1) hash算法生成的摘要不可逆(不能通过密文来获取原文)
2) 只有相同的数据通过相同算法得到的摘要才是一致的
3) 不管原数据多大产生的摘要的长度是一致的.(无法通过摘要来确认密文长度)
'''
# 2.生成hashlib摘要
# 1) 通过算法创建hash对象:hash.md5()
import hashlib
hash = hashlib.md5() # 所有的hash算法里长度最短的,保存密文的时候需要的内存最小,因此一般使用md5加密密文.
# 算法名:md5,shaXXX
# 2)添加数据
# hash对象.update(二进制数据)
# 数据必须是二进制数据,类型是bytes
# 补充:二进制和字符串之间的相互转换:
'''
字符串转二进制:(编码)
bytes(字符串)
# bytes('123456',encoding='utf-8')
字符串.encode()
二进制转字符串(解码)
str(二进制,encoding='utf-8')
二进制.decode()
'''
# hash.update(bytes('123456',encoding='utf-8'))
hash.update(bytes('123456', encoding='utf-8')) # 数据类型需是二进制数据
# 3)获取摘要
result = hash.hexdigest() # 获取十六进制摘要
print(result) # e10adc3949ba59abbe56e057f20f883e
# 练习:判断wendang.text是否被修改,对比生成的密文是否一致
hash = hashlib.md5()
with open('wendang.text', 'rb') as f:
hash.update(f.read())
print(hash.hexdigest())
# 数据持久化
'''
1) 程序中保存的数据默认保存在运行内存中的,程序运行结束后会被释放
2) 如果希望将这次运行程序产生的数据在下次程序运行的时候还能用,就需要将数据保存到硬盘中(磁盘中)
3) 将数据保存到磁盘或者硬盘的过程就是数据持久化的过程
4) 硬盘保存数据的基本单元是文件,所以如果要讲数据保存到硬盘中,只需要将数据保存在文件中就可以了
5) 程序中数据持久化的常用文件类型:数据库文件(.db、.sqlite等),plist文件,json文件,txt文件,
二进制文件(图片,视频,音频,exe可执行文件)
'''
# 文件操作 -- 操作文件内容(针对所有的文件都有效)
'''
文件操作的基本流程:打开文件 --> 操作文件(读,写) --> 关闭文件
'''
# 1) 打开文件
'''
open(file,mode='r',*,encoding=None) -- 以指定方式打开指定文件,返回一个文件对象
file - 需要打开的文件在计算机中的位置信息(简称文件路径), 字符串
文件路径:绝对路径:文件在计算机里面的全路径(一般从盘开始写)
在桌面搜索中搜索cmd,会打开命令提示符把文件拖到光标上,就会直接显示文件的绝对路径
相对路径:. - 表示当前目录(当前代码文件所在的文件夹)
.. - 表示当前目录的上层目录
... - 表示当前目录的上层目录的上层目录
mode - 文件打开方式,决定打开文件后后续能对该文件进行的操作以及操作数据的类型
第一组:控制后续操作(是读还是写):'r'.'w'.'a'
第二组:控制操作数据的类型:'t'-文本.'b'-二进制
第一组必须选一个,第二组可以不选,不选时默认't'
'r' - 只读
'w' - 只写:打开就会清空原文件内容,只保留新内容
'a' - 只写:保留原原文件内容,添加新内容
't' - 读操作返回的数据和写操作返回的数据是字符串(str)
'b' - 读操作和写操作写入的数据时二进制(bytes)
注意: 二进制打开的时候只能用'b'打开
encoding - 文本文件的编码方式(需要保证写入数据采用的是什么方式进行编码,读数据的时候就需要用对应的方式解码)
一般采用'utf-8',但是很多Windows文本默认方式是gbk
注意:只有在以't'的方式打开文本文件时,才需要考虑给encoding赋值
'''
# =====================绝对路径========================
# open(r'C:\Users\lxh\Desktop\总结.md')
# =====================相对路径========================
# open(r'.\总结.md')
# open('总结.md')
# open(r'..\day14-异常捕获和文件操作\res\a.txt') # 相对路径.包里的文件
# ======================r是只读========================
# f = open('总结.md', 'r')
# f.read()
# f.write('123') # 报错:io.UnsupportedOperation: not writable
# ======================a是只写========================
# a = open('总结.md', 'a')
# a.write('123')
# a.read() # io.UnsupportedOperation: not readable
# ======================w是只写========================
# f = open(r'..\day14-异常捕获和文件操作\res\a.txt', 'w')
# f.write('abx')
# f.read() # io.UnsupportedOperation: not readable
# =========='t'读操作返回的数据和写操作返回的数据是字符串(str)=========
# f = open(r'..\day14-异常捕获和文件操作\res\a.txt', 'rt', encoding='utf-8')
# content = f.read()
# print(type(content))
# 3) 关闭文件
# 文件对象.close()
# f.close()
# f.read() ValueError: I/O operation on closed file.
# 3)操作
# 读操作
'''
文件对象.read() -- 从读写位置开始,读到文件结束(读写位置默认在文件开头)
文件对象.readline() -- 只针对文本文件,从读写位置开始,读到当行结束
文件对象.readlines() -- 读全部的文件
'''
f = open(r'..\day14-异常捕获和文件操作\res\a.txt', encoding='utf-8')
print('===========================1===============================')
print(f.read())
print('===========================2===============================')
print(f.read()) # 上次读到文件结束,从文件结束读到文件结束,所以是空的
print('===========================3===============================')
f.seek(0) # 将光标移到文件开头
print(f.read())
print('===========================3===============================')
f.seek(0)
print(f.readline())
print('===========================4===============================')
f.seek(0)
print(f.readlines()) # ['床前明月光\n', '疑似地上霜\n', '举头望明月\n', '低头思故乡']
# 写操作
# 文件对象.write(数据) -- 将数据写入指定文件中
f = open(r'..\day14-异常捕获和文件操作\res\a.txt', 'a',encoding='utf-8')
f.write('\n你是谁?') # \n 换行
# 怎么做到数据持久化
'''
1) 用一个文件来保存需要持久化的数据
2) 程序汇总需要这个数据的时候就从文件中读取这个数据
3) 如果数据发生改变需要把最新的数据再写入文件中
'''
# 练习:写一个程序打印程序执行的次数
f = open('document.txt', encoding='utf-8')
count = int(f.read())
count += 1
print(count)
f = open('document.txt', 'w', encoding='utf-8')
f.write(str(count))
# 2.eval() - 把字符串转换成其他类型(字符串里的数据时什么类型对应就转换成什么类型)的数据
result = eval('100')
print(result, type(result))
result = eval('[1, 2, 3, 4]')
print(result, result[-1], type(result))
import time
import hashlib
def input_message():
global user_name_1
user_name_1 = input(‘请输入用户名:’)
hash = hashlib.md5()
hash.update(bytes(user_name_1, encoding=‘utf-8’))
global result_user_name_1
result_user_name_1 = hash.hexdigest()
global password_1
password_1 = input(‘请输入密码:’)
hash = hashlib.md5()
hash.update(bytes(password_1, encoding=‘utf-8’))
global result_password_1
result_password_1 = hash.hexdigest()
global open_1
open_1 = open(‘01.txt’, ‘rt’, encoding=‘utf-8’)
global f
f = eval(open_1.read())
open_1.close()
while True:
def input_data():
print(’----------------------------------------------’)
print(’ 学生管理系统 ‘)
print(’ ❀ 1.登录 ‘)
print(’ ❀ 2.注册 ‘)
print(’ ❀ 3.退出 ‘)
print(’----------------------------------------------’)
global option_1
option_1 = int(input(‘请选择:’))
作业:
import time
import hashlib
def input_message():
global user_name_1
user_name_1 = input('请输入用户名:')
hash = hashlib.md5()
hash.update(bytes(user_name_1, encoding='utf-8'))
global result_user_name_1
result_user_name_1 = hash.hexdigest()
global password_1
password_1 = input('请输入密码:')
hash = hashlib.md5()
hash.update(bytes(password_1, encoding='utf-8'))
global result_password_1
result_password_1 = hash.hexdigest()
global open_1
open_1 = open('01.txt', 'rt', encoding='utf-8')
global f
f = eval(open_1.read())
open_1.close()
while True:
def input_data():
print('----------------------------------------------')
print(' 学生管理系统 ')
print(' ❀ 1.登录 ')
print(' ❀ 2.注册 ')
print(' ❀ 3.退出 ')
print('----------------------------------------------')
global option_1
option_1 = int(input('请选择:'))
input_data()
# 开始注册登录名
if option_1 == 2:
input_message()
for user in f:
if result_user_name_1 == user['name:']:
print('注册失败!该账号之前已经注册过就注册')
time.sleep(2)
break
else:
print('注册成功')
time.sleep(2)
open_2 = open('01.txt', 'wt', encoding='utf-8')
f.append(dict([('name:', result_user_name_1), ('pwd:', result_password_1)]))
open_2.write(str(f))
open_2.close()
elif option_1 == 1:
input_message()
for user in f:
if result_user_name_1 == user['name:'] and result_password_1 == user['pwd:']:
print('登录成功')
time.sleep(2)
break
else:
print('登录失败')
time.sleep(2)
break
elif option_1 == 3:
print('退出')
time.sleep(2)
break