Home > database >  How to import from a file a class that will later be defined
How to import from a file a class that will later be defined

Time:08-24

I'm working on a simple "sign in " "sign up" program, and the idea is that the user inputs a username and a password and they get saved in a python file named Database. And the sign in process works the same, but I'm stuck in the step where the program should import a class named after the username that has the password in it, and to import this class the user should input its name (username) so at first it will be just a variable and later it will be defined. I'm still a beginner in python and this is my idea as a code:

#This is the file named Database
class Username:
  password = ["Mypassword"]
#This is the sign in program

user = input("enter your username:\n")

with open('Database.py') as file:
    content = file.read()

if user in content:
    from Database import user
else:
    print("this username is not in the database")

in the console it shows that "user" is not defined because it's a variable, so is there any other way to achieve the same goal with another function or something. This is the best I can think of as a beginner perhaps if someone has a better idea let me know please.

CodePudding user response:

I suggest having class User, which saves name and password in a txt file once the object has been created. It opens .txt file, adds a line, and closes it. After that in the main function you can open the txt file and iterate through as many times as you want and check if it contains a certain user. Hope it helps.

CodePudding user response:

So if i understand correctly there is no user in the Database file. There are one way of solving the problem that i can think of learn some pandas(its module) and create a table with unsernames and passwords(its gona be 10 times easier than what you trying to do)

AND

As @jokkk2312 dont read a python file you can create a text file that will save the username and password as one(e.x username:panos password:123 in the file save it as one string: panos123 and put \n so it changes row every time.) After that make a program that ask you for your user name and password and combines them to a single string. You take than string and search the hole file for the same string....

If you want me to create a code example add a comment to this post and i will make one...

  • Related