How to find if a directory exists in Python?

Directory creation is a common task for computer users. You might need to create a directory to store some files, or to place some new files inside an existing directory. In this article, you will learn how to find if a directory already exists in Python or not.

A directory is a file system structure used by operating systems and software applications to organize files. It can also refer to a list of files and folders, like the current folder in Windows Explorer.

Sometimes there arises a need to check whether a certain directory exists in a system or not; for example, we need to decide that if we want to save a file in an already existing directory or create a new directory.

There are several ways to check whether a directory exists or not. They are as follows ?

  • Using os.path.isdir() method

  • Using os.path.exists() method

  • Using pathlib module

Using os.path.isdir() Method

In Python, you can use the isdir() function to check if a directory exists. This method can only be used to check if a directory exists; hence, it does not work for files. However, it must be remembered that the directory to be checked should be a sub directory within the current main directory.

Syntax

The syntax for this method is as follows ?

os.path.isdir(path)

where path is the name of the directory you want to check the status of.

Example

In the following example, let us try to check if a directory exists or not using the isdir() method ?

import os

# Check if directory exists
if os.path.isdir('test_folder'):
    print("Given directory exists") 
else:
    print("Given directory doesn't exist")

# Create directory for demonstration
os.makedirs('test_folder', exist_ok=True)

# Check again
if os.path.isdir('test_folder'):
    print("Directory now exists") 
else:
    print("Directory still doesn't exist")

The output of the above code is ?

Given directory doesn't exist
Directory now exists

Using os.path.exists() Method

The os.path.exists() method also works similar to the os.path.isdir() method, but it also checks if files in the current directory exist or not. Therefore, it works for both files and folders of a system.

Syntax

The syntax of this method is given below ?

os.path.exists(path)

where path is the name of a file or directory whose status needs to be checked.

Example

In this example, let us check if a specific directory is present in the current directory or not ?

import os

# Check for existing directory
if os.path.exists('my_folder'):
    print("Given directory exists")
else:
    print("Given directory doesn't exist")

# Create the directory
os.makedirs('my_folder', exist_ok=True)

# Check again after creating
if os.path.exists('my_folder'):
    print("Directory now exists")
else:
    print("Directory still doesn't exist")

The output of the above code is ?

Given directory doesn't exist
Directory now exists

Using pathlib Module

The pathlib module has been introduced since the Python version 3.4; and this module provides various object oriented approaches to handle the file system. In this case, the module contains two methods: exists() and is_dir() to check if a certain directory exists or not.

These methods do not accept any parameters; instead, they are called on file or directory paths.

Example: Using is_dir() Method

The is_dir() method returns a Boolean value, True, if the path points to an existing directory and False, if not ?

from pathlib import Path
import os

# Create Path object
p = Path('demo_directory')

if p.is_dir():
    print("Given directory exists")
else:
    print("Given directory doesn't exist")

# Create directory for demonstration
os.makedirs('demo_directory', exist_ok=True)

# Check again
if p.is_dir():
    print("Directory now exists")
else:
    print("Directory still doesn't exist")

The output of the above code is ?

Given directory doesn't exist
Directory now exists

Example: Using exists() Method

The exists() method returns a Boolean value, True, if the path points to an existing directory and False, if not. However, unlike is_dir() method, the exists() method works for both files and directories ?

from pathlib import Path
import os

# Create Path object
p = Path('sample_folder')

if p.exists():
    print("Given directory exists")
else:
    print("Given directory doesn't exist")

# Create directory
os.makedirs('sample_folder', exist_ok=True)

# Check again
if p.exists():
    print("Directory now exists")
else:
    print("Directory still doesn't exist")

The output of the above code is ?

Given directory doesn't exist
Directory now exists

Comparison of Methods

Method Works for Files? Works for Directories? Python Version
os.path.isdir() No Yes All versions
os.path.exists() Yes Yes All versions
pathlib.Path.is_dir() No Yes 3.4+
pathlib.Path.exists() Yes Yes 3.4+

Conclusion

Use os.path.isdir() when you specifically need to check for directories only. Use pathlib.Path methods for modern, object?oriented file system operations. All methods return Boolean values making them easy to use in conditional statements.

Updated on: 2026-03-24T18:22:05+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements