Change language

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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically