loading...
Cover image for Make a password manager with python: Making the basic mechanisms

Make a password manager with python: Making the basic mechanisms

muhimen123 profile image Muhimen ・3 min read

Make a Password Manager with Python (3 Part Series)

1) Make a password manager with python 2) Make a password manager with python: Setting up the environment 3) Make a password manager with python: Making the basic mechanisms

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πŸ’—

Make a Password Manager with Python (3 Part Series)

1) Make a password manager with python 2) Make a password manager with python: Setting up the environment 3) Make a password manager with python: Making the basic mechanisms

Posted on by:

muhimen123 profile

Muhimen

@muhimen123

Nothing more than a programmer nothing less than a programmer. Loves to solve Rubik's cubes in spare time.

Discussion

markdown guide
 

thanks Sir , I can be able to easily understand the basics.
But my question how can I use these database to use in log in programme?
please help me Sir

 

First of all I am not a sir(it makes me uncomfortable πŸ˜…).

Secondly, sorry because I didn't continued the series for some reasons.

Finally, to answer your question, use selenium. It's a python web automation framework. I would take the login data and then input them in a website using selenium.

 

It's too complicated. Use a database (eg SQLite) instead.

 

Thanks, I will try it for sure. πŸ˜‰