The logging module’s FileHandler
takes care of that for you. No need for complexity.
The handler takes an optional mode
parameter, to specify whether it starts writing or appending data to it.
From the docs:
class logging.FileHandler(filename, mode="a", encoding=None, delay=False)
The specified file is opened and used as the stream for logging. If
mode
is not specified,'a'
is used.