1

I'm making a login system for my school task and when I try to add text to a Notepad file using Python it deletes what is already there so I can only store the information of one user at a time. Any idea how I can fix this? I've looked around on the internet but I can't find the right combination of words to get the search result I want... thanks in advance. I'm using IDLE (Python 3.2 GUI)

Edit: Problem solved, thanks for the help everyone.

ItsFyn
  • 7
  • 4

3 Answers3

1

You have to open file in append mode, for example:

f_obj = open('myfile','a+')
Vel
  • 524
  • 3
  • 11
1

For that, you have to use "a" mode for appending data in a file.

f_obj = open("c:/file_path",'a')
f_obj.write("append")

or

with open("c:/file_path", "a") as f_obj:
    f_obj.write("append")
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

You can use 'a for append' mode instead of 'r for right' mode, see following:

with open('your_exist_file.txt', 'a') as file:
    file.write('appended_text')
Faddi
  • 1