How to check if the file is existing or not in python
9 months ago
you may want to know how you can know what are the files already exist in
computer. So, the ability to check whether a file exists on
disk or not is an important task for many types of Python programs.
take an example
you want to make sure a data file is available before you try to load it, or
you want to prevent overwriting an existing file. This is also true with
respect to directories where you try to ensure an output folder is available
before your program runs with an error.
consist several ways which helps us to verify a file or directory exists using
some functions built using the core language and the Python standard library.
in this article, you’ll see different techniques for file existence checks in
One of the most common way which is the simplest among others
methods and widely used to check for the existence of a file in Python is using
the exists() and isfile() methods
from the os.path module
in the standard library of the python programming language.
the above functions are available on Python 2 and Python 3.
Let’s take an example and understand how to work with the os.path.exists() function.
In the below example we are trying to check several paths such as files
and directories whether it exists or not.
In the above example we can see, when we try to call os.path.exists()
it return True for
files and directories
as both are present in the computer. And if the file or directory does not exist
it will return False.
We can use os.path.isfile()
function to ensure that a given path points to a file and
not to a directory.
In the above example we just saw how both the functions in
the os.path module
can be used to check whether a file or a folder exist or not.
Here’s in python another straightforward algorithm is present for
checking whether a file exists. Here, we simply attempt to
open the file with the built-in open() function, which is given below-
“[Errno 2] No such file or
directory: ‘myfile_copy.txt’ ”
If the file exists in the given path the open method will complete successfully open the file
and return a valid file handle. If the file does not exist however, an
error FileNotFoundError exception
will be raised:
“FileNotFoundError is raised when a file or directory is
requested but doesn’t exist. Corresponds to errno ENOENT.” (Source: Python Docs)
This helps us to watch for this FileNotFoundError exception
type in our own code, and use it to detect whether a file exists or not. Below
is an example that will demonstrates this technique:
file = open(“myfile.txt”)
print(“The given file does not exist”)
You may notice in the above example, how I’m immediately calling
the close() method
on the file object to release the underlying file handle. This
is generally considered a good practice when dealing with files in
we don’t close the file handle explicitly as shown in the above example it is
difficult to know when exactly it will be closed automatically by the Python
runtime. It wastes system resources and can make our programs run less
python, another option is also available if we don’t want to close it
explicitly. We use the context manager protocol and the with statement to auto-close the
Now, the same “just attempt to open it” technique also works for
ensuring a file is both readable and accessible. Instead of watching for FileNotFoundError exceptions you’ll want to
look out for any kind of IOError:
file = open(“myfile.txt”)
print(“You cannot access this file”)
print(“You can access this
Now Let’s consider, if we want to use the above technique very
frequently then we can create a helper function that will allow us to test
whether a file exists and is accessible at the same time as show below.
Check if the file or directory at “path” can
be accessed by the program using the
open plays equal to reading mode (r).
file = open(path, mode)
In python there are many more techniques available for handling
different types of file. But, in this article I only explained you about two
Now if you are planning to deal with a file in python afterwards, I
would recommend that you do the existence check via the open() method and exception handling. This will
help us to code better.
I hope after you enjoyed reading this article and finally,
you came to know about How to work with file in python.
For more such blogs/courses on data science, machine
learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.