This tutorial will show you how to determine whether a file or directory exists in Python. To check if directory exists Python, there are several methods. These methods keep the programs from crashing.
In particular, being able to check if a file or directory exists lets us run our code safely. Working with files is an essential ability for any Python user, regardless of experience level.
Also read: Python Set Intersection Methods with Best Examples
The significance of checking if a directory or file exists in Python is to avoid overwriting a file that already exists. This method also ensures that the file is present before loading it.
Python offers a variety of alternatives for operating system (OS) interaction. You will therefore learn a variety of Python methods for determining whether a file or directory in Python exists.
How do I find the path of a directory in Python?
exists() method is used to check whether a specific file or directory exists or not. It can also check if a path refers to any open file descriptors or not.
exists() method returns the boolean value true if the file exists and returns false otherwise.
To know how to check if a directory exists in Python without exceptions, we have the following methods:
In some cases, programmers use the
getcwd() function in Python to determine the current working directory. Cwd is for the current working directory in Python.
This Python function returns the path to the current Python directory as a string. To obtain a string as a bytes object, it employs the
How do I check if a directory is empty in Python?
To determine if a directory is empty, the
os.listdir() method is utilized. This function can get a list of all the files and folders in a given directory.
It returns a list containing the names of each directory entry specified by the path and is arranged promptly. It does not include the special entries “
.” and “
..” even though they are present in the directory.
list() produces a list of files and folders in the directory supplied by its path. This file list is kept as a string array. If the length of this string array is larger than 0, then the supplied directory is not empty.
Here’s a method to check if the Directory is empty:
listdir() method can retrieve a list of all of the files and directories that are contained within a given directory. If we don’t choose any folders, we’ll see a list of the files and folders in the active working directory.
import os os.listdir(path='.') if len(os.listdir('.')) == 0: print("Directory is empty") else: print("Directory is not empty")
Directory is not empty
Note: The output varies depending on the path that you provide.
Check if a File or Directory Exists in Python using os.path.exists()
The OS module in Python provides functions for operating system interaction. It belongs to the standard Python utility module. This module provides a portable method for utilizing capabilities depending on the operating system.
os.path module is a submodule of the OS module used to manipulate common path names.
os.path.exists() function in Python is used to determine whether a particular path exists. It can also be used to determine if the specified path corresponds to an open file descriptor.
The path parameter of the
os.path.exists() method represents a file system path.
os.path.exists() method returns a bool Boolean value. If the path exists, the
os.path.exists() method returns True; otherwise, it returns False.
import os path = '/usr/local/bin/' isExist = os.path.exists(path) print(isExist) path = '/home/User/Desktop/file.txt' isExist = os.path.exists(path) print(isExist)
Check if a File or Directory Exists in Python using os.path.isfile()
os.path.isfile() method is used to determine whether the provided path contains an existing regular file.
os.path.isfile() function accepts as a parameter a path object that represents a file system path. A path-like object is a string or byte object which represents a path.
os.path.isfile() method returns a Boolean value of type bool.
os.path.isfile() returns true if the supplied path is a regular file. If not, it returns false.
import os path = 'C:/Users/gfg/Desktop/file.txt' isFile = os.path.isfile(path) print(isFile) path = '/home/User/Desktop/' isFile = os.path.isfile(path) print(isFile)
Check if a File or Directory Exists in Python using os.path.isdir()
os.path.isdir() method can determine if the path is an existing directory. This function returns “true” if the given path is a symbolic link that points to a directory. It is also possible if the given path is also a symbolic link.
isdir() takes a folder path as an argument and determines whether or not it exists. If it exists, it returns True; otherwise, it returns False.
The method returns either True or False as boolean values.
import os.path path = '/home/User/Documents/file.txt' isdir = os.path.isdir(path) print(isdir) path = '/home/User/Documents/' isdir = os.path.isdir(path) print(isdir)
Check if a File or Directory Exists in Python using pathlib.Path.exists()
pathlib() module in Python provides various classes. These classes represent file system paths with semantics appropriate for different operating systems.
pathlib() method comes under Python’s standard utility modules and Path classes. The Pathlib module is divided into pure paths and concrete paths.
Pure paths offer only numerical operations and no I/O operations. On the other hand, concrete paths come from pure paths and can be used for both I/O and computation.
pathlib.Path.exists() method is used to check whether the given path points to an existing file or directory or not.
from pathlib import Path path = '/home/gfg/Desktop' obj = Path(path) print(obj.exists())
There are different ways to check if a path or directory exists in Python. These ways also differ in a function that answers to a specific request. Here are the tips that you need to remember:
os.path.exists() function determines if the path exists or not. It is also used to determine if the specified path corresponds to an open file descriptor.
os.path.isfile() Python function returns True if the path is an absolute pathname.
os.path.isdir() is a built-in Python function used to determine if the supplied path is a directory or not.
isdir() takes a folder path as an argument and states if it exists. If it exists, it returns True; otherwise, it returns False.
pathlib Python module provides an object-based application programming interface for interacting with files and directories.
pathlib() is a standard library module and Path is the primary object for managing files.
In conclusion, this tutorial has discussed the easy ways to check the directory in python. The methods vary to meet the types of files that you need to direct using Python.
The very purpose of checking the path in Python’s directory is to avoid overwriting and conflicts between your files.