os.path.exists() Function Explained

os.path.exists() checks whether a path exists in Python.

You can use it to test:

  • whether a file exists
  • whether a folder exists
  • whether a path is missing

This is useful before you try to open, delete, rename, or work with a path.

Quick example

import os

path = "example.txt"

if os.path.exists(path):
    print("Path exists")
else:
    print("Path does not exist")

Use this to quickly check whether a file or folder path exists before trying to open or use it.

What os.path.exists() does

os.path.exists() is part of the os.path tools in Python.

It:

  • checks whether a path exists
  • works for both files and directories
  • returns True if the path exists
  • returns False if the path does not exist

It does not tell you whether the path is a file or a folder. It only checks whether something exists at that path.

If you want a broader introduction to path tools, see the Python os module overview.

Basic syntax

The basic syntax is:

import os

os.path.exists(path)

How it works

  • path is usually a string
  • the function returns a Boolean value
  • that value is either True or False

Example:

import os

result = os.path.exists("example.txt")
print(result)

Possible output:

True

or:

False

Simple example

Here is a simple file check:

import os

file_path = "example.txt"

if os.path.exists(file_path):
    print("The file exists.")
else:
    print("The file does not exist.")

Expected output if the file exists

The file exists.

Expected output if the file does not exist

The file does not exist.

This is a common pattern before opening a file. If you are doing that often, you may also want to read how to check if a file exists in Python.

Checking a directory

os.path.exists() also works with folders.

import os

folder_path = "data"

if os.path.exists(folder_path):
    print("The folder exists.")
else:
    print("The folder does not exist.")

This checks whether the path exists, but it does not tell you whether data is a file or a directory.

For example, if a file named data exists, os.path.exists("data") will still return True.

File or folder? Use the right function

Use the right function for the job:

  • os.path.exists() → checks whether any path exists
  • os.path.isfile() → checks whether the path exists and is a file
  • os.path.isdir() → checks whether the path exists and is a directory

Example:

import os

path = "example.txt"

print(os.path.exists(path))
print(os.path.isfile(path))
print(os.path.isdir(path))

Possible output for a real file:

True
True
False

Possible output for a real folder:

True
False
True

If you are building paths from folder names and file names, os.path.join() explained is also useful.

Common beginner mistakes

Beginners often run into problems with os.path.exists() because the path is not what they think it is.

Common mistakes include:

  • using the wrong relative path
  • forgetting that the current working directory matters
  • checking a path after the file was already deleted or moved
  • assuming exists() means the file can be opened

1. Using the wrong relative path

This path:

"example.txt"

means “look in the current working directory.”

This path:

"data/example.txt"

means “look in the data folder inside the current working directory.”

If your file is somewhere else, exists() will return False.

2. Forgetting about the current working directory

Python does not always run from the same folder as your script.

Check your current working directory with:

import os
print(os.getcwd())

If needed, see os.getcwd() explained to understand what folder your script is using.

3. Checking after the file was moved or deleted

A path that existed a moment ago may no longer exist.

For example:

  • the file was deleted
  • the file was renamed
  • the file was moved to another folder

So os.path.exists() only tells you what is true at the moment you check.

4. Assuming exists() means the file can be opened

A path may exist, but you can still have problems opening it.

For example:

  • you may not have permission
  • it may be locked by another process
  • it may be a directory, not a file

If you try to open a missing file, Python may raise a FileNotFoundError: Errno 2 No such file or directory.

Path examples beginners often need

Here are some common path formats.

File in the current folder

example.txt

File in a subfolder

data/example.txt

Absolute path

An absolute path starts from the full location on your computer.

Windows example:

C:\Users\YourName\Documents\example.txt

In a normal Python string, backslashes need escaping:

path = "C:\\Users\\YourName\\Documents\\example.txt"

You can also use forward slashes on Windows:

path = "C:/Users/YourName/Documents/example.txt"

macOS/Linux example:

path = "/home/yourname/example.txt"

If you want a beginner-friendly explanation of relative and absolute paths, see working with file paths in Python.

When to use os.path.exists()

os.path.exists() is useful in many real programs.

Use it:

  • before opening a file
  • before deleting or renaming a file
  • before creating something that should not already exist
  • when validating user input paths

Example:

import os

path = input("Enter a file path: ")

if os.path.exists(path):
    print("That path exists.")
else:
    print("That path does not exist.")

Common causes of unexpected results

If os.path.exists() is returning False when you expect True, common causes are:

  • the path string is misspelled
  • the file is in a different folder than expected
  • the script is running from a different working directory
  • a relative path is used when an absolute path is needed
  • you expect exists() to check permissions instead of existence

Useful debugging checks

These quick commands can help you debug path problems:

import os
print(os.getcwd())
import os
print(os.path.exists("example.txt"))
import os
print(os.path.isfile("example.txt"))
import os
print(os.path.isdir("example.txt"))
import os
print(os.listdir())

These checks help you answer questions like:

  • What folder am I running from?
  • Does this path exist?
  • Is it a file?
  • Is it a directory?
  • What files are actually in the current folder?

FAQ

Does os.path.exists() work for folders?

Yes. It returns True for both files and directories if the path exists.

What does os.path.exists() return?

It returns True if the path exists and False if it does not.

What is the difference between os.path.exists() and os.path.isfile()?

exists() checks whether any path exists. isfile() checks whether the path exists and is a file.

Why does os.path.exists() return False when the file is there?

The path may be wrong, or your script may be running from a different working directory than you expect.

See also