Advertisement
DrAungWinHtut

fileio3.py

Apr 2nd, 2023
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. def PrintPass(fname):
  2.     # w - write, + means you can read too
  3.     login_file = open(fname, 'r')
  4.     funame = login_file.readline()
  5.     fpasswd = login_file.readline()
  6.     funame = funame.rstrip('\n')
  7.     fpasswd = fpasswd.rstrip('\n')
  8.     login_file.close()
  9.     print('Username = {} \nPassword = {} \n\n'.format(funame, fpasswd))
  10.  
  11.  
  12. def Signin(uname, passwd, fname):
  13.     # w - write, + means you can read too
  14.     login_file = open(fname, 'r')
  15.     funame = login_file.readline()
  16.     fpasswd = login_file.readline()
  17.     funame = funame.rstrip('\n')
  18.     fpasswd = fpasswd.rstrip('\n')
  19.     login_file.close()
  20.  
  21.     if uname != funame or passwd != fpasswd:
  22.         return False
  23.     else:
  24.         return True
  25.  
  26.  
  27. def ResetSignin(olduname, oldpass, fname):
  28.     if not Signin(olduname, oldpass, fname):
  29.         print('Wrong username or password:')
  30.         return False  # Fail to update password
  31.  
  32.     newpass = 'u1'
  33.     newpass2 = 'u2'
  34.  
  35.     while newpass != newpass2:
  36.         newuname = input('Please Enter New Username: ')
  37.         newpass = input('Please Enter New Password: ')
  38.         newpass2 = input('Please Enter New Password again: ')
  39.         if newpass != newpass2:
  40.             print('Passwords do not match!, pls try again\n')
  41.  
  42.     login_file = open(fname, 'w')  # w to override
  43.     login_file.write(newuname+'\n')
  44.     login_file.write(newpass+'\n')
  45.     login_file.close()
  46.     return True  # Success
  47.  
  48.  
  49. # Main Start Here
  50. uname = input('Enter Username: ')
  51. passwd = input('Enter Password: ')
  52. if not Signin(uname, passwd, 'd:\\game\\login.dat'):
  53.     exit()
  54.  
  55.  
  56. ans = -1
  57. while ans != '0':
  58.     print("You are logging in....")
  59.     print()
  60.     print('1- Change Username and Password: ')
  61.     print('2- Print Username and Password: ')
  62.     print('0- Exit')
  63.     ans = input('Please choose (0,1): ')
  64.     if ans == '0':
  65.         exit()
  66.     elif ans == '1':
  67.         olduname = input('Please Enter Old username: ')
  68.         oldpass = input('Please Enter Old password: ')
  69.         status = ResetSignin(olduname, oldpass, 'd:\\game\\login.dat')
  70.         if status == True:
  71.             print('Login Reset Success!')
  72.         else:
  73.             print('Login Reset Fail!')
  74.     elif ans == '2':
  75.         PrintPass('d:\\game\\login.dat')
  76.     else:
  77.         print('Invalid choice, please choose only 0 and 1 : \n\n')
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement