Python os.path.isfile()method is “used to check whether the specified path is an existing regular file.”
path: The os.path.isfile() method takes a path as a parameter representing an object representing a file system path. A path-like object is either a string or bytes object representing a path.
The os.path.isfile() method returns a Boolean value of class bool. The os.path.isfile() method returns True if the specified path is an existing regular file. Otherwise, it returns False.
Example 1: How to Use os.path.isfile() Method
import os path = "/Users/krunal/Desktop/code/pyt/database" isFile = os.path.isfile(path) print(isFile)
import os path = "/Users/krunal/Desktop/code/pyt/database/app.py" isFile = os.path.isfile(path) print(isFile)
To check if the file exists in the file system using Python, use the “os.path.isfile()” method. If you attempt to navigate or open a non-existent file, then this will cause an error.