用户登陆
#!/usr/bin/env python # -*- coding:utf-8 -*- #删除用户,修改密码 def login(username, password): """ 用户登录验证 :param username: 用户名 :param password: 密码 :return: True成功,False失败 """ f = open('log', 'r', encoding='utf-8') for line in f: line = line.strip() userlist = line.split('$') if username == userlist[0] and password == userlist[1]: return True return False def user_ex(username): """ 用于检测用户是否存在 :param username: 用户名 :return: True 表示存在,False用户不存在 """ with open('log','r',encoding='utf-8') as f: for line in f: line = line.strip() userlist = line.split('$') if username in userlist: return True return False def res(username, password): """ 用于用户注册 :param username: 用户名 :param password: 密码 :return: True注册成功 """ with open('log','a',encoding='utf-8') as f: temp = "\n" + username + "$" + password f.write(temp) return True def change_pwd(username, password): """ 用户修改密码 :param username: 用户名 :param password: 修改后的密码 :return: True修改成功,False修改失败 """ file_object = open('log') try: lines=open('log','r').readlines() for i in range(len(lines)): if username in lines[i]: test=lines[i].split('$')[1] lines[i]=lines[i].replace(test,password) open('log','w').writelines(lines) return True return False finally: file_object.close( ) def del_user(username): """ 删除用户 :param username: 要删除用户名 :return: true 删除成功,False用户名不在列表中 """ file_object = open('log') try: lines = open('log', 'r').readlines() for i in range(len(lines)): if username in lines[i]: del lines[i] print(lines) open('log','w').writelines(lines) return True else: return False finally: file_object.close() # def del_user(username): # with open('log', 'r') as f: # for line in f: # if re.match("lisi", line): # pass # else: # data = line # with open('log', 'w+') as f: # f.write(data) # return True # return False def main(): inp = input("1:登录;2:注册;3:删除用户;4:修改密码") user = input("请输入用户名:") pwd = input("请输入密码:") if inp == "1": ret = login(user, pwd) if ret: print("登录成功") else: print("登录失败") elif inp == "2": is_ex = user_ex(user) if is_ex: print("用户已存在,无法注册") else: resgister = res(user, pwd) if resgister: print("用户已成功注册") else: print("注册失败") elif inp == "3": del_us = del_user(user) if del_us: print("已删除") else: print("无此用户") elif inp == "4": is_chage = change_pwd(user, pwd) if is_chage: print("修改成功") else: print("无此用户") main()
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 5 def login(username,password): 6 ''' 7 登陆 8 :param username:登录名 9 :param password:登陆密码 10 :return:登陆成功返回True,登陆错误返回False 11 ''' 12 13 14 with open('log','r',encoding='utf-8') as f: 15 for line in f: 16 line=line.strip() 17 line_list=line.split(';') 18 if username==line_list[0] and password==line_list[1]: 19 return True 20 return False 21 22 def exist(username,password): 23 ''' 24 检测用户名是否存在 25 :param username:用户名 26 :param password:密码 27 :return:存在返回True,不存在返回False 28 ''' 29 30 with open('log','r',encoding='utf-8') as f: 31 for line in f: 32 line=line.strip() 33 line_list=line.split(';') 34 if username==line_list[0]: 35 return True 36 return False 37 38 39 def register(username,password): 40 ''' 41 注册 42 :param username:用户名 43 :param password:用户密码 44 :return:注册成功返回True,不成功返回False 45 ''' 46 47 48 try: 49 with open('log','a',encoding='utf-8') as f: 50 new='\n'+username+';'+password 51 f.write(new) 52 return True 53 except: 54 return False 55 56 def change(username,password): 57 ''' 58 修改密码 59 :param username:用户名 60 :param password:用户密码 61 :return:成功返回True,失败返回False 62 ''' 63 64 try: 65 with open('log','r+',encoding='utf-8') as f,open('t','w',encoding='utf-8') as f1: 66 a=f.readlines() 67 for i in a: 68 b=i.strip().split(';') 69 if username==b[0] and password==b[1]: 70 b[1]=input('newpwd:') 71 print(b) 72 i1=';'.join(b)+'\n' 73 # print(i1) 74 a[a.index(i)]=i1 #通过索引赋值 75 f1.writelines(a) 76 return True 77 78 79 except: 80 return False 81 82 83 def del_name(username,password): 84 ''' 85 删除用户 86 :param username: 87 :param password: 88 :return: 89 ''' 90 try: 91 with open('log','r+',encoding='utf-8') as f,open('t','w',encoding='utf-8') as f1: 92 a=f.readlines() 93 for i in a: 94 b=i.strip().split(';') 95 if username==b[0] and password==b[1]: 96 del a[a.index(i)] #通过索引赋值 97 f1.writelines(a) 98 return True 99 except: 100 return False 101 pass 102 103 def main(): 104 ''' 105 定义两个文件log(源文件)、t(new文件) 106 :return: 107 ''' 108 inp=input('1,deng;2,zhu;3,del;4,xchange') #1.登陆2.注册3.删除用户4.修改密码 109 usr=input('yonghu:') 110 pwd=input('mi:') 111 if inp=='1': 112 l=login(usr,pwd) 113 if l: 114 print('d ok') 115 else: 116 print('d no') 117 118 elif inp=='2': 119 if exist(usr,pwd): 120 print('cunzai') 121 else: 122 r=register(usr,pwd) 123 if r: 124 print('z ok') 125 else: 126 print('z no') 127 128 elif inp=='3': 129 if del_name(usr,pwd): 130 print('del ok') 131 else: 132 print('del no') 133 134 elif inp=='4': 135 if change(usr,pwd): 136 print('change ok') 137 else: 138 print('change no') 139 140 main()

浙公网安备 33010602011771号