函数,博客案例,用户持久化

it2024-10-07  37

import os import sys def io_read(): with open('d:\\user\\users.txt','r') as a: users = a.read() def io_write(c): with open('d:\\user\\users.txt','w') as a: a.write(c) a.close() def io_writecsdn(name): path = 'd:\\user\\csdn\\'+'name'+'.txt' with open(path,'w') as a: a.write(c) a.close() def io_readcsdn(name): path = 'd:\\user\\csdn\\'+'name'+'.txt' with open(path,'r') as a: return a.read() def zc(): a = os.listdir('d:\\user') if 'user' in a: b = os.listdir('d:\\user\\csdn') if 'd:\\user\\csdn' not in b: os.mkdir('d:\\user\\csdn') else: break else: os.mkdir('d:\\user\\csdn') io_read() while True: print("1、用户注册\n2、用户登录\n3、退出系统") choice = input("请输入您的选择:") if choice == "1": username = input("请输入您的用户名称:") password = input("请输入您的密码:") flag = False for u in users: if u.get(username) is not None: flag = True break if flag: print("用户名称已经存在,不能注册") else: user = {username:password} print(user) print(type(user)) c = str(user) io_write(c) print("注册成功") elif choice == "2": username = input("请输入您的用户名称:") password = input("请输入您的密码:") flag = False for u in users: print(u) if u.get(username) == password: flag = True if flag: print("登录成功") print("1、发表文章\n2、修改自己发表的文章\n3、查看自己发表的文章\n4、查看所有文章\n5、退出系统") choices = input("请输入您的选择:") if choices == '1': while True: name = input('请输入发表文章的名字:') a = os.listdir('d:\\user\\csdn') if name in a: print('文章名已存在请重新输入:') else: return name io_writecsdn(name) if choices == '2': if choices == '3': a = os.listdir('d:\\user\\csdn') print(a) name = input('你要查看的文章:') b = io_readcsdn(name): print(b) if choices == '4': a = os.listdir('d:\\user\\csdn') print(a) if choices == '5': yes = input("您确定要退出系统吗(Y or y)?") if yes == "Y" or yes == "y": sys.exit() else: print("登录失败") elif choice == "3": yes = input("您确定要退出系统吗(Y or y)?") if yes == "Y" or yes == "y": sys.exit() a.close() return users def copy(a): b = open('d:\\users.txt','wb') b.write(a) b.close() while True: zc()
最新回复(0)