Skip to content

Add support for path-like file name in to_csv and to_parquet#527

Open
leostimpfle wants to merge 1 commit into
duckdb:mainfrom
leostimpfle:pathlike-output
Open

Add support for path-like file name in to_csv and to_parquet#527
leostimpfle wants to merge 1 commit into
duckdb:mainfrom
leostimpfle:pathlike-output

Conversation

@leostimpfle

@leostimpfle leostimpfle commented Jul 2, 2026

Copy link
Copy Markdown

This PR adds support for os.PathLike output filenames in the DuckDBPyRelation.to_csv and DuckDBPyRelation.to_parquet methods.

Currently, only plain Python strings are supported which requires explicit casting of pathlib.Path objects.

from pathlib import Path
import duckdb

# On main this throws TypeError: to_csv(): incompatible function arguments.
duckdb.sql("SELECT 43").to_csv(Path("test.csv"))

# Support for plain strings is unaffected
duckdb.sql("SELECT 43").to_csv("test.csv")

# Other arguments throw: _duckdb.InvalidInputException: Invalid Input Error: Expected a str, bytes, or os.PathLike object for the file path, not 'int'
duckdb.sql("SELECT 43").to_csv(123)

The core change is the addition of PathToString which is used in DuckDBPyRelation::ToCSV and DuckDBPyRelation::ToParquet. I have added tests in tests/fast/api/test_to_csv.py and tests/fast/api/test_to_parquet.py for the new argument types.

@evertlammerts Please let me know any comments. I have a few further ideas on similar small-ish improvements to duckdb-python which I would love to work on. (Please let me know if I should always first open discussions before PRs.)

Comment thread src/path_like.cpp
};

void PathLikeProcessor::AddFile(const nb::object &object) {
bool TryDecodePath(const nb::object &object, string &result) {

@NickCrews NickCrews Jul 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to TryEnsurePathString ("Ensure" implies that it can handle an already-string, "Decode" makes it sound like it can only handle Paths)

@NickCrews NickCrews left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, two small nits that are non blockers for me (I'm not a maintainer though)

Comment thread src/path_like.cpp
return false;
}

string PathToString(const nb::object &object) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to EnsurePathString (or whatever you end up with for the function above, without the "Try" prefix)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants