Python DictWriter header - not writing in first line of existing file

Hello

I am facing a very unique problem and not able to understand why. I have written a function which will check header of the file. If header is present good else it will write the header on top

def writeHeaderOutputCSV(fileName):
# See if the file exist already
    try:
        with open(fileName) as f:
            # Find the first line
            f.seek(0)
            head = f.readline()
            headList = [int(e) if e.isdigit() else e for e in head.split(',')]
            if headList == ['Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword\n']:
                # both file exist and header also exist
                logging.debug('writeHeaderOutputCSV - File ' + fileName + ' found and header is also found')
            else:
                  #file found but header not found write header
                with open(fileName,'a') as existFile:
                    writehead='Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword'
                    existFile.seek(0)
                    writer = csv.writer(existFile,delimiter=',')
                    writer.writerow(writehead)
                    # even dictwriter is also a problem not writing in first line but in the last line and then other datas are copied
                    logging.debug('writeHeaderOutputCSV - File ' + fileName + ' found but no header is found. So writing header')
            
    except:
        with open(fileName,'w') as NewFile:
            csv.DictWriter(NewFile,fieldnames=['Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword']).writeheader()
            logging.debug('writeHeaderOutputCSV - File ' + fileName + ' not found so header inserted in the new file')
            

So both in dictwriter and doing seek(0) and normal writing it is writing on the last line before other lines are written. If the file does not exist (except block) header is written at the top without an issue. But if the file exist but header is not present - then it is writing after the existing data :frowning:

The above code screws up the whole writing to csv file also. It looks like it randomly deletes line or override or write from top

with open(fileName,'a') as existFile:
                    csv.DictWriter(existFile,fieldnames=['Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword']).writeheader()
                    logging.debug('writeHeaderOutputCSV - File ' + fileName + ' found but no header is found. So writing header')
                    existFile.close()

This one doesn't override while writing new data. But header row is written at the last line

---------- Post updated at 08:54 AM ---------- Previous update was at 12:43 AM ----------

Closing the thread : Solution as below
When calling the function it looks like it writes to the last line always

So in place replacement is not possible - we have to create temp file and swap it back

def writeHeaderOutputCSV(fileName):
    try:
        with open(fileName) as f:
            f.seek(0)
            head = f.readline()
            headList = [int(e) if e.isdigit() else e for e in head.split(',')]
            if headList == ['Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword\n']:
                logging.debug('writeHeaderOutputCSV - File ' + fileName + ' found and header is also found')
                f.close()
            else:
                df = read_csv(fileName)
                tempOutputFile = open(appPath+'output_temp.csv','w')
                df.columns = ['Customer' , 'Alertkey' , 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword']
                df.to_csv(tempOutputFile,index=False)
                tempOutputFile.close()
                with open(fileName,'w') as updateFile:
                    with open(appPath+'output_temp.csv','r') as readUpdateFile:
                        for allRows in readUpdateFile:
                            updateFile.write(allRows)
                logging.debug('writeHeaderOutputCSV - File ' + fileName + ' found but no header is found. So writing header - Investigate who deleted the header slowing down the program')
    except:
        with open(fileName,'w') as NewFile:
            csv.DictWriter(NewFile,fieldnames=['Customer', 'Alertkey', 'Node', 'Alertgroup', 'FirstOccurrence', 'TKT_Flag', 'X733SpecificProb', 'TKT_TicketNumber', 'TKT_Keyword']).writeheader()
            NewFile.close()
            logging.debug('writeHeaderOutputCSV - File ' + fileName + ' not found so header inserted in the new file')