How to check directory exists in Python

Python PythonFile

In this post, We will learn how to check directory exists in Python. Sometimes while performing an operation on files and directories, first we have to check whether they exist in System.Python os module provide three methods isdir(), and exists(). In Python version 3.4+ pathlib module is_dir() method its used to handle file system path.

Ways to check directory Exist in Python


  • os module isdir() method
  • os module exists() method
  • pathlib Module is_dir()
  • unipath module exists()

1. isdir() to Check directory exist in Python


The os. path.isdir() method check if a directory exists at the given path. It returns True if the directory exists else it returns False.

Syntax

os.path.isdir(path)

Parameter

  • path: The path of a directory that we want to check exists or not.

Program to Check directory exists in Python

In this example, we are passing the directory path as an argument to os. path.isdir(path) method. If it returns TRUE that means the directory exists at the specified path when it returns false that means the directory does not exist.

#Python Program to check if directory exists at the specified path

import os.path
from os import path

if path.os.path.isdir('test') :
 print ("Directory  exists")
else:
 print ("Directory not exists")

Output

Directory  exists

2. Using os.path.exists() method


The python os module exists() method allows us to find out if a directory or file exists at a specific path.We pass the path of the directory to check directory exists or not.

Syntax

os.path.exists(path)

Parameters

  • path : It is required parameter we can pass file or directory path to this method as an argument.

Program to check Directory exist in Python

In this example, we are passing the directory path as an argument to os.path.exists() method.If it returns TRUE that means the directory exists at the specified path and prints “Directory exists” else it returns False.

Steps to check directory exists using os. path.exists()


  • First import the os. path module using import os.path.
  • check directory exists using the os module exists() method.
  • The exists() method returns TRUE if a directory exists else return FALSE.when it return true that mean directory exist else directory does not exist.
#Python Program to check directory exists using exists() Method

import os.path
from os import path

if (path.exists('test')) :
 print ("Directory exists")
else:
 print ("Directory not exists")

Output

Directory exists

3. Using pathlib Module is_dir()


In Python version 3.4+ pathlib module use to handling with file system path. In this example, we will use the is_dir() method to check file exists or not. if a directory exists is_dir() method return true we will print “directory exists” else when it returns False we are printing “directory does not exist”

Program Example

#Python Program to check directory exists using is_dir() method


from pathlib import Path
if Path('test').is_dir():
    print('Directory Exist')
else:
   print('Directory does not exist')

Output

Directory Exist

4. Unipath module to check file exist in Python


The unipath module exists() method it returns boolean value true and false. If a directory exists then it returns True else returns False based on this we print the result.

  • To use this module first we have to install it by using pip install unipath
  • Import the installed module by using code “from unipath import Path”.
  • Use the Exists() to check directory exist or not

Python Program Example

from unipath import Path

if Path('test').exists():
    print('Directory Exist')
else:
   print('Directory does not exist')

Output

Directory Exist

Summary :

In this post, we have explored how to check directory exist in Python with examples by Using Python os module methods isdir(), and exists(), another module is pathlib module.is_dir() method its added in Python version 3.4+,The unipath module exists() method.