Every password manager must save the password a user gives and output it when the user needs it. We are gonna apply the same thing to our password manager. We will first start by creating a .txt file to store the passwords.
Get the full code from here with some extra addition to get the code up and running.
1| Create a text file in python
Making a text file in python is fairly simple.
import os.path
def checkExistence():
if os.path.exists("info.txt"):
pass
else:
file = open("info.txt", 'w')
file.close()
At first we are checking if the file info.txt
exists in our directory where the python file is. If not, just simply create one. 'w' means we are creating the file to write something. If you want to know more about files follow this.
2| Write to file
Alike you print in the terminal, you can similarly write into a file. just use the write
to write inside a file. Here is how.
def appendNew():
# This function will append new password in the txt file
file = open("info.txt", 'a')
print()
print()
userName = input("Please enter the user name: ")
password = input("Please enter the password here: ")
website = input("Please enter the website address here: ")
print()
print()
usrnm = "UserName: " + userName + "\n"
pwd = "Password: " + password + "\n"
web = "Website: " + website + "\n"
file.write("---------------------------------\n")
file.write(usrnm)
file.write(pwd)
file.write(web)
file.write("---------------------------------\n")
file.write("\n")
file.close
At first, we are opening the file we just have created. 'a' argument means we will append something in that file. We could have used 'w' which stands for write. But every time we open a file with a 'w' argument, it erases everything written previously. Which we don't want at all. So, we will continue with 'a'.
Then we take input from the user about his user name, password and the website. I'm using the empty print statement to space things out in the terminal so that it looks good. Then we are simply creating three string variables to store the username, password and website.
And then we will write to our file by using the write
function. Remember, unlike print
, write
doesn't add a new line every time we call it. So, use \n
if you want to add a new line in your file. That's it! Now our user can save passwords in info.txt file(or whatever you call).
3| Output the password
This time we will see what passwords the user has saved. The bellow function will get the job done.
def readPasswords():
file = open('info.txt', 'r')
content = file.read()
file.close()
print(content)
Just like our previous function, we are opening our file at first. But this time, instead of append, we will open the file as read(use r
). Then we will create a new variable content
which will be the place holder of the contents in the file. And then just simply print it out. BOOM!!!
Yeah! I know this function will print all the passwords which we don't want. For that, we will make need to a search operation. But in order to keep the post as simple as possible, I will end it just right here. In the next post, we will make a search option.
You can get the full code here with some extra addition to get the code up and running.
Until next time, stay safe, stay at home.
💗Love from humanity💗