A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-os-path-isfile-method/ below:

Python os.path.isfile() method - GeeksforGeeks

Python os.path.isfile() method

Last Updated : 11 Jul, 2025

The os.path.isfile() method in Python is used to check if a path refers to a regular file (not a directory or any other type of file system object). This method can be particularly useful when you need to validate file paths before performing file operations like reading, writing, or modifying files. It returns a boolean value:

Example: Use of os.path.isfile() method

This code demonstrates the use of the os.path.isfile() method in Python to check whether a given path is a file or not.

Python
import os

path = '/home/User/Desktop/file.txt'
 
isFile = os.path.isfile(path)
print(isFile)

path = '/home/User/Desktop/'

isFile = os.path.isfile(path)
print(isFile)

Explanation:

Syntax:

os.path.isfile(path)

Parameters: Return Value:

The method returns:

Example of os.path.isfile() method 1. Checking if a File Exists: Python
import os

# Example file path
path = "example.txt"

# Check if the file exists
if os.path.isfile(path):
    print(f"{path} exists and is a regular file.")
else:
    print(f"{path} does not exist or is not a file.")

Output
example.txt does not exist or is not a file.

Explanation: The code checks if "example.txt" is a valid file. It prints whether the file exists and is a regular file or not.

2. Checking for a Directory: Python
import os

# Example directory path
path = "/home/user/documents"

# Check if it's a file or not
if os.path.isfile(path):
    print(f"{path} is a file.")
else:
    print(f"{path} is not a file.")

Output
/home/user/documents is not a file.

Explanation: The code checks if "/home/user/documents" is a valid file. It prints whether the path is a file or not.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4