I want to append some text to a file if it exists, or create the file and append some additional text to it if it was just created. I know that I can append/create by using open("filename","a"), as this line of code will create the file if it doesn't exist. However, how can I know if the file existed or it was just created?
Eventually I want to achieve this:
with(open("filename","a")) as f:
if filename existed before open
# Append text
else if filename was just created
# Append some headers
# Append text
I could achieve this by checking if the file exists first (os.path.isfile(filename)) and then act accordingly, but I am looking for a more elegant way.