Intro
First of all, Sorry for creating the post so late.
As you all might know that in my first post we created a password generator in Python
What are we going to do
In this post we will add a new feature to save password that is generated in a file named password.txt if user wants to store it.
Let's Code
The basic code we have written already
#importing the required module
import random
import string
#defining variables
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
symbol =string.punctuation
# making a variable a mix of all other vars
all = lower + upper + number + symbol
#asking the user for entering the password length he/she wants
length = int(input("Enter the length of the password \n"))
#generating the password
pwd = "".join(random.sample(all, length))
#now the password is generated and now we have to print it for the user
print("Your password is: ",pwd)
Now, for storing the password we have to first create a file named password.txt in the same directory.
Then we will ask user if he/she wants to store the password or not ?
choice = input("Do you want to store password in txt file ? y/n ")
Now we will use if else statement to do the steps according to the input if user says yes then python will open that file and write the password and save it else it will just end the program
if choice == "y" :
f = open("pwd.txt","a")
f.write("\n"+pwd)#We are using \n so that if we store multiple passwords then we must be able to identify the different passwords
print("Your password is successfully stored in the file pwd.txt")
f.close()
else :
pass
And with this we are done adding our new feature to the password generator.
I hoped you all liked it and feel free to ask anything in the comments section !
Top comments (0)