Reputation:
I'm trying to create a personnal password vault. Basically I want to create a program that asks me what I want to do and depending on my input (New, or Access "title of website here") the program either lets me add a website title with it's corresponding username and password. Then in I type "Access..." i should get the username and password returned relative to the website's tite I input after the Access input. OFC all of this data is to be stored in a text file.
PROBLEM: The problem is that when I check the text file, it stays blank. I tried changing the access mode (a, r, etc) and it doesn't work. Not only that, but the program returns only the last data entered, and not whichever data in select.
Keep in mind I'm a beginner.
Below is the code used:
vault = open("Passvault.txt", "a")
total = True
while total == True:
creation = True
action = input("What do you want to do? ")
if action == "New" or action == "new":
title = input("Add website: ")
username = input("Create username: ")
password = input("Create password: ")
vault.write("Title:" + username)
vault.write("\n")
vault.write("Username:" + username)
vault.write("\n")
vault.write("Password:" + password)
elif action == "Access " + title:
print(title)
print("Username: " + username)
print("Password: " + password)
creation = False
elif action == "Close":
creation = False
total = False
vault.close()
Upvotes: 1
Views: 904
Reputation: 159
First of all you seem to be a bit confused how everything is working here so lets me clarify and then I will point out all the problems.
You first create a variable vault and assign it to file opened via append(a) mode.
Now you set total to True and then use it for infinite while loop.
while True
so that loop continues
forever and you don't need to assign total a value of True because it doesn't need to
be changed as you can use break
keyword to break out of loop. You just wasted memory
by assigning variable to TrueNow inside the while loop, you create creation variable but never use it for a purpose, so it is I think not needed or you might have another plan and might use it in the future so I won't say about it.
Now you get input from the user and set it's value to action variable. This step is OK but I wan't to clarify that there are several security problems here but since they are very complex so I won't tell them here but this should be OK.
Now's the time of conditions:
Now in first if condition, you check is action == 'New
or action == 'new', here you don't need to include
or` , although that's okay and there is no problem with it, you can do better. .lower() is a string method and it lower cases all the letters of the alphabet.
Either you can do if action.lower() in ['new' , 'create' , 'n' ....all inputs you want ]
, note here you dont have to include '.....all inputs you want' , Its just to tell you that you need to put there all inputs on which you want the condition to be true.
Or you can simply do if action.lower() == 'new'
Now only if the condition is true this code runs
You create title variable and set it to user input, now here's too security problem but due to complexity I won't go into much deep. Always remember , wherever you have user interaction with application , there's a chance of security problem there.
Then you get the password and same problem. But also notice that you don't encrypt anything here. That's a big issue.
Now you write everything to the file but be aware that .write()
doesn't write directly. You either have to call .flush()
or close the file via .close()
. But notice the problem , you called .close()
but in the very end and out of the infinite while loop. You can tell that that close command is out of while loop by telling that both while loop and close are at the same level. If you have to put it inside while loop, you need to indent it a bit just like other code which is inside while loop. But that is also not gonna work because you close the file and you can't reuse it withot reopening it so either you put both open and close parts inside while loop or you use the .flush()
method. This is the biggest bug due to which you are facing such a problem.
Now's the time of elif conditon
Here you see that in the condition you do if action == 'Access ' + title:
and there are several problems here.
First you see that you directly compare action to access and you can do it better by using that list trich used in above if condition and can use .lower()
method
Second that you use the title variable which notice only get defined when the above if
condition runs. What if the above if
condition doesn't run and what is the user directly said 'Access Google" or anything accept the above if
condition such as "#^%&GJ" . Then elif condition will run and your variable title will not be found by python and then your program will crash. So it is better that you first search that if the input contains Access and the break it into Access And The keyword after it. There are several functions you can use like .strip()
or others. Visit this link fore more methods.
Third, you are using the file cursor without being aware of it. Just like cursor in any text editor, the file handler also has a cursor which move foreward as you read the file and in the end it will reach the end of the file and once again you read , it wont return anything since there's nothing ahead of it. To know more about it, visit this link.
And these were the main problems due to which your code is not working correctly
There are also many more problems such as
But for now, that's gonna help you in your task.
Good Luck And Smart Work!
Upvotes: 0
Reputation: 264
Try this code:
vault = open("Passvault.txt", "a")
total = True
while total == True:
creation = True
action = input("What do you want to do? ")
if action.lower() == "new":
title = input("Add website: ")
username = input("Create username: ")
password = input("Create password: ")
vault.write("Title:" + username)
vault.write("\n")
vault.write("Username:" + username)
vault.write("\n")
vault.write("Password:" + password)
elif action == "Access " + title:
print(title)
print("Username: " + username)
print("Password: " + password)
creation = False
elif action == "Close":
creation = False
total = False
vault.close()
You didn't close the file at the end.
Upvotes: 0
Reputation: 159
First of all, where do you define the variable title
used in the elif
condition. Another that how you are reading the file for title. Third and most important that your problem clearly mentions is that you are not seeing results being saved and that is because you don't close the file instantly and close
method actually writes all data and saves the file in the end. Another way to save data before closing the file is by using the flush
method.
Upvotes: 0
Reputation: 1873
First of all, you are not reading the file. Reading AND writing in 'append' ('a') mode is a bad idea (see potential cursor issues).
I tested the code on Google Colab and it works. Note that the writing occurs only if you close the file.
Suggestions:
It is a good time to learn about databases. Even a simple Excel file would fit your goal better (check package csv
).
Use 'a+' as the mode of opening the file: if the file does not exist, create it.
Try to open and close the file a bit more frequently, say, each time when you want to write something. Also with
clause may be useful (https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)
Upvotes: 1