DEV Community

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

Posted on

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()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (7)

Collapse
 
banajitrajbongshi profile image
Banajitrajbongshi

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

Collapse
 
muhimen123 profile image
Muhimen

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.

Collapse
 
emperor profile image
King

Well you can use MySQL, SQLite or PostgreSQL as these are the common ones. You can check their functioning on google or watch a video on YouTube. As for log in programmer, you can also do it without a database.
user_name and password are two variables. When you enter a value in them you can create a file with the name {user_name}.{password}.txt. So basically you are opening the file where your passwords are save and your file name is your username.password

Collapse
 
0x4a0x410x4b profile image
0x4A-0x41-0x4B

This is a terrible idea.

Learning the basics of python (or anything for that matter) is a good thing, but you seem to start at the wrong end. Password Managers are not used to just store passwords, they are used to store them encrypted and save, they are supposed to save yourself from using the same password for everything. Nothing can be learned by reading your 3 articles on the topic. Bad practices such as not using context managers or the use of CamelCase (These two are not terrible but when trying to teach you should always try to teach the best practices) as well as suggesting that it is totally fine to store passwords unencrypted without anything to protect them.
If you wanted to teach people about python then take down this article and start simple.

Collapse
 
jonasgozdecki profile image
Jonas Gozdecki

Please, move this a new post as "How to read/write text file with data in Python".
The context of "Password manager" is the opposite of store plain text sensitive data.
Advice: Do not mention your employee's company name nor project. They may suffer more invasion attacks motivated by no security worries in your post.

Collapse
 
vlasales profile image
Vlastimil Pospichal

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

Collapse
 
muhimen123 profile image
Muhimen

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