IsADirectoryError: Path is a Directory

IsADirectoryError: [Errno 21] Is a directory: '/tmp/data'

Quick Answer

You are trying to open a directory as a file. Check your path to make sure it includes the filename, not just the directory.

Why This Happens

The open() function expects a file path, not a directory. This error occurs when your path variable contains a directory instead of a file.

The Problem

with open('/tmp/data') as f:
    content = f.read()

The Fix

with open('/tmp/data/config.json') as f:
    content = f.read()

Step-by-Step Fix

  1. 1

    Check the path

    Verify with Path(path).is_file().

  2. 2

    Add the filename

    Append the filename: Path(dir_path) / 'file.ext'.

  3. 3

    List directory contents

    Use os.listdir(path) to see files in the directory.

Got the actual stack trace?

Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.

Explain my error

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free