Check if the directory exists in Python
To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent overriding the file that already exists. Once the existence is assured, then the file can be loaded and used as required.
The word directory has many meanings in different fields, and in the computing field, it refers to the folder or the file system structure in which the computer files are stored. These files can be of any type, i.e., “.exe”, “.txt”, “zip”, etc.
Therefore, to check the existence of the directory or the file in Python, the following methods can be used:
- Using the os.path.exists() method
- Using the os.path.isfile() method
- Using the os.path.isdir() method
- Using the pathlib.Path.exists() method
Method 1: Using the “os.path.exists()” method to check the existence of the directory or the file
To use this method, first, we have to import the “OS module”. This module of Python provides multiple functions that are used to interact with the operating system. This module comes in the domain of Python’s standard utility modules. A portable way of using functions that depend on the operating system is provided by this module.
- os.path is used to manipulate the name for the common path. We use the os.path.exists() method to check whether the path exists or not. This method can also be used to determine whether the given path refers to an open file descriptor or not.
Syntax:
os.path.exists(path)
In this syntax, the path is passed as a parameter to this method. This parameter is used to represent the path of the file. It is generally a string or bytes object that represents a path.
A Boolean value of the bool class is returned by this method. We get the output as True if the directory or the file exists, or else this method will return False as output.
Example:
# Importing the OS module as discussed earlier
importos
# Specifying path to pass as a parameter in the method
pth ='/usr/local/bin/'
# Checking the existence of the specified path
Exst =os.path.exists(pth)
print(Exst)
# Specifying path to pass as a parameter in the method
pth1='/home/User/Desktop/file.txt'
# Checking the existence of the specified path
Exst1=os.path.exists(pth1)
print(Exst1)
Output:
False
False
Method 2: Using the “os.path.isfile()” method to check the existence of the directory or the file
This method also uses the OS module to perform this specified task. In Python os.path.isfile method is used to verify whether the specified path does exist as a regular file or not.
Syntax:
os.path.isfile(path)
In this syntax, the same as the first method, the path is passed as a parameter to this method. This parameter is used to represent the path of the file. It is generally a string or bytes object that represents a path.
A Boolean value of the bool class is returned by this method. We get the output as True if the directory or the file exists, or else this method will return False as output.
Example:
# Importing the OS module as discussed earlier
importos
# Specifying path to pass as a parameter in the method
pth ='C:/Users/gfg/Desktop/file.txt'
# Checking whether the specified path has an existing file or not
Fle =os.path.isfile(pth)
print(Fle)
# Specifying path to pass as a parameter in the method
pth1='/home/User/Desktop/'
# Checking whether the specified path has an existing file or not
Fle1=os.path.isfile(pth1)
print(Fle1)
Output:
False
False
Method 3: Using the “os.path.isdir()” method to check the existence of the directory or the file
We use the os.path.isdir() method to check whether the path specified is an existing directory or not. This method is designed to follow a symbolic link, i.e., this method returns True as output when the provided path is a symbolic link that is pointing to the directory.
Syntax:
os.path.isdir(path)
In this syntax, the same as the first method, the path is passed as a parameter to this method. This parameter is used to represent the path of the file. It is generally a string or bytes object that represents a path.
A Boolean value of the bool class is returned by this method. We get the output as True if the directory or the file exists, or else this method will return False as output.
Example:
# Importing the os.path module
importos.path
# Path
pth ='/home/User/Documents/file.txt'
# Check whether the
# specified path is an
# existing directory or not
isdir =os.path.isdir(pth)
print(isdir)
# Path
pth ='/home/User/Documents/'
# Check whether the
# specified path is an
# existing directory or not
isdir =os.path.isdir(pth)
print(isdir)
Output:
False
False
Method 4: Using the “pathlib.Path.exists()” method to check the existence of the directory or the file
The “pathlib.Path,exists()” method is the final method to check the status of the given directory. To use this method, the path class from the Pathlib module has to be imported. There are multiple classes available in the Pathlib module that provides the paths of the file systems, with appropriate semantics for multiple operating systems.
Python’s standard utility module is the one containing this module. The path class imported from this module is divided into two parts: concrete path and pure path. Computational operations are provided by the pure path, but it does not provide I/O operations. Whereas, the concrete path, inherited from the pure path, provides both, computational operations and I/O operations too.
This method simply checks whether the path provided points to the file or the directory or not.
Syntax:
pathlib.Path.exists(path)
In this syntax, the same as the first method, the path is passed as a parameter to this method. This parameter is used to represent the path of the file. It is generally a string or bytes object that represents a path.
A Boolean value of the bool class is returned by this method. We get the output as True if the directory or the file exists, or else this method will return False as output.
Example:
# Import the Path class from the pathlib module
frompathlib importPathas Pt
# Specifying the path to pass as a parameter in the method
pth ='/home/gfg/Desktop'
# Instantiate the Path class
objct=Pt(pth)
# Checking whether the specified path has an existing directory or not
print(objct.exists())
Output:
False