Which method is used to check whether the specified file is present or not?

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().

Which method is used to check whether the specified file is present or not?
Which method is used to check whether the specified file is present or not?

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses
Select your interest
First name

Last name

Email

Phone number


By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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.

OS Refresher

Python’s built-in os module allows you to access operating system functions in your code.

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:

import os.path

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:

os.path.isfile(path)

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.

» MORE:  Python local variable referenced before assignment Solution

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:

os.path.isdir(directory)

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.

» MORE:  Learn Python for Bioinformatics: Learning Resources, Libraries, and Basic Steps

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:

Which method is used to check whether the specified file is present or not?
Which method is used to check whether the specified file is present or not?

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Find Your Bootcamp Match

import os

os.path.exists('./final_data')

Our final_data directory exists in our example, so our code returns True.

Conclusion

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:

FunctionWhat the Function Determinesos.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.

9 Ratings



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.

Which method is used to check whether the specified file is present or not in Java?

File exists() method in Java with examples The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false.

Which of the following can be used to check whether a file exists or not?

One way is using isfile() function of os. path module. The function returns true if file at specified path exists, otherwise it returns false.

Which method is used to check if the path is of a file?

The pathlib. Path. exists() method is mainly used to check if the given path points to an existing directory or a file.

Which command is used to check whether the file is present or not in current working directory?

The pwd command can be used to determine the present working directory. and the cd command can be used to change the current working directory. When changing directory either the full pathname or the relative pathname is given. If a / precedes the directory name then it is a full pathname, else it is a relative path.