Python Strange Issue Using File in Folder

41 Views Asked by At

I have a simple login script I am making for education.

def login():
    user = input("Username: ")
    paw = getpass.getpass(prompt="Password: ")
    f = open(r'usera.txt', "r")
    for line in f.readlines():
        us, pw = line.strip().split("|", 1)
        if (user in us) and (paw in pw):
            print("Login Successful")
            return True

def menu():
    print("Alex it worked")


def main():
    log = login()
    if log == True:
         menu()

Does not work it says usera.txt does not exist in the folder path, although I have my main.py and usera.txt in the same folder, but making my code like this works, why?

def login():
    user = input("Username: ")
    paw = getpass.getpass(prompt="Password: ")
    f = open(r'C:\Users\alex\pycharm\sandboxes\sandboxes\usera.txt', "r")
    for line in f.readlines():
        us, pw = line.strip().split("|", 1)
        if (user in us) and (paw in pw):
            print("Login Successful")
            return True


def menu():
    print("Alex it worked")


def main():
    log = login()
    if log == True:
         menu()

Folder Structure

  • sandboxes/sandboxes.py
  • sandboxes/usera.txt

but also on another note, what Am I doing wrong in order to allow any entry to pass the login screen.

def login():
        user = input("Username: ")
        paw = getpass.getpass(prompt="Password: ")
        f = open(r'usera.txt', "r")
        for line in f.readlines():
            us, pw = line.strip().split("|", 1)
            if (user in us) and (paw in pw):
                print("Login Successful")
                return True

    def menu():
        print("Alex it worked")


    def main():
        log = login()
        if log == True:
             menu()

EDIT: Sorry I am a noob, I need better error handling and also to make an else statement for improper login. What is best way to cycle back into the start of the login function?

0

There are 0 best solutions below