pola-rs/polars · error · IsADirectoryError

expected a file path;

Error message

expected a file path; {path!r} is a directory

What it means

normalize_filepath validates that a given path string denotes a file, not a directory. If check_not_directory is set and the path exists and is a directory, it raises IsADirectoryError with the offending path repr. It protects IO APIs (read/write/serialize) from being handed a directory where a file path is required.

Solutions

  1. Append a file name with extension to the path: df.write_parquet('data/out.parquet').
  2. Check os.path.isdir(path) before calling and handle accordingly.
  3. If you intended to read many files in a directory, use a glob or scan source list instead of the bare directory (where supported).

Example fix

// before
df.write_parquet('out_dir/')
// after
df.write_parquet('out_dir/data.parquet')
Defensive patterns

Strategy: validation

Validate before calling

import os
if os.path.isdir(path):
    path = os.path.join(path, 'data.parquet')

Type guard

def is_file_path(p) -> bool:
    import os
    return os.path.exists(p) and not os.path.isdir(p)

Try / catch

try:
    df.write_parquet(path)
except IsADirectoryError:
    df.write_parquet(os.path.join(path, 'data.parquet'))

Prevention

When it happens

Trigger: Calling pl.read_*/scan_*/write_*, serialize_polars_object, write_json/write_ndjson, or deserialization with a path that points to an existing directory (e.g. df.write_parquet('out_dir/') or read_csv on a folder).

Common situations: Passing an output directory instead of a file name, forgetting the file name in a path, or on cloud/URI paths where the last component is a folder created earlier by a write step.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/56d7558a7d31c320. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/various.py:261

def arrlen(obj: Any) -> int | None:
    """Return length of (non-string/dict) sequence; returns None for non-sequences."""
    try:
        return None if isinstance(obj, (str, bytes, dict)) else len(obj)
    except TypeError:
        return None


def normalize_filepath(path: str | Path, *, check_not_directory: bool = True) -> str:
    """Create a string path, expanding the home directory if present."""
    # don't use pathlib here as it modifies slashes (s3:// -> s3:/)
    path = os.path.expanduser(path)  # noqa: PTH111
    if (
        check_not_directory
        and os.path.exists(path)  # noqa: PTH110
        and os.path.isdir(path)  # noqa: PTH112
    ):
        msg = f"expected a file path; {path!r} is a directory"
        raise IsADirectoryError(msg)
    return path


def parse_version(version: Sequence[str | int]) -> tuple[int, ...]:
    """Simple version parser; split into a tuple of ints for comparison."""
    if isinstance(version, str):
        version = version.split(".")
    return tuple(int(re.sub(r"\D", "", str(v))) for v in version)


def ordered_unique(values: Sequence[Any]) -> list[Any]:
    """Return unique list of sequence values, maintaining their order of appearance."""
    seen: set[Any] = set()
    add_ = seen.add
    return [v for v in values if not (v in seen or add_(v))]


def deduplicate_names(names: Iterable[str]) -> list[str]:

View on GitHub (pinned to fe841f959e)