Check if File Exists in Python

If you are a Python developer then you may need to check whether a specific file exists or not before performing any action.

This will help you to prevent overwriting to the already existing file. There are several ways you can check whether the file exists or not.

In this tutorial, we will show you three different methods to check if a file exists in Python.

Check if File Exists

The simple and easiest way to check file existence is to open the file and perform some action.

You don’t need to import any module to check the file. This method works on Python 2 and 3.

To understand this better, let’s take an example. The following Python script will open the file named file-not-exists and if the file does not exist, it will print the message “File is not exists”:


try:
f = open("file-not-exists")
except IOError:
print("File is not exists")
finally:
f.close()

To run the above code, you will need to connect to the Python shell with the following command:

python3

Once you are connected, paste the above code and you will get the result as shown below.

It is also recommended to use with keyword to close the file after performing the action.

The following Python script will open the file named /etc/hostname, if it does not exists it will print the message “File is not exists” otherwise it will open the file, print the file content and close the file:


try:
with open('/etc/hostname') as f:
print(f.readlines())
except IOError:
print("File is not exists")

Now, run the above code in the Python shell. You should see the following output:

Check if File Exists using the os.path Module

The most common method to check the file existence in Python is by using os.path module with exists() and isfile() functions. Both functions support on Python2 and Python3 versions.

In the following example, we will check whether the file /opt/myfile.txt exists or not before performing any action.

If the file is exists it will print “File is exist” else it will print “File is not exist”:


import os.path


if os.path.isfile('/opt/myfile.txt'):
print ("File is exist")
else:
print ("File is not exist")

Now, run the above code in the Python shell. You should see the following output:

You can also check the directory existence using the isdir() function.

In the following example, we will check if the path /opt is a directory or not.

If the path is a directory it will print “This is a directory” else it will print “This is not a directory”


import os.path

if os.path.isdir('/opt'):
print ("This is a directory")
else:
print ("This is not a directory")

Now, run the above code in the Python shell. You should see the following output:

Check if File Exists using the pathlib Module

pathlib module is used to check whether the specified path is a directory or file.

pathlib module supports Python version 3.4 and above and used for handling with file system path.

In the following example, we will check whether the file /opt/myfile.txt exists or not using the pathlib module:


from pathlib import Path


if Path('/opt/myfile.txt').is_file():
print ("File exist")
else:
print ("File is not exist")

Now, run the above code in the Python shell. You should see the following output:

You can use is_dir function if you want to check a directory existence.

Conclusion

In the above guide, we’ve learned how to check file or directory existence in Python using several methods. We hope you can now use any of the above methods to check the file or directory existence in your daily coding and administration practices!

Leave a Reply

Your email address will not be published. Required fields are marked *