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

isdir(object)

where object 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. Since the method returns a Boolean type values, we are using the conditional statements if-else to create less ambiguity in the output.

import os
# sub directory is a directory present within the current directory
if os.path.isdir('sub directory'):
   print("Given directory exists") 
else:
   print("Given directory doesn't exist")

Output

If we compile and run the given program, the output is displayed as follows −

Given directory doesn't exist

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 article, we are only trying to check if the directory exists or not; therefore, let us first see an example to check if a specific directory is present in the current directory or not.

import os
if not os.path.exists('my_folder'):
   print("Given directory doesn't exist")
else:
   print("Given directory exists")

Output

Given directory doesn't exist

The output for the given program is produced as follows −

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.

The example is shown below.

from pathlib import Path
p = Path('sub directory')
if p.is_dir():
   print("Given directory exists")
else:
   print("Given directory doesn't exist")

Output

The output for the program above is displayed as −

Given directory doesn't exist

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.

Let us see an example below.

from pathlib import Path
p = Path('new folder')
if p.exists():
   print("Given directory exists")
else:
   print("Given directory doesn't exist")

Output

The output for the program above is displayed as −

Given directory doesn't exist

Conclusion

In this article, we discussed how to check if a directory exists in Python. We will be using the os module which is a part of the standard library.

Updated on: 24-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements