Check if a File is a Directory or Regular File in Python



In Python programming, it is important to check whether a given path points to a file or a directory. This check is very useful before performing operations such as reading, writing or listing contents. In this article, we'll see the different methods to identify file types using Python.

Why should we check the file type?

Before interacting with a file path, it is important to confirm the type of the file to make sure that the code behaves as expected. If the code treats a directory as a file or file as a directory, it can cause errors or data mishandling -

  • Preventing operations meant for files from being used on folders
  • Avoiding accidental modification of directories
  • Executing different logic based on whether the path is a file or directory

Using os.path module

The os.path module in Python has functions such as isfile() and isdir() to determine the type of a given path. It is a simple and reliable way to perform the file type check.

Example

This example uses os.path to verify if the path is a regular file or a folder -

import os

path = r"D:\Tutorialspoint\Articles\file1.txt"

if os.path.isfile(path):
    print("This is a file.")
elif os.path.isdir(path):
    print("This is a directory.")
else:
    print("The path does not exist.")

Following is the output of the above program, which uses the  os.path module -

This is a file.

Using pathlib module

The pathlib module in Python provides an object-oriented approach to handle filesystem paths. This module have the methods such as is_file() and is_dir() for checking file types.

Example

The following example uses pathlib.Path to identify whether a path is a file or a folder -

from pathlib import Path

path = Path(r"D:\Tutorialspoint\Articles")

if path.is_file():
    print("This is a file.")
elif path.is_dir():
    print("This is a directory.")
else:
    print("The path does not exist.")

Below is the output of the above program which uses pathlib.Path module -

This is a directory.

Using os.stat() with stat module

When we need a more low-level or detailed way to check file types, Python provides the os.stat() function. When we combine the os.stat() with the stat module, it allows us to examine the file mode and determine whether the path is a regular file, directory, or something else.

Example

Here is an example that shows how to check a file type using os.stat() module -

import os
import stat

path = r"D:\Tutorialspoint\Articles"

try:
    mode = os.stat(path).st_mode
    if stat.S_ISREG(mode):
        print("This is a file.")
    elif stat.S_ISDIR(mode):
        print("This is a directory.")
    else:
        print("The path is of unknown type.")
except FileNotFoundError:
    print("The specified path does not exist.")

Below is the output of the above program -

This is a directory.
Updated on: 2025-05-02T16:50:13+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements