Python Login Script; Usernames and Passwords in a separate file
You could start having a dictionary of usernames and passwords:
credentials = {}
with open('Usernames.txt', 'r') as f:
for line in f:
user, pwd = line.strip().split(':')
credentials[user] = pwd
Then you have two easy tests:
username in credentials
will tell you if the username is in the credentials file (ie. if it's a key in the credentials
dictionary)
And then:
credentials[username] == password
Python Login Script; multiple passwords per account
import getpass
credentials = {} ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f: ## Opens the file and reads it
for line in f: ## For each line
username, delim, password = line.strip().partition(':') ## Separate each line into username and password, splitting after a colon
credentials[username] = password.split(';') ## Links username to password
while True:
username = raw_input("Please enter your username: ") ## Asks for username
if username in credentials: ## If the username is in the credentials array
while True:
password = getpass.getpass("Please enter your password: ") ## Asks for password
if password == credentials[username][0]:
print "Logged in successfully as " + username ## Log in
break
elif password in credentials[username]: ## If the password is linked to the username
print "Specific error message " + username ## Log in
else:
print "Password incorrect!"
break
else:
print "Username incorrect!"
It's simpler though if you just ask for a username/password fresh each time. The way you have it - if the user enters someone else's username by mistake they are stuck in a loop forever unless they can guess the password.
while True:
username = raw_input("Please enter your username: ")
password = getpass.getpass("Please enter your password: ")
if username in credentials and password == credentials[username][0]:
print "Logged in successfully as " + username
break
elif password in credentials.get(username, []):
print "Specific error message " + username
else:
print "Login incorrect!"
Saving passwords in a separate file
It's a common mistake in Python to confuse the dot in a import path as the dot in the folder system. In Python, the dot refers to the current package, while in the folder system it refers to the current folder. Basically a package in Python is defined as a folder with a __init__.py
file.
So here when you enter from .details import username
, what Python does is it tries to import username
from the module details
in the current package. If the current folder has a __init__.py
file, no problem, but I don't think it's your case, hence the error.
What you want to write here is : from details import username, password, appKey
.
You should see this SO post to understand more about relative imports.
Hope this helps!
python read/write to file login script
As said by sytech, you could use the break
and else
clauses of the for loop:
for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
if username1 == username and password1 == password:
print("Hello",username)
break
else:
print("incorrect")
Storing the secrets (passwords) in a separate file
I think storing credentials inside another *py file is your safest bet. Then just import it. Example would look like this
config.py
username = "xy"
password = "abcd"
main.py
import config
login(config.username, config.password)
Simple login script multiple users from file
The logic was all wrong. You need something like that:
def check():
# You need to read it only once, not every loop
users = open("users.txt").read().split("\n")
for i in range(len(users)): users[i] = users[i].split("|")
# Now what you want is to loop infinitely
# until you get correct username/password
# instead of recursive calling this
# function over and over again
while True:
username = str(input("Username: "))
password = str(input("Password: "))
for user in users:
uname = user[4]
pword = user[5]
if uname == username and pword == password:
print("Hello " + user[1] + ".")
print("You are logged in as: " + user[2] + ".")
main_menu()
# If none of the records matched the input
print("Wrong username/password.")
print("Try again!\n\n")
check()
Related Topics
How to Convert .Dat to .Csv Using Python
Counting the Number of Duplicates in a List
How to Print a String Multiple Times
How to Save Opened Page as Pdf in Selenium (Python)
Get Max Value Comparing Multiple Columns and Return Specific Values
Python Calculate Distance Closest Xy Points
Saving Numpy Array to Txt File Row Wise
How to Delete Quotes from Data Read from .Csv File
Django - How to Retrieve Data in Database in Dropdownlist
How to Do This Horizontally Instead of Vertically in Python
How to Pad a String With Leading Zeros in Python 3
Django Login - Missing 1 Required Positional Argument
Convert Image Files to a CSV File
Anaconda Installed But Cannot Launch Navigator
How to Simply Add a Column Level to a Pandas Dataframe
Convert Float to Float Time in Python