Python, how to check if a file or directory exists
New Courses Coming Soon
Join the waiting lists
The os.path.exists()
method provided by the os
standard library module returns True
if a file exists, and False
if not.
Here is how to use it:
import os
filename = '/Users/flavio/test.txt'
exists = os.path.exists(filename)
print(exists) # True
→ Get my Python Handbook
→ Get my Python Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- Follow me on X