Open In App

os.path.exists() method-Python

Last Updated : 01 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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))

Output

False

Explanation: An empty string is not a valid path, so os.path.exists() returns False.

Related articles


Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy