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!!

Generally:

import os

filename = "example.txt"
if os.path.exists(filename):
    print("file %s exists" % filename))
    # now do something with the file
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.