Skip to content Skip to sidebar Skip to footer

Python: Saving A String To File Without Overwriting File's Contents

I need to add my program's outputs to a saved file every time I run the program without overwriting the previous outputs. This is how I'm writing to a file now: lead.write(str(alld

Solution 1:

Here, you should open the file in append mode:

append_text = str(alldates)
withopen('my_file.txt', 'a') as lead:
    lead.write(append_text)

There are lots of specialised ways to open files for different purposes, and you can read about them in the documentation.

Solution 2:

You should open the file in append mode.

Post a Comment for "Python: Saving A String To File Without Overwriting File's Contents"