I don't have ads on this website.
Donations help keep this site going.

If you find tkinter-snippets.com useful, please donate any amount you feel comfortable with.
Visit https://ko-fi.com/jobinpy to donate. Thank you!

pathlib – Check if file or folder exists

from pathlib import Path


full_path = Path(r"/home/documents/my_file.txt")

# Does the path exist?
# It could be a folder or file.
if full_path.exists():
    print("The path exists. It could be a folder or it could be a file.")

    # Is it a file?
    if full_path.is_file():
        print("It's a file")

    # Is it a directory?
    elif full_path.is_dir():
        print("It's a folder")

else:
   print("The path does not exist")