Latest Code Tutorials

Python os.path.exists() Method Example

When you are working with a remote server, you often need to check if the directory or file exists in the server or not. Python provides an OS module that deals with the file system.

The os.path module also provides the following functions for working with pathnames. The module is available for both Python 2 and 3.

In the context of this tutorial, the most important functions are:

  1. os.path.isfile(path) – Returns True if the path is a regular file or a symlink to a file.
  2. os.path.isdir(path) – Returns True if the path is a directory or a symlink to a directory.

But in this tutorial, we will only see the os.path.exists() function.

Python os.path.exists()

To check If the file or directory exists or not in Python, use the os.path.exists() method. The os.path.exists() method is used to check whether the specified path exists or not. It is also used to check whether the given path refers to the open file descriptor or not.




The os.path.exists() method accepts the path as a parameter that object representing a file system path.

Return Value

The os.path.exists() method returns a Boolean value of class bool. The os.path.exists() method returns True if the path exists otherwise returns False.


To use the path.exists() method, import the OS module at the top of the file. 

import os

Now, use the path.exists() function to check whether a file or folder exists.

import os

path = "/Users/krunal/Desktop/code/"

is_exist = os.path.exists(path)




As you can see that the path does exist in the file system, and that is why it returns True. The os.path.exists() function may return False, if permission is not granted to execute os.stat() on the requested file, even if the path exists.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.