Last Updated On By Anmol Lohana
Usually, Python provides many unlimited functionalities, and checking a file exists or directory is one of them. Python check if file exists can be done using Python’s built-in OS module. OS module in Python allows interaction with the operating system.
Python OS module has a submodule os.path, which is used to perform the task of Python check if a file exists or not. There are different ways to find out whether a Python file or directory exists in the system or not.
In this article, we will learn how to check if file exists Python. Let’s discuss different ways to check if file exists in Python.
Table of Contents
The method Python os.path.exists() will check the file or directory exists at the given path or not. Before executing this method or code, we need to import the os.path module in our program code to use the path.exists() function. It will return Boolean values true if the file exists; else, it will return false.
import os.path
from os import path
Here, find.py is a file in working directory that we want to search for.
import os.path
from os import path
def main():
print ("File find.py exists in working directory: "+str(path.exists('find.py')))
print ("File booklist.txt exists in writing directory: " + str(path.exists('writing.booklist.txt')))
print ("directory writing exists: " + str(path.exists('writing')))
if __name__== "__main__":
main()
The method Python os.path.isfile() is used to find the given path. Before executing this method or code, we need to import the os.path module in our program code to use the path.isfile() function. It will return Boolean values true if it finds the file; else, it will return false.
import os.path
from os import path
path.isfile("find.py")
Here, find.py is a file in working directory that we want to search for.
import os.path
from os import path
def main():
print ("Is find.py a file? : "+str(path.isfile('find.py')))
print ("Is booklist.txt is a file inside writing directory? : " + str(path.isfile('writing.booklist.txt')))
if __name__== "__main__":
main()
The method Python os.path.isdir() is used to find the directory at the given path. Before executing this method or code, we need to import the os.path module in our program code to use the path.isdir() function. It will return Boolean values true if it finds the file; else, it will return false.
import os.path
from os import path
path.isdir("find.py")
Here, find.py is a file in working directory that we want to search for.
import os.path
from os import path
def main():
print ("Is find.py a directory? : "+str(path.isdir('find.py')))
print ("Is booklist.txt is a directory inside writing directory? : " + str(path.isdir('writing.booklist.txt')))
if __name__== "__main__":
main()
The method Python pathlibPath.exists() is used to check file existence at object-oriented approach. Before executing this method or code, we need to import the pathlib module in our program code to use pathlib.Path() function. It will return the value of the if statement while the file exists, and if the file does not exist, it will return the value of the else statement.
import pathlib
pathlib.Path("find.py")
Here, find.py is a file in working directory that we want to search for.
import pathlib
file = pathlib.Path("find.py")
if file.exists ():
print ("File find.py exist")
else:
print ("File find.py does not exist")
In conclusion, we had a look at the process of check if a file exists in Python the system or not. We saw four different ways of finding it with coding examples. We need to import required submodules of the OS module like os.path and pathlib.