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:

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 2: Checking an empty path

Python `

import os

path = '' print(os.path.exists(path))

`