To check if, in Python, a file exists use the
os module to call one of three methods:
isfile() checks if a file exists,
isdir() checks if a directory exists, and
exists() checks for both.
Checking whether or not a certain file or directory exists has a number of uses in Python. Perhaps you want to check if a specific file exists so that, if it doesn’t, you can create the file you need for your program to work properly. Or perhaps you want to check if a certain directory exists because you have a program that works with folders in that directory.
That’s where the Python
os module comes in. The os module includes three methods that you can use to check if a certain file or directory exists:
In this guide, we will discuss how you can check whether or not a certain file or directory exists using the
exists() methods in Python. We will explore a few examples of each of these methods in action to illustrate how they work.
os module allows you to access operating system functions in your code. For example, the module includes methods that allow you to create, find, and delete files. The
os module works for all major operating systems, so you can use it when you’re running on Mac, Windows, or Linux.
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 or not certain files or directories exist, so we only need to import the
os.path module. We can do so using the
import statement. Here’s how we can import
os.path into our code:
Now that we have imported the
os.path module, we are ready to start checking if certain files or directories exist.
Checking If a Certain File Exists
In Python, we can use the
os.path.isfile() method to check whether a certain file exists. This method returns
False depending on whether that file can be found.
Here’s the syntax for the
Let’s go through an example to show how you can use this method. 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 not, we will need to create one.
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
True. If there is no existing file (and therefore no existing file path), our code will return
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:
Checking If a Certain Directory Exists
What if you want to check whether a certain directory exists using Python? That’s where the
isdir() method comes in.
isdir() method takes in one argument: the directory whose existence you want to verify. If the directory can be found,
isdir() will return
True. Otherwise, the program will return
False. Here’s the syntax for the
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')
In this case, our directory does exist, so our program returns
Similar to how the
isfile() method responds to directories, if you try to use
isdir() to check whether a file exists, the method will return
Checking If a Certain File or Directory Exists
In Python, you can check whether or not certain files or directories exist using the
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
But what if you want to check whether or not 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')
In this case, our file exists, so our program returns
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')
final_data directory exists in our example, so our code returns
The following table summarizes when to use what function in Python you should use to determine whether or not 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:
When you’re working in Python, you may want to check whether or not certain files and/or directories exist before allowing your program to proceed. The
exists() methods allow you to do so.
In this tutorial, we discussed how to use
isfile() to check if a certain file exists and
isdir() to check if a certain directory exists. We also discussed how to use
exists() to check whether or not a certain file or directory exists.
After reading this article, you have the knowledge you need to check if certain files or directories exist in Python like an expert!