From a988ce5c956571d9b14eafd193e81300603031b9 Mon Sep 17 00:00:00 2001 From: Genevieve Buckley <30920819+GenevieveBuckley@users.noreply.github.com> Date: Wed, 16 Aug 2023 11:32:24 +1000 Subject: [PATCH] Allow file mode="x" with get_fs_token_paths (#1333) * Also expand paths in exclusive file creation mode=x * Add test for filepath expandsion with file mode=x --- fsspec/core.py | 2 ++ fsspec/tests/test_core.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/fsspec/core.py b/fsspec/core.py index c777a339c..797d401d9 100644 --- a/fsspec/core.py +++ b/fsspec/core.py @@ -635,6 +635,8 @@ def get_fs_token_paths( else: if "w" in mode and expand: paths = _expand_paths(paths, name_function, num) + elif "x" in mode and expand: + paths = _expand_paths(paths, name_function, num) elif "*" in paths: paths = [f for f in sorted(fs.glob(paths)) if not fs.isdir(f)] else: diff --git a/fsspec/tests/test_core.py b/fsspec/tests/test_core.py index 7271d9875..c78664354 100644 --- a/fsspec/tests/test_core.py +++ b/fsspec/tests/test_core.py @@ -13,6 +13,7 @@ _expand_paths, expand_paths_if_needed, get_compression, + get_fs_token_paths, open_files, open_local, ) @@ -75,6 +76,11 @@ def test_expand_error(): _expand_paths("*.*", None, 1) +@pytest.mark.parametrize("mode", ["w", "w+", "x", "x+"]) +def test_expand_fs_token_paths(mode): + assert len(get_fs_token_paths("path", mode, num=2, expand=True)[-1]) == 2 + + def test_openfile_api(m): m.open("somepath", "wb").write(b"data") of = OpenFile(m, "somepath")