Python — Check if file or directory exists

There are various ways to check if a file or directory exists or not. They are —

Using os.path.exists ()

OS Module in Python provides functions for interacting with the operating system. The OS comes with standard Python service modules. This module provides a portable way to use operating system specific functionality. The os.path module is a submodule of the OS module in Python that is used to manage shared paths.

Note. To find out more about OS module,

# Python program to explain the os.path.exists () method

 
# import of the os module

import os 

 
# Specify path

path = `/ usr / local / bin /`

 
# Check if
# path exists or not

isExist = os.path.exists (path) 

print (isExist) 

 

 
# Specify path

path = `/ home / User / Desktop / file.txt`

 
# Check if
# path exists or not

isExist = os.path.exists (path) 

print (isExist) 

Exit:

 True False 

Using os.path.isfile ()

os.path.isfile () in Python is used to check if whether specified or not.

Syntax: os.path.isfile (path)

Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.

Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing regular file, otherwise returns False.

Example :

# Python program to explain the os.path.isfile () method

 
# import of the os module

import os

 
# Path

path = `C: / Users / gfg / Desktop / file.txt`

 
# Check
# specified path
# existing file

isFile  = os.path.isfile (path)

print (isFile)

  
# Path

path = `/ home / User / Desktop /`

 
# Check
# specified path
# existing file

isFile = os.path.isfile (path)

print (isFile)

Exit :

 True False 

Using os.path.isdir ()

os.path.isdir () in Python is used I am to check if the specified path is an existing directory or not. This method follows a symbolic link, which means that if the specified path is a symbolic link pointing to a directory, then the method will return True .

Syntax: os.path.isdir (path)

Parameter:
path: A path-like object representing a file system path .

Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing directory, otherwise returns False.

Example :

# Python program to explain the os.path.isdir () method

 
# import of the os.path module

import os.path 

 
# Path

path = `/ home / User / Documents / file.txt`

 
# Check
# the specified path is
# existing directory or not

isdir = os.path.isdir (path) 

print (isdir) 

 

 
# Path

path = `/ home / User / Documents /`

 
# Check
# the specified path is
# existing directory or not

isdir = os.path.isdir (path) 

print (isdir) 

Exit:

 False True 

Example: if the specified path is symbolic link.

# Python program to explain the os.path.isdir () method

 
# import os.path module

import os.path 

  

 
# Create directory
# (in the current working directory)

dirname = "GeeksForGeeks"

os.mkdir (dirname) 

 
# Create a symlink
# points to the above directory

symlink_path = " / home / User / Desktop / gfg "

os.symlink (dirname, symlink_path) 

 

 

path = dirname 

 
# Now check
# specified path is
# existing directory or not

isdir = os.path.isdir (path) 

print (isdir) 

 

path = symlink_path 

  
# Check
# the specified path (which is
# symlink) is
# existing directory or not

isdir = os.path.isdir (path) 

print (isdir) 

Exit :

 True True 

Using pathlib.Path.exists ()

The Pathlib module in Python provides various classes representing file system paths with semantics suitable for different operating systems. This module is included in the standard Python service modules.  Path classes paths in the Pathlib module are clean and specific paths . Pure paths only provide computational operations and do not provide I / O, while concrete paths inherit from pure paths provide both computational and I / O.

Note. To learn more about the pathlib module,

# Path import class

from pathlib import Path 

 
# Path

path = `/ home / gfg / Desktop`

 
# Create the Path class

obj = Path (path) 

 
# Check if the path points to
# existing file and or directory

print (obj.exists ()) 

Exit :

 True