Python Login System: Using a text file (Beginners Project)
HTML-код
- Опубликовано: 27 сен 2024
- This tutorial is a project you can take on as a Python beginner. It exposes you to how to use functions, loops, conditions, and a lot more in Python.
Here, we'll be collecting users' inputs into a text file, then run some checks on those inputs during registration and login. It's just how authentication or login systems work on the internet.
Note that you’d use a real database for a real-life project instead.
I’ve also updated this video to include password encryption. You can continue watching the encryption tutorial here:
• Python Login System: U...
Here's the source code: github.com/omi...
Came here as a beginner and leaving as a beginner with knowledge on how to make a simple login system.....Thanks a lot man...i really appreciate your help...
Great man!! You have become my saviour for my last minute assignment submission
I’m glad the video helped Farooz.
Thank you so much for this video! Super helpful and just made it so much easier to complete my assignment! Love to see more videos of yours!
thank you so much broski your content helped me so much in my tkinter project
Well done bro. Very useful tutorial. Thanks.
Please how do you secure the text database file so that it cannot be easily accessed by anyone?
Hi, I just updated the video to include password encryption. You can have a look here:
ruclips.net/video/BQlv5miJQdw/видео.html
@@techWithId Good job!! Thank you once again.
Loved this video. Super clear and really helpful!
Thank you very much for this video brother, it helped me a lot, I wanted to point something out tho, when it asks you if you want to “Login or Signup” if you type it with a lowercase letter it will cause an erroe
Just let your code accept lower or upper cases.
@@techWithId no but like say I wanted to make my code available to the public and they don’t k ow that they need to type in uppercase letters, it will give them an error
there are some irrelevant lines of code, which are as follows: try, except do nothing other than override what the if else statements where already doing, pass it does the same thing as not having it there, and print(data) that is bad for security reasons and you can just use the data variable in the elif statement for check weather the input username is the same as another in the text file looks like this (elif Username in data:). I would like to add that you should recall the function whenever something is incorrect in the access function, also you should just say Username does not exist and say that the password is incorrect when the username does exist so it looks something like this:
if data[Username]:
if password == data[Username]:
print("Login Successful.")
print("Welcome, " + Username)
else:
print("Password is incorrect.")
access()
else:
print("Username does not exist.")
access()
the reason why you state things in this way is that it is clearer users to identify errors and help correct them, plus it is not very helpful to tell the User that the password they had inputted doesn't exist when the programme has not checked that the inputted password is correct for the username.
Other than what is above it is a very good programme with a very good tutorial.
Thanks! Yeah, I agree printing data is bad practice. I just left it there for the tutorial purpose and testing.
Then the try…except blocks, those can be relevant actually-in case you want to scale your code to use some APIs or database. An error other than the code Ade might occur that you have no control over. And also, I don’t think a try except does similar job as conditions. Try except just masks and help catch errors you sometimes have little or no control over…these might be unforeseen or due to poor network. I didn’t explain this in detail in the video but for beginners, it might be worth staying basic.
I’ll drop an advanced one involving a real database and creating API soon using OOP. Overall, thanks for the observation bro. This will help improve future tutorials 👍
My first data = dict(zip(d, f)) wasn’t used and there was no new line for new signups
Amazing, thank you.
Traceback (most recent call last):
File "c:/Users/ozte2/Desktop/Programlar/AsistanLogMain/AsistanLogin.py", line 119, in
home()
File "c:/Users/ozte2/Desktop/Programlar/AsistanLogMain/AsistanLogin.py", line 108, in home
gainAccess()
File "c:/Users/ozte2/Desktop/Programlar/AsistanLogMain/AsistanLogin.py", line 17, in gainAccess
a,b = i.split(",")
ValueError: too many values to unpack (expected 2)
Make sure there's nothing like a line space in your text file before writing to it at first. Let Python do the writing. It should be a fresh text file.
@@techWithId hello , i have the same error but i dont know what to do, also i dont know wat you mean by line space. could you please help
@@slipyslapii1841 I mean spaces between the text. You can fork the project from my repo if you need some comparison. The link is in the description section. Run it and let me know how it goes.
This is a really great tutorial!
Great stuff! really appreciate it!
Thanks Chris. I'm glad it helped. Please don't forget to subscribe to my channel..
why i got this error? ValueError: not enough values to unpack (expected 2, got 1)
you missed the 'zip' while storing data into dictionary i think..
great video brother, thanks
THX, helpd me a lot!
Can u make a project on exam system?
1)admin
2)student
3)Logout
1) Admin can add,remove, update and display questions.
Questions has 4 options.
2)student can give exam and result is shown after exam
Bro I just finished a project that has every single thing you just named and came across this comment 😂
Right on my list of content. Thanks!
Right on my list of content. Thanks!
33:10 The problemim having is that you dont need to enter the full specific login details, for example i can enter "o" for the username and "y" for the password, as it is still detected to be in the line, but it dosent take into account other letters in the same line, help pelase!
Hi, sorry I missed this. I believe you’ve solved the problem.
Thanks a alot
how ever ive navigated a whole lots of videos trying to learn python, all i see is tutors develop codes as you have done , and end the video , i follow instructions and input a code and run it then it appears on the display section in the python platform. my question is how and where can one use this , as in real life, real application . is this what its meant for? just opening python and typing codes . i really need help. im beginning to lose interest
You don’t have to lose interest if you have interest, Iyama. Every block of code, function, or class has its application in a bill of a program or a full scale application. All you need is the creativity to put things together so they can work as a whole. Keep learning man. You’ll soon see the essence. Also, start a project… might be a website or an app that does a particular thing.
so I watched this video and got the code but I'm having 2 errors so far, the first one is it says "ModuleNotFoundError: No module named 'bcrypt'" I assumed this was because python in 3.9.7 had integrated it, seems fine once I took it out so that bit doesn't matter, however I keep getting the error:
in Register
a,b = i.split(",")
ValueError: not enough values to unpack (expected 2, got 1)
I looked the comments and I checked the text file to make sure its completely empty, that didn't help, there is no space between comma and speech mark in a,b = i.split(",") I also tried just typing the username and password in the text file then login instead but I get the same error message in GainAccess() any help?
Hi, you can try to pip install bcrypt to see if that solves the problem.
For the second error, I think you may be having some white spaces after entering your values into the database. The error is saying that you have two variables that you want to assign a split to, but couldn’t assign split values cos the value on the right is one and on the left, thee are two.
So, you might want to add a space between a and b
a,b = i.split(“, “)
So that you’ll have left values assigned to a and the right values assigned to b.
I hope that works. Let me know please.
Please cross-check how you’re entering your data into the text file as well
just erase the space between d, f like: data = dict(zip(d,f))
Really good vid pls do more ill sub
why did you write if not len less than 1 instead of writing if len more than 7? also what is the point of the if data[Username] after the first try?
Hi, partnermammoth, if not less than 1 is only ensuring that the user enters a value. It means you can’t go ahead without providing a value in that field.
Then if data[Username] gets the value of the corresponding Username. Basically, it’s checking the array contains the username supplied. You might want to look up Python dictionaries or arrays to understand how that works.
Let me know if you have further queries.
ok thank you that was helpful but now I'm wondering if I can use this type of database to store information so that it is stored corresponding to the user's account because I am trying to create a game and would like the data to be saved to the user's account which is the purpose of my login system.
@@partnermammoth2562 if your purpose is that robust, for security and ease, I suggest using a real database like MongoDB instead of a text file, as this method isn’t secure. This is just to show you the fundamentals of any login system.
Let me know how I can help with a real database or you need me to create another tutorial on that?
@@techWithId yes please that would be great thank you
My code isn't working with the data[Username]...what does it do exactly and is there another to do what it does? (nvm, figured it out! this video was really helpful!)
The code works only on Signup. But Login prints out . = Password or username doesn’t exist. (On except)
even tho the name and password are stored in the database.txt ?
How do I fix this error.
Bro I’m curious on why you’ve deleted my comment regarding the traceback error
Your comment is still there. I don't delete feedbacks.
If that's the error you're getting now, I think you should take a look at your validation code more closely. It has to do with what you've written while validating. That's not a built-in traceback.
Hii sir, how can we do for Purchase History using python?
btw, very nice video and helped me a lot!!!!
Added to my video que
@@techWithId Thanks!!!! Can you do it ASAP sir? My project deadline this Wednesday but I leave the history part😭Your video really helped me on my project a lot!!!!!
How to add change password function
It’s simple. You just need to overwrite the password input in a password change function.
Sadly I left that out in the the tutorial.
Thank you, sir
Bro,
a, b = I. split(", ")
^
TabError : inconsistent use of tabs and spaces in indentation. How to solve this
Python is indent-sensitive. So like the error says, make sure you use consistent tab throughout.
Eg.
def example():
dosometing
Python automatically tabs the child call with four spaces from the parent code, which is the function call.
@@IdowuPaul but How to fixed any code function that's Space
I have a problem in db.write(first name+","+password+"
")
Invalid syntax .Perhaps you forgot a comma?
You have first name written in your code, this is seen as two variable names.
If you need to use first name at all, try separate them using an underscore like this first_name. Then make sure you do the same thing across all variables with that name to avoid error.
@@techWithId I have a many information for register account , but as u know in login I need just firstname and password ,so I don't know how I write it
I am getting an error in spilt function called a,b = i.split(".")
ValueError :not enough values to unpack(expected 2,got1) how to solve this error. Pls help
Even I tooo
Hi Lahari, please make sure there is no line space in your text file before trying to split your data. Also, ensure that when writing to your text file for the first time in this case, clean any initial text input in your text file. Let me know if that works. Your file should be a fresh text file.
Please refer to my reply for @Lahari
@@techWithId thank you so much program worked without error
Glad it worked! Please don't forget to subscribe if you've not done so already. :)
Hi, can you help me, I want to store more data on that dictionary, like username, gender, email and password, how to write and read that one
While you might do this with a text file, I suggest using a real database like MongoDB or PostgreSQL instead. I’ll put up a video to show that soon. Please watch out.
@@techWithId the thing I'm working on this project broh, and I have to finish it by Wednesday (Next week), so time is not really on my side
@@learnwithme395 I should put up a video today or tomorrow.
@@techWithId appreciated man, do you have github account or an email, I want you to check out my code
@@learnwithme395 yes github.com/omisolaidowu
Email: omisolaidowu@gmail.com
Tq so much ❤️
My pleasure. Please don't forget to subscribe if you've not done so already.
This really helped me with an assignment. Is there a way I can use a While loop for the code to continuously ask the user to input the correct user name and or password?
i have been looking for this for so longg! because i want to create a simple creat & login type system, so you'd have to create an account each time.
i wanted to use while loops but eventually the code breaks because it essentially runs through each piece of the code and then moves to the next
e.g. when you get your password wrong and username right, when you type you password correctly and username wrong, it seems to not start from the top. So it technically sees it as it has been inputed correctly somewhere in the code and it enters.
i've been wondering if there's a way to get the program to re read a set of code until ALL conditons are a true and not just a segment.
r u from guvi insititute
@@saranrajv2342 hi ,I'm from guvi Institute
@@hushashree641 how is ur guvi's traning
im also from Guvi, these codes are working for u
We are getting error in split function saying expected two parameters can you please help me out.
Hi, can you please paste your code so I can see?
Thank you so much.. error got cleared.
Happy it worked! Please don't forget to subscribe :)
@@ashwithashetty8797 how did you clarify the error?
@@tomasoon It's either the text file wasn't clear before writing. (clear each time before you run) or the python code wasn't writing "," user and pass in each line. Lmk if that works.
"No such file or directory: 'database.txt' " but i have the txt created and in the same root directory.
Hi David, can I have a look at your full code so I can know where the problem is? Just that block where you tried to read or write to the txt file.
Thankyou!
Nice vid
Please, sir. Is there a way to get the following result to a txt file?
jamiu = {username: "atom", password: "****", accountID: 6533664, email: "test@test"}
Please, note that this is just for a single user and any other new user's data will be saved below in this same format. How then would one refer to this information in a system where you want the user to be able to login using either his/her username/email/account ID followed by their corresponding passwords?
How would this code look like if i could avoid using dict()? is there any other way to validate username and password?
Hi, Moe, the best way is to merge the list into a dict since a dict looks like JSON--and JSON is a great way to access data. You might leave the data in lists as they were but dict lets you query user entry. For instance, how would you have accessed the password based on the username? It's easier with dict as all you need to do is query the dict by user entry like so:
data["username"] which will provide the password of the user. Then just calling username will produce the username.
Note that this was just a demonstration of login system. I would use a real database like SQL or NoSQL for a real-life project. You'll write less code that way and your query is optimized. Hope this helps?
@@techWithId Hey thanks for the quick reply! This somewhat helps and I understand that this is not a real-world demonstration, I need to do this for my assignment and this code snippet is exactly what i was looking for, but due to assignment restraints I cannot use dict() or libraries so I'm very lost as to how I can avoid using dictionary for the login function
@@moemalik6732 if you're looking for an alternative to the dict function instead, you could use a dictionary comprehension like this:
data = {f[i]:d[i] for i in range(len(f))}
Just replace dict(zip(d, f)) with the above.
Hope that helps?
@@techWithId I don't know if it's my implementation but it didn't work, I am willing to ditch the error handling as it is not necessary for my case, I just want the sign up and login features to function properly.
@@moemalik6732 what was the error message?
Please help me with some question
Thanks though am new to this so direct me to more
Bro,
Line 59 Except :
Syntax Error : Invalid Syntax
how solve the problem
when i run my code in cmd at that time python file run but its not showing create username input even no error
please help me
Hi, Shivanee. Are you sure you’re returning the correct function response. Then check if you’re calling the functions in the correct places.
@@techWithId yes
can you do a passwordless authentication login system?
Noted
how if wanna put more resources
Sure, you can do that using a real database (e.g mongodb) instead of a text file. You can even make a light web app from this using flask or any other Python web framework you’re familiar with.
nice! Could You please make a tutorial that would encrypt the password i database.txt inc case it got leaked or exposed. That would be very cool
Added to my que. Thanks for the suggestion.
Hi. So I've just updated the video to include password encryption. You can find it on my channel. Here's the link:
ruclips.net/video/BQlv5miJQdw/видео.html
Oohhh dude thanks mannn
I was stuck 6 months (ik. Embarrassing)
In this tpc
Great tut btw
do we need to close the database.txt at the end of the function ?
Doesn’t change a thing here.
thank you so much, this is so helpful and you're so funny & kind!
Mine can’t define reused usernames, it can keep have the same ones
My 9ja brother. I dey uni for england and this your video just save me like that lol
Ah ah ah 😜
Great video 👍
really good tut, helped me alot with my studies
Got an error. There's no such file or directory
Hey Haven. Make sure that you have a named txt file in your project root directory.
it did
nt work at all
Can I know what the error info was?
why mine will show value error ValueError: not enough values to unpack (expected 2, got 1) on the line a,b=i.split()
Delete the previous inputs in your text file and make sure you write this line properly:
db.write(Username+", "+Password1+"
"). Adding
to the end of the write function prevents any empty line in your text file.
Your error is most likely caused because there is an empty line before your first user input. Hope that works?
@@techWithId ok 👌 ltr i try btw ur video help me so much thanks
You're welcome. Please dont forget to subscribe :)
There's probably a line space in your text file. Make sure you don't have preexisting texts in your txt file before writing to and reading it
@@techWithId so sorry to bother you but
it still says there is an IndentationError: expected an indented block for d on line 37
Thanks for the video! I’m pretty new to python and I’m having trouble understanding what the a,b= i.split(“, “) means. Where did a & b come from?
its a variable, instead of doing a[0] and a[1] he define it by a,b.
it splits the string in each line into two strings. The first part before the "," is assigned to variable "a" and the second one after the "," is assigned to variable "b".