-1

I have a function which prints messages. then I want to save this messages into a file. But when I use .write(function parameter) it only write last message inside my file

writing_in_log = True def print_and_log(message): if write_in_log is True: logFile = open("log", "w+") logFile.write(message) logFile.close()

2 Answers2

1

I suppose you are not using the 'a' parameter when opening the file:

with open('file.txt', 'a') as file:
    file.write('function parameter')
py_dude
  • 822
  • 5
  • 13
1

You probably open the file for each writing with open(yourfile, 'w') which will erase any content from the file before you write to it. If you want to append to your file, use open(yourfile, 'a').

In case this is not the error we need more information about what you are doing, i.e. the relevant parts of the code.

timgeb
  • 76,762
  • 20
  • 123
  • 145