From aab36ef9a62fa00e7b968de28d0a3e6a5698aebd Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Fri, 18 Mar 2022 14:04:11 +0800 Subject: Prevent append CSV header when file pre-exists --- libs/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'libs/utils.py') diff --git a/libs/utils.py b/libs/utils.py index fde86eb..3df1574 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -7,12 +7,15 @@ BATCH_LOGGER = 'batch_logger' class FileHandlerWithHeader(logging.FileHandler): - def __init__(self, filename, header, mode='a', encoding=None, delay=0): + def __init__(self, filename, header, mode='a', + encoding=None, delay=False, errors=None): self.header = header self.file_pre_exists = os.path.exists(filename) - logging.FileHandler.__init__(self, filename, mode, encoding, delay) - if not delay and self.stream is not None: + super(FileHandlerWithHeader, self).__init__( + filename, mode, encoding, delay, errors + ) + if not delay and self.stream is not None and not self.file_pre_exists: self.stream.write(f'{header}\n') def emit(self, record): -- cgit v1.2.3