Last Updated : 24 Apr, 2024
Summarize
Comments
Improve
Sometimes it’s necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file.
In this tutorial, we will cover an important concept of file handling in Python about How to check if a file already exists in Python. We will cover four methods to check if a file or directory is already present.
How to check if a File or Directory Exists in Python?
To check if a file or directory already exists in Python, you can use the following methods:
- os.path.exists(path): Checks if a file or directory exists at the given path.
- os.path.isfile(path): Checks if a file exists at the given path.
- os.path.isdir(path): Checks if a directory exists at the given path.
- pathlib.path.exists(): Checks if the represented file or directory exists (part of the Pathlib object).
Using os.path.exists() to Check if a File or Directory Exists
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality.
os.path module is a submodule of the OS module in Python used for common path name manipulation.
os.path.exists() method in Python is used to check whether the specified path exists or not. You can use this method to check if a file or directory exists. This method can also be used to check whether the given path refers to an open file descriptor or not.
Syntax: os.path.exists(path)
Parameter:
- path: A path-like object representing a file system path.
Return: Returns TRUE if the path exists else FALSE.
Example: Checking if a path exists using os.path.exists()
import os # Specify path path = '/usr/local/bin/' # Check whether the specified # path exists or not isExist = os.path.exists(path) print(isExist) # Specify path path = '/home/User/Desktop/file.txt' # Check whether the specified # path exists or not isExist = os.path.exists(path) print(isExist)
Output:
TrueFalse
Using os.path.isfile() Method to Check if the File Exists
os.path.isfile() method in Python is used to check if a file exists or not. It checks whether the specified path is an existing regular file or not.
Syntax: os.path.isfile(path)
Parameter:
- path: A path-like object representing a file system path.
Return Type: Returns TRUE if file exits, else FALSE
Example: Checking if a path pointing to a resource is a file
import os# Path path = 'C:/Users/gfg/Desktop/file.txt'# Check whether a path pointing to a fileisFile = os.path.isfile(path)print(isFile)# Pathpath = '/home/User/Desktop/'# Check whether the path is a fileisFile = os.path.isfile(path)print(isFile)
Output:
TrueFalse
Using os.path.isdir() Method to Check if Directory Exists
os.path.isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows a symbolic link, which means 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: Returns TRUE if directory exists, else FALSE
Example 1: Check if a path is a directory using os.path.isdir()
import os.path # Path path = '/home/User/Documents/file.txt' # Check whether the path is an existing directoryisdir = os.path.isdir(path) print(isdir) # Path path = '/home/User/Documents/' # Check whether the path is a directoryisdir = os.path.isdir(
Output:
FalseTrue
Example 2: If the specified path is a symbolic link.
import os.path # Create a directorydirname = "GeeksForGeeks"os.mkdir(dirname) # Create a symbolic link # pointing to above directory symlink_path = "/home/User/Desktop/gfg"os.symlink(dirname, symlink_path) path = dirname # Check whether the specified path is an # existing directory or not isdir = os.path.isdir(path) print(isdir) path = symlink_path # check whether the symlink is # an existing directory or notisdir = os.path.isdir(path) print(isdir)
Output:
TrueTrue
Using pathlib.Path.exists() to Check if the File or Directory Exists
pathlib module in Python provides various classes representing file system paths with semantics appropriate for different operating systems. This module comes under Python’s standard utility modules.
Path classes in pathlib module are divided into pure paths and concrete paths. Pure paths provide only computational operations but do not provide I/O operations, while concrete paths inherit from pure paths to provide computational as well as I/O operations.
pathlib.Path.exists() method is used to check whether the given path points to an existing file or directory or not.
Syntax: pathlib.Path.exists(path)
Parameter:
- path: A path-like object representing a file system path.
Return Type: Returns TRUE if file or directory exists, else FALSE
Example: Check if the path exists using pathlib module
# Import Path classfrom pathlib import Path# Pathpath = '/home/tuhingfg/Desktop'# Instantiate the Path classobj = Path(path)# Check if path existsprint("path exists?", obj.exists())
Output:
True
In this tutorial, we have covered 4 methods on how to check if a file or directory already exists in Python. We have covered the use of OS module and pathlib module with their respective functions like os.path.isfile(), os.path.isdir(), pathlib.path.exists(), etc. These are some of the easiest methods you can try to check if the file already exists in Python.
Next Article
Python: Check if a File or Directory Exists