The Python os.path.isdir() method checks if a directory exists. os.path.isfile() checks whether a file exists. Both of these methods are part of the Python os library.
Checking whether a certain file or directory exists has a number of uses in Python. The os module includes three methods that you can use to check if a certain file or directory exists: isfile(), isdir(), and exists().
In this guide, we will discuss how you can check whether a certain file or directory exists using the isfile(), isdir(), and exists() methods in Python. We will explore a few examples of each of these methods in action to illustrate how they work.
Python’s built-in os module allows you to access operating system functions in your code.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Os is a Python module, which means that before we use it we need to import the module into our code. We will only be using the module’s path point functions to check whether certain files or directories exist. So, we only need to import the os.path module.
We can do so using the Python import statement:
Python Check if File Exists
The os.path.isfile() method checks if a file exists in Python. os.path.isfile() returns True or False, depending on whether that file can be found. This method returns False if you specify a directory as an argument.
Here’s the syntax for the isfile() method:
isfile() accepts one argument: the name of the file whose existence you want to verify. “path” represents the file path in the above example. Let’s go through an example to show how you can check if a file exists in Python.
Check if File Exists Python Example
Say that we are creating a program that analyzes data on avocado yields for a local farm. Before conducting our analysis, we want to check whether we have a processed file in place in which we can store our analyzed data. If the file does not exist, we will need to create it.
Our processed file should be called ./final_data.csv. We can use the following code to check whether this file exists:
import os os.path.isfile('./final_data.csv')
If our file path exists, our code will return the Python boolean value True. If there is no existing file (and therefore no existing file path), our code will return False.
The isfile() method only works for files; it does not work for directories. If you use isfile() to check whether a directory exists, the method will return False. Here’s an example of isfile() being used to check if a directory exists:
import os os.path.isfile('./final_data_folder')
Our code returns: False.
Python Check if Directory Exists
The Python os.path.isdir() method checks if a directory exists. It returns False if you specify a path to a file or a directory that does not exist. If a directory exists, isdir() returns True.
Here’s the syntax for the isdir() method:
The isdir() method takes in one argument: the directory whose existence you want to verify.
Check if Directory Exists Python Example
Say that we want to check whether the folder final_data_folder exists. We plan to use this directory to store the data processed by our avocado data analysis program.
We could use the following code to check if this directory exists:
import os os.path.isdir('./final_data_folder')
Our directory does exist, so our program returns True.
If you try to use isdir() to check whether a file exists, the method will return False. This is similar to how the isfile() method responds to directories.
Checking If a Certain File or Directory Exists in Python
In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively.
However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.
But what if you want to check whether a certain file or directory exists? You can use the os.path.exists() method to perform this action.
Say that you want to know whether the file ./final_data_2020.csv exists. You could use the following code to check if that file exists:
import os os.path.exists('./final_data_2020.csv')
Our file exists, so our program returns True.
In addition, if we want to check whether the directory ./final_data exists, we can use os.path.exists() for this as well, as follows:
import os os.path.exists('./final_data')
Our final_data directory exists in our example, so our code returns True.
When you’re working in Python, you may want to check whether certain files and/or directories exist before allowing your program to proceed. The isfile(), isdir() and exists() methods allow you to do so.
The following table summarizes when to use what function in Python you should use to determine whether certain files or directories exist:
|Function||What the Function Determines|
|os.path.isfile(‘file’)||Does ‘file’ exist?|
|os.path.isdir(‘directory’)||Does ‘directory’ exist?|
|os.path.exists(‘file/directory’)||Does ‘file/directory’ exist?|
Remember that before using these functions, you first need to import Python’s os path point module. To do so, use the following code: import os.path.
Read our How to Learn Python article for advice on how you can acquire the skills you need to master Python.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.