I am working on an assignment for my fundamentals class and we have to create a program that is going to write/read/ and append a file. I wanted to set it up to check and see if example.txt exists. If so then append, else write. My question would be how do I check to see if a file exists or not?

See the definition for append mode = "a" Click Here which explains why checking is not necessary.

Thank you very much!!


import os

filename = "example.txt"
if os.path.exists(filename):
    print("file %s exists" % filename))
    # now do something with the file