Skip to content

Commit 55c058a

Browse files
authored
pygmt.which: Fix the bug when passing multiple files (#2726)
1 parent c9d6147 commit 55c058a

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

pygmt/src/which.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
GMTTempFile,
77
build_arg_string,
88
fmt_docstring,
9-
kwargs_to_strings,
9+
is_nonstr_iter,
1010
use_alias,
1111
)
1212

1313

1414
@fmt_docstring
1515
@use_alias(G="download", V="verbose")
16-
@kwargs_to_strings(fname="sequence_space")
1716
def which(fname, **kwargs):
1817
r"""
1918
Find the full path to specified files.
@@ -63,6 +62,9 @@ def which(fname, **kwargs):
6362
FileNotFoundError
6463
If the file is not found.
6564
"""
65+
if is_nonstr_iter(fname): # Got a list of files
66+
fname = " ".join(fname)
67+
6668
with GMTTempFile() as tmpfile:
6769
with Session() as lib:
6870
lib.call_module(

pygmt/tests/test_which.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Test pygmt.which.
33
"""
4-
import os
4+
from pathlib import Path
55

66
import pytest
77
from pygmt import which
@@ -13,20 +13,20 @@ def test_which():
1313
Make sure `which` returns file paths for @files correctly without errors.
1414
"""
1515
for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]:
16-
cached_file = which(f"@{fname}", download="c")
17-
assert os.path.exists(cached_file)
18-
assert os.path.basename(cached_file) == fname
16+
cached_file = which(fname=f"@{fname}", download="c")
17+
assert Path(cached_file).exists()
18+
assert Path(cached_file).name == fname
1919

2020

2121
def test_which_multiple():
2222
"""
2323
Make sure `which` returns file paths for multiple @files correctly.
2424
"""
2525
filenames = ["ridge.txt", "tut_ship.xyz"]
26-
cached_files = which(fname=[f"@{fname}" for fname in filenames], download="c")
26+
cached_files = which([f"@{fname}" for fname in filenames], download="c")
2727
for cached_file in cached_files:
28-
assert os.path.exists(cached_file)
29-
assert os.path.basename(cached_file) in filenames
28+
assert Path(cached_file).exists()
29+
assert Path(cached_file).name in filenames
3030

3131

3232
def test_which_fails():

0 commit comments

Comments
 (0)