{"record":{"id":"56d7558a7d31c320","repo":"pola-rs/polars","slug":"expected-a-file-path-path-r-is-a-directory","errorCode":null,"errorMessage":"expected a file path; {path!r} is a directory","messagePattern":"expected a file path; (.+?) is a directory","errorType":"exception","errorClass":"IsADirectoryError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":261,"sourceCode":"def arrlen(obj: Any) -> int | None:\n    \"\"\"Return length of (non-string/dict) sequence; returns None for non-sequences.\"\"\"\n    try:\n        return None if isinstance(obj, (str, bytes, dict)) else len(obj)\n    except TypeError:\n        return None\n\n\ndef normalize_filepath(path: str | Path, *, check_not_directory: bool = True) -> str:\n    \"\"\"Create a string path, expanding the home directory if present.\"\"\"\n    # don't use pathlib here as it modifies slashes (s3:// -> s3:/)\n    path = os.path.expanduser(path)  # noqa: PTH111\n    if (\n        check_not_directory\n        and os.path.exists(path)  # noqa: PTH110\n        and os.path.isdir(path)  # noqa: PTH112\n    ):\n        msg = f\"expected a file path; {path!r} is a directory\"\n        raise IsADirectoryError(msg)\n    return path\n\n\ndef parse_version(version: Sequence[str | int]) -> tuple[int, ...]:\n    \"\"\"Simple version parser; split into a tuple of ints for comparison.\"\"\"\n    if isinstance(version, str):\n        version = version.split(\".\")\n    return tuple(int(re.sub(r\"\\D\", \"\", str(v))) for v in version)\n\n\ndef ordered_unique(values: Sequence[Any]) -> list[Any]:\n    \"\"\"Return unique list of sequence values, maintaining their order of appearance.\"\"\"\n    seen: set[Any] = set()\n    add_ = seen.add\n    return [v for v in values if not (v in seen or add_(v))]\n\n\ndef deduplicate_names(names: Iterable[str]) -> list[str]:","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/various.py#L243-L279","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Append a file name with extension to the path: df.write_parquet('data/out.parquet').","Check os.path.isdir(path) before calling and handle accordingly.","If you intended to read many files in a directory, use a glob or scan source list instead of the bare directory (where supported)."],"exampleFix":"// before\ndf.write_parquet('out_dir/')\n// after\ndf.write_parquet('out_dir/data.parquet')","handlingStrategy":"validation","validationCode":"import os\nif os.path.isdir(path):\n    path = os.path.join(path, 'data.parquet')","typeGuard":"def is_file_path(p) -> bool:\n    import os\n    return os.path.exists(p) and not os.path.isdir(p)","tryCatchPattern":"try:\n    df.write_parquet(path)\nexcept IsADirectoryError:\n    df.write_parquet(os.path.join(path, 'data.parquet'))","preventionTips":["Always include a file name and extension in output paths","Check os.path.isdir before IO calls","Use path constants that end in a file name, not a directory"],"tags":["python","polars","filesystem","path"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}