Python os.path.isfile() Method

Python os.path.isfile()method is “used to check whether the specified path is an existing regular file.”

Syntax

os.path.isfile(path)

Parameters

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.

Return Value

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)

Output

False

Example 2

import os

path = "/Users/krunal/Desktop/code/pyt/database/app.py"

isFile = os.path.isfile(path)

print(isFile)

Output

True

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. 

That’s it.

Related posts

Python os.path.exists()

Python os.path.split()

Python os.path.dirname()

Python os.path.basename()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.