Check if a file exists using Python

Using os.path

import os

file_path = 'path/to/your/file.txt'

if os.path.exists(file_path):
    print(f"The file '{file_path}' exists.")
else:
    print(f"The file '{file_path}' does not exist.")

Using pathlib (Python 3.4 or later)

from pathlib import Path

file_path = Path('path/to/your/file.txt')

if file_path.exists():
    print(f"The file '{file_path}' exists.")
else:
    print(f"The file '{file_path}' does not exist.")