In file manipulation programs, you often need to distinguish whether a specified path is a “file” or a “directory (folder)” to handle them differently. For example, after getting a list of items in a directory, you might want to perform a read operation only on the files.
The Python standard library os.path module provides specialized functions for this purpose: isfile() and isdir().
This article explains how to use these two functions and how they behave when a path does not exist.
1. Checking if it is a File: os.path.isfile()
The os.path.isfile() function returns True only if the provided path is an existing file.
- If it is a directory: Returns
False - If it does not exist: Returns
False
Syntax:
import os
result = os.path.isfile(path)
Specific Usage Example:
Here is an example checking if monthly_report.pdf exists as a file.
import os
# Paths to check
file_target = "monthly_report.pdf"
dir_target = "backup_folder"
# Check if they are files
is_file_1 = os.path.isfile(file_target)
is_file_2 = os.path.isfile(dir_target)
print(f"Is '{file_target}' a file?: {is_file_1}")
print(f"Is '{dir_target}' a file?: {is_file_2}")
Execution Result (Assuming both exist in the current directory):
Is 'monthly_report.pdf' a file?: True
Is 'backup_folder' a file?: False
2. Checking if it is a Directory: os.path.isdir()
The os.path.isdir() function returns True only if the provided path is an existing directory.
- If it is a file: Returns
False - If it does not exist: Returns
False
Syntax:
import os
result = os.path.isdir(path)
Specific Usage Example:
Here is an example checking if a backup folder exists.
import os
# Paths to check
dir_target = "backup_folder"
file_target = "monthly_report.pdf"
# Check if they are directories
is_dir_1 = os.path.isdir(dir_target)
is_dir_2 = os.path.isdir(file_target)
print(f"Is '{dir_target}' a directory?: {is_dir_1}")
print(f"Is '{file_target}' a directory?: {is_dir_2}")
Execution Result:
Is 'backup_folder' a directory?: True
Is 'monthly_report.pdf' a directory?: False
3. Checking Non-Existent Paths
An important point to note is that both isfile() and isdir() return False if the target path does not exist.
The logic “Not a file = It is a directory” does not hold true because there is a third state: “Neither file nor directory (does not exist).”
import os
# A path that does not exist
ghost_path = "phantom_file.txt"
print(f"Exists check: {os.path.exists(ghost_path)}")
print(f"File check: {os.path.isfile(ghost_path)}")
print(f"Directory check: {os.path.isdir(ghost_path)}")
Execution Result:
Exists check: False
File check: False
Directory check: False
Practical Example: Classifying Items in a List
When processing a list of items in a directory, you can separate files and directories like this:
import os
# List of paths to process (assuming mixed types)
path_list = ["config.ini", "logs", "setup.py", "assets"]
print("--- Classification Start ---")
for path in path_list:
if os.path.isfile(path):
print(f"[FILE] Reading {path}")
elif os.path.isdir(path):
print(f"[DIR ] Scanning contents of {path}")
else:
print(f"[None] {path} not found")
Execution Result (Example):
--- Classification Start ---
[FILE] Reading config.ini
[DIR ] Scanning contents of logs
[FILE] Reading setup.py
[DIR ] Scanning contents of assets
Summary
- os.path.isfile(path): Returns
Trueif it is an existing file. - os.path.isdir(path): Returns
Trueif it is an existing directory. - If the path does not exist, both return
False.
Use these functions to identify the type of the target before performing file operations.
