-2

I'm trying to save login info to a .txt file from inputs stored in a dict which i have done successfully but when try add a second set of login info it rewrites the existing login info instead of adding it to the file in a new line. This is the code i have. please forgive me if this is a simple answer i'm sorta new I'm using python 3

login_info = {
    "login_site": "blank",
    "username": "blank",
    "password": 1234
}

login_location = input('What site/app is this login info used for?')
user_define = input('What is your Username?')
password_define = input('What is your password')

login_info["login_site"] = login_location
login_info["username"] = user_define
login_info["password"] = password_define

with open(r"C:\MyTextFile\login.txt", "w+") as file:
    file.write(str(login_info))
file.close()
khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

0

Change your code to this:

with open(r"C:\MyTextFile\login.txt", "a") as file:

from python docs:

'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.

Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
0

Instead of opening the file in write 'w' mode, you should open it in append 'a' mode.

with open('abc.txt','a') as file: