os.path.exists() methodPython (original) (raw)
os.path.exists() method-Python
Last Updated : 1 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:
- The first two statements return True assuming the file and directory exist at the given paths.
- The third returns False because 'nonexistent.txt' does not exist in the current directory.
Syntax of os.path.exits()
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.
Examples
**Example 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.txt'** is checked relative to the current working directory. If it exists there, it returns True.
- ****'./no_such_dir'** refers to a subdirectory that does not exist, so it returns False.
**Example 2: Checking an empty path
Python `
import os
path = '' print(os.path.exists(path))
`