Search and write lines of multiple strings in python? - python

def info(): #Here you can write your password and username.
Username = raw_input ("Username: ")
Password = raw_input ("Password: ")
print("")
for line in open('/home/hello/Usernames.txt'):
if Username == Username in line: #Checks if username is available.
print ("Username is already taken!\n")
info()
else:
User = open("/home/hello/Usernames.txt", "w") #Registers username.
User.write(Username)
Psw = open("/home/hello/Passwords.txt", "w") #Registers password.
Psw.write(Password)
print ("You have succsesfully registered!") #If you managed to register.
break
info()
This is an account registerer that can register both username and password. But I need help with something... How can I make it check multiple lines of strings in a file, and how can I make the program write a new line of string in the text files when I register without replacing the old string?

Open the file for appending ('a') mode instead of writing ('w') which truncate the file.

Related

Dictionary is being overwritten with new input

I'm starting my first project (a password manager). What I have done so far is make it so the user can input whether they want to make a new password or look for a password. If they choose to enter a password, the account/purpose for the password and the actual password will be saved to a dictionary. For example, a purpose could be for "yahoo" and the password being "example". That dictionary is then written down in a text file. If the user decides to look for a password, all they would have to do is type in the account for the password. So far everything is working except for the fact that when I enter another password and account, it overwrites the pre-existing password and account instead of adding the new password to the dictionary.
import json
passwords = {
}
prompt = "If you want to make a new password, type 'Make password'."
prompt += "\nIf you want to look for a password, type 'Look for password'.\n"
answer = input(prompt)
def password_list(account_name, password_name):
passwords[account_name] = password_name
answer = answer.upper()
found = 0 # used to see whether account for password can be found
if answer == "MAKE PASSWORD":
account_name = input("What use is this password for? ")
account_name = account_name.upper()
password_name = input("What is the password? ")
password_list(account_name, password_name) # purpose and password saved to dict
with open("passwords.txt", 'w+') as f:
f.write(json.dumps(passwords))
print("Your password was saved!") # dictionary gets saved to text file
elif answer == "LOOK FOR PASSWORD":
with open("passwords.txt", "r") as f:
passwords = json.loads(f.read()) # text file gets opened to read
if not passwords: # if the list is empty...
print("Sorry but there are no passwords available. Make a new one!")
elif passwords: #if the list isn't empty...
search_account = input("What account is this password for? ")
search_account = search_account.upper()
for name in passwords.keys(): # list of accounts get searched
if search_account == name: #if an account is in the dictionary
print(f"The password is '{passwords.get(name)}'.")
found = 1
break
if found != 1:
print("Sorry, we can't find such name.")
Cool project.
It's because every time you start the script you force the password dic to be empty. So when you add a password, it's added to a new empty dic and than you overwrite the file with this empty dic + new_password.
When you code, think about the most likely outcome at every run: the file exists. IF it doesn't, than create it.
This is what I demonstrate here in the load_passwords() function.
As an extra, I propose to you a more Pythonic (and efficient) way to search through a dictionary's keys in O(1) rather than O(n).
import json
prompt = "If you want to make a new password, type 'Make password'."
prompt += "\nIf you want to look for a password, type 'Look for password'.\n"
answer = input(prompt).upper()
def load_passwords():
try:
with open("passwords.txt", "r") as f:
passwords = json.loads(f.read()) # text file gets opened to read
return passwords
except FileNotFoundError:
print("Sorry but there are no passwords available. Make a new one!")
return {}
def password_list(account_name, password_name):
passwords = load_passwords()
passwords[account_name] = password_name
return passwords
if answer == "MAKE PASSWORD":
account_name = input("What use is this password for? ").upper()
password_name = input("What is the password? ")
passwords = password_list(account_name, password_name) # purpose and password saved to dict
with open("passwords.txt", 'w+') as f:
f.write(json.dumps(passwords))
print("Your password was saved!") # dictionary gets saved to text file
elif answer == "LOOK FOR PASSWORD":
passwords = load_passwords()
if passwords: #if the list isn't empty...
search_account = input("What account is this password for? ").upper()
# this is much better
if search_account in passwords:
print(f"The password is '{passwords.get(search_account)}'.")
else:
print("Sorry, we can't find such name.")
Note: Be sure to encrypt your passwords before saving to a file.
"You should probably initialize your password list by reading your json file if it's there. Otherwise, when you run MAKE PASSWORD, it adds the new password to an empty dict and overwrites the existing password file which might've had a password in there before." – rchome

When i try to make python write user input to a txt file, it doesn't

i am learning python, and i wanted to create a little login and register program, that writes the username and the password to a txt file (so that it can be later used), i am currently working on the register function, when i try to write to the txt files it does nothing, i tried doing a with loop, a .flush() and .close() but neither of them saves the info.
Here's my code:
username.write = input ('username > ')
password = open("password.txt", "w")
password.write = input ('password > ')
print('Welcome.')
username.close()
password.close()
What am i missing?
Edit.
Neither 3 of the solutions to the suggested question work for me...
Get your input and store them in two variables and then write them to files:
username = input ('username > ')
password = input ('password > ')
with open('usernames.txt', 'w') as userFile:
userFile.write(username)
with open('passwords.txt', 'w') as passFile:
passFile.write(password)
yourfile.open(filename,'w')
username = input()
password = input()
yourfile.write(username)
yourfile.write(password)
yourfile.close()

Getting python to write to new line in a FIle

I am trying to make a simple login system because I am bored and want to learn python, I am trying to store the usernames in a file but new usernames are just replacing the current one in the list.
#USENRAME UNENCRYPTED PASSWORD SYS
print ("WOuld you like to login or signup (login/signup)")
choice = input()
if choice == "signup":
print ("Can you enter your username please?")
username = input()
with open('username') as f:
if username in f.read():
print("That Username already exists")
else:
f= open("username","w+")
f.write(username + "\n")
f.close()
Say if the first username I enter is "Dave" and then I close the program, the next username I register with being "Harry" The "Harry" will just replace the "Dave" In line one of the "usernames" file.
You dont need to open your file twice, but when you open it the first time what you you want to be able to do is append to what is already there.
with open('username.txt','a+') as f:
if username in f.read():
print("That Username already exists")
else:
f.write(username + "\n")
Try reading this if you're unsure.
https://www.guru99.com/reading-and-writing-files-in-python.html

How can I create a login program that checks for availability on Python?

I'm making a user login for a little project I'm working on at school, practising my file handling and whatnot. For the most part, it's going fine, but I'm having trouble when I try and create new users. The new user needs to have an original username and password to be created, otherwise, it should tell them that their choice is unavailable. For some reason, it works fine with the first username and password in the files and makes the user retry. But when I try and create a new user with a name that I know I shouldn't be able to, it lets me.
def signUp():
username = str(input("\nPlease enter your new username "))
file = open ("usernames.txt","r")
for x in file:
if username in x:
print ("\nThat username is already in use, please try another")
signUp()
else:
print ("Your username is now ",username)
file.close()
password = input("\nPlease create a password ")
file = open ("passwords.txt","r")
for x in file:
if password in x:
print ("\nThat password is already in use, please try another")
signUp()
else:
print ("Your password is now ", password," Don't forget it")
file.close()
file = open ("usernames.txt","a")
file.write (username)
file.write ("\n")
file.close()
file = open ("passwords.txt","a")
file.write (password)
file.write ("\n")
file.close()
print ("\nYour login details have been saved")
print ("Please login")
logIn()
In the username file, it has the names:
Alex
Josh
The password file has:
123qwe
ewq321
The code works for Alex and 123qwe and stops them from being repeated, but not for Josh or ewq321.
At the moment, I'm not looking for any other improvements. I'll refine it all later. For now, I just need help with this little predicament. I understand that my explanation is a little confusing, so it might help to copy what I've already done and play around with it. Obviously, this isn't the whole code, so it might not make perfect sense.
The problem is in both loops, exactly on the else clause.
Your code checks that if the only first username already exists that's because you introduced an else on the loop, and also you need to get out of the program if the username already exists using return.
Here is the new code:
def signUp():
username = str(input("\nPlease enter your new username "))
file = open ("usernames.txt","r")
for x in file:
if username in x:
print ("\nThat username is already in use, please try another")
signUp()
return
print ("Your username is now ",username)
file.close()
password = input("\nPlease create a password ")
file = open ("passwords.txt","r")
for x in file:
if password in x:
print ("\nThat password is already in use, please try another")
signUp()
return
print ("Your password is now ", password," Don't forget it")
file.close()
file = open ("usernames.txt","a")
file.write (username)
file.write ("\n")
file.close()
file = open ("passwords.txt","a")
file.write (password)
file.write ("\n")
file.close()
print ("\nYour login details have been saved")

python 3 password checker will only read the exact txt file content

My password checker is working however it resolves only when the user input is the same as the entire .txt file.
How can I put multiple passwords in the .txt file and have my program work if any of them match the input? I would like to be able to be able to add the password 123456 so that my second if statement will work.
#simple program to check passwords against a txt file database
passwordfile = open('secretpasswordfile.txt')
secretpassword = passwordfile.read()
print('Enter your password.')
typedpassword = input()
if typedpassword == secretpassword:
print('Access granted.')
if typedpassword == '123456':
print('This password is not secure.')
else:
print('Access denied.')
the secretpasswordfile.txt only has genericpassword written in it.
Assuming every password in your file is separated by a newline, you can check if any of them match with this code. It uses the fact that you can treat the file object returned by open as an iterator for each line in the file and compares the typed password to each of these lines. the .strip() is to pull off the trailing newline from each line.
passwordfile = open('secretpasswordfile.txt')
print('Enter your password.')
typedpassword = input()
if any(typedpassword == pw.strip() for pw in passwordfile):
print('Access granted.')
if typedpassword == '123456':
print('This password is not secure.')
else:
print('Access denied.')

Categories