'How to write header to a text file? [closed]

I write output of my code into a .txt file. I need to put a header in the .txt file. Something like this:

id, width, height, weight
1     252    455      555
2     544    525      475
3     4752   2445     58.5 
4     452.3  125.2    1422.36

I wrote this code but it just writes the header each time that I write the numbers. I only need to write header once. The rest should be numbers. I was wondering how to do so?

header = "id, width, height, weight"
with open(outputFilename, 'a') as file_writer:
    file_writer.write(header + "\n")
    file_writer.close
    
    
output_string=f"{f1}, {f2},  {f3}, {f4}\n"
print(output_string)
with open(outputFilename, 'a') as file_writer:
    file_writer.write(output_string)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source