Last Updated : 01 Jul, 2025
os.path.exists() method in Python check whether a specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. Example:
Python
import os
print(os.path.exists('/home/User/Desktop/file.txt'))
print(os.path.exists('/home/User/Desktop/'))
print(os.path.exists('nonexistent.txt'))
Output
True
True
False
Explanation:
os.path.exists(path)
Parameter: path is a path-like object representing a file system location. This can be a str, bytes or object implementing __fspath__().
Return Type: bool returns True if the path exists, False otherwise.
ExamplesExample 1: Using with relative paths
Python
import os
print(os.path.exists('example.txt'))
print(os.path.exists('./no_such_dir'))
Output
True
False
Explanation:
Example 2: Checking an empty path
Python
import os
path = ''
print(os.path.exists(path))
Output
False
Explanation: An empty string is not a valid path, so os.path.exists() returns False.
Related articles
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4