'Register account feature in Python app returns "FileNotFoundError"
When you run this python file and then register an account, it keeps giving me:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'D:\\\\\___\accounts\accounts.txt'
How can I fix this? I'm new to Python. I learnt the files through tutorials.
Here's the code:
import csv
import os
import time
from colorama import Fore
#======================================================================#
clearConsole= lambda: os.system("cls")
#======================================================================#
def register():
firstname = input(Fore.CYAN + "Enter your first name: " + Fore.WHITE).lower()
print(Fore.GREEN + "First name set to: " + Fore.LIGHTGREEN_EX + firstname)
surname = input(Fore.CYAN + "Enter your surname: " + Fore.WHITE).lower()
print(Fore.GREEN + "Surname set to: " + Fore.LIGHTGREEN_EX + surname)
age = input(Fore.CYAN + "Enter your age: " + Fore.WHITE)
if not str.isdecimal(age):
print(Fore.RED + "Age must be a number. Please try again.")
age = input(Fore.CYAN + "Enter your age: " + Fore.WHITE)
print(Fore.GREEN + "Age set to: " + Fore.LIGHTGREEN_EX + age)
password = input(Fore.CYAN + "Enter a strong password: " + Fore.WHITE)
if len(password) < 6:
print(Fore.RED + "Password should be between 6 to 18 characters long.")
password = input(Fore.CYAN + "Enter a strong password: " + Fore.WHITE)
print(Fore.GREEN + "Password is: " + Fore.LIGHTGREEN_EX + password)
username = firstname[:1] + surname + age
username.lower()
print(Fore.GREEN + "Your username is: " + Fore.LIGHTGREEN_EX + username + Fore.GREEN + "\nYour password is: " + Fore.LIGHTGREEN_EX + password + Fore.GREEN + "\nYour firstname is: " + Fore.LIGHTGREEN_EX + firstname + Fore.GREEN + "\nYour surname is: " + Fore.LIGHTGREEN_EX + surname + Fore.GREEN + "\nYour age is: " + Fore.LIGHTGREEN_EX + age)
time.sleep(3)
global encryptedUsername, encryptedPassword, encryptedFirstname, encryptedSuraname, encryptedAge
encryptedUsername = "".join(chr(ord(char)+3) for char in username)
encryptedPassword = "".join(chr(ord(char)+3) for char in password)
encryptedFirstname = "".join(chr(ord(char)+3) for char in firstname)
encryptedSuraname = "".join(chr(ord(char)+3) for char in surname)
encryptedAge = "".join(chr(ord(char)+3) for char in age)
if not str.isdecimal(age) or len(password) < 6:
clearConsoleMessageAndTimer(Fore.RED + "Error occured, please try again.", 3)
loginMenu()
else:
cwd = os.getcwd()
accountsFolder = "accounts"
accountFile = "accounts.txt"
fileAccDirect = os.path.join(cwd, accountsFolder, accountFile)
directoryExists = os.path.exists(fileAccDirect)
if directoryExists == False:
os.mkdir(fileAccDirect)
with open(f"{fileAccDirect}", "a", newline = "") as myFile:
writer = csv.writer(myFile)
writer.writerow([encryptedUsername, encryptedPassword, encryptedFirstname, encryptedSuraname, encryptedAge])
myFile.close()
clearConsoleMessageAndTimer(Fore.LIGHTGREEN_EX + "Account successfully made. Please login now.", 3)
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
loginMenu()
elif directoryExists == True:
with open(f"{fileAccDirect}", "a", newline = "") as myFile:
writer = csv.writer(myFile)
writer.writerow([encryptedUsername, encryptedPassword, encryptedFirstname, encryptedSuraname, encryptedAge])
myFile.close()
clearConsoleMessageAndTimer(Fore.LIGHTGREEN_EX + "Account successfully made. Please login now.", 3)
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
loginMenu()
#======================================================================#
def databaseOptionMenu():
print(Fore.WHITE + "#========================================# #==============>>")
print("|" + Fore.LIGHTBLUE_EX + " Please select what you want to do. " + Fore.WHITE + "| |" + Fore.LIGHTMAGENTA_EX + " Logged in as:")
print(Fore.WHITE + "| | | " + Fore.MAGENTA + f"{user0}" + Fore.WHITE)
print("|" + Fore.BLUE + " Press 1 to create a table "+ Fore.WHITE + "| #==============>>")
print("|" + Fore.BLUE + " Press 2 to Load table " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 3 to Add Student " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 4 to Remove Student " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 5 to Search Student " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 6 to see account details " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 7 to Log out " + Fore.WHITE + "|")
print("|" + Fore.LIGHTBLACK_EX + " Press 8 to exit the program " + Fore.WHITE + "|")
print("#========================================#")
selection2 = input(Fore.GREEN + f"[{user0}] >>> " + Fore.WHITE)
if selection2 == "7":
clearConsoleMessageAndTimer(Fore.WHITE + "Logging out...", 2)
loginMenu()
elif selection2 == "6":
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
cwd = os.getcwd()
file = open(f"{cwd}\\accounts\\accounts.txt", "r")
detailsList = []
for line in file:
detailsList.append(line.split(","))
for details in detailsList:
encryptedUsername = "".join(chr(ord(char)+3) for char in user0)
encryptedPassword = "".join(chr(ord(char)+3) for char in pass0)
if encryptedUsername == details[0] and encryptedPassword == details[1]:
decryptedUsername = "".join(chr(ord(char)-3) for char in details[0])
decryptedPassword = "".join(chr(ord(char)-3) for char in details[1])
decryptedFirstname = "".join(chr(ord(char)-3) for char in details[2])
decryptedSurname = "".join(chr(ord(char)-3) for char in details[3])
decryptedAge = "".join(chr(ord(char)-3) for char in details[4])
print(Fore.WHITE + "#=========================>>")
print("|" + Fore.BLUE + "Username: " + Fore.LIGHTBLUE_EX + f"{decryptedUsername}")
print(Fore.WHITE + "#=========================>>")
print("|" + Fore.BLUE + "Password: " + Fore.LIGHTBLUE_EX + f"{decryptedPassword}")
print(Fore.WHITE + "#=========================>>")
print("|" + Fore.BLUE + "Firstname: " + Fore.LIGHTBLUE_EX + f"{decryptedFirstname}")
print(Fore.WHITE + "#=========================>>")
print("|" + Fore.BLUE + "Surname: " + Fore.LIGHTBLUE_EX + f"{decryptedSurname}")
print(Fore.WHITE + "#=========================>>")
print("|" + Fore.BLUE + "Age: " + Fore.LIGHTBLUE_EX + f"{decryptedAge}")
print(Fore.WHITE + "#=========================>>")
print(Fore.LIGHTBLACK_EX + "\nPress enter to go back...")
selection1 = input(Fore.GREEN + "[Info] >>> " + Fore.WHITE)
if not selection == "":
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
databaseOptionMenu()
elif selection2 == "5":
clearConsoleMessageAndTimer(Fore.YELLOW + "Still coding...", 2)
databaseOptionMenu()
elif selection2 == "4":
clearConsoleMessageAndTimer(Fore.YELLOW + "Still coding...", 2)
databaseOptionMenu()
elif selection2 == "3":
clearConsoleMessageAndTimer(Fore.YELLOW + "Still coding...", 2)
databaseOptionMenu()
elif selection2 == "2":
clearConsoleMessageAndTimer(Fore.YELLOW + "Still coding...", 2)
databaseOptionMenu()
elif selection2 == "1":
clearConsoleMessageAndTimer(Fore.YELLOW + "Still coding...", 2)
databaseOptionMenu()
if selection2 == "8":
clearConsoleMessageAndTimer(Fore.RED + "Exiting program..." + Fore.RESET, 2)
exit()
elif not selection == "[1-8]":
clearConsoleMessageAndTimer(Fore.RED + "Please select from 1 to 8.", 2)
databaseOptionMenu()
#======================================================================#
def login():
global user0
user0 = input(Fore.CYAN + "Username: " + Fore.WHITE).lower()
global pass0
pass0 = input(Fore.CYAN + "Password: " + Fore.WHITE)
encryptedUsername = "".join(chr(ord(char)+3) for char in user0)
encryptedPassword = "".join(chr(ord(char)+3) for char in pass0)
try:
cwd = os.getcwd()
file = open(f"{cwd}\\accounts\\accounts.txt", "r")
except:
clearConsoleMessageAndTimer(Fore.RED + "Account doesn't exist. Please register an account.", 3)
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
loginMenu()
detailsList = []
line = 0
for line in file:
detailsList.append(line.split(","))
loggedIn = False
for details in detailsList:
if encryptedUsername == details[0] and encryptedPassword == details[1]:
loggedIn = True
break
if loggedIn:
clearConsoleMessageAndTimer(Fore.LIGHTGREEN_EX + "Successfully logged in.", 2)
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 1)
databaseOptionMenu()
else:
clearConsoleMessageAndTimer(Fore.RED + "Incorrect password or username. Please try again.", 2)
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 1)
loginMenu()
#======================================================================#
def loginMenu():
print(Fore.WHITE + "#===================================#")
print("|" + Fore.LIGHTBLUE_EX + " Welcome to Python Database! " + Fore.WHITE + "|")
print("| |")
print("|" + Fore.BLUE + " Press 1 to Register " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " Press 2 to Login " + Fore.WHITE + "|")
print("| |")
print("|" + Fore.BLUE + " Press 3 for help menu " + Fore.WHITE + "|")
print("| |")
print("|" + Fore.BLUE + " Press 4 to exit the program " + Fore.WHITE + "|")
print("#===================================#")
global selection
selection = input(Fore.GREEN + "[PyDatabase] >>> " + Fore.WHITE)
if selection == "1":
register()
elif selection == "2":
login()
elif selection == "3":
clearConsole()
print(Fore.WHITE + "#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" + Fore.LIGHTBLUE_EX + "Help Menu" + Fore.WHITE + "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#")
print("| |")
print("|" + Fore.BLUE + " First time using? Start by registering an account. " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " To do that you need to press 1, and enter your details " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " If your already a user then press 2 to login. " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " To view your account details when your're logged in press 5. " + Fore.WHITE + "|")
print("|" + Fore.BLUE + " To log out from your account press 6, when your're logged in. " + Fore.WHITE + "|")
print("| |")
print("|" + Fore.LIGHTBLACK_EX + " Press enter to go back " + Fore.WHITE + "|")
print("#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#")
selection1 = input(Fore.GREEN + "[Help] >>> " + Fore.WHITE)
if not selection == "":
clearConsoleMessageAndTimer(Fore.WHITE + "Loading...", 2)
loginMenu()
elif selection == "4":
clearConsoleMessageAndTimer(Fore.RED + "Exiting program..." + Fore.RESET, 2)
exit()
else:
clearConsoleMessageAndTimer(Fore.RED + "Please select from 1 to 4.", 3)
loginMenu()
#======================================================================#
def clearConsoleMessageAndTimer(message, seconds):
clearConsole()
print(message)
time.sleep(seconds)
clearConsole()
#======================================================================#
clearConsole()
loginMenu()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|