Skip to content

Commit 1d1dddf

Browse files
committed
Add pathtools.join2()
1 parent e7b1af8 commit 1d1dddf

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
### Added
88

9+
* `pathtools.join2` can be used to join paths, much like `os.path.join` except
10+
that it will work with `java.io.File` objects as well (but doesn't support
11+
more than two path components / parameters).)
12+
913
### Changed
1014

1115
* `strtools.filename` and `pathtools.parse_path` can now also work on

src/imcflibs/pathtools.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ def parse_path(path, prefix=""):
8181
return parsed
8282

8383

84+
def join2(path1, path2):
85+
"""Join two paths into one, much like os.path.join().
86+
87+
The main difference is that `join2()` takes exactly two arguments, but they
88+
can be non-str (as long as they're having a `__str__()` method), so this is
89+
safe to be used with stuff like `java.io.File` objects as retrieved when
90+
using ImageJ2's *Script Parameter* `#@ File`.
91+
92+
In addition some sanitizing is done, e.g. in case one of the components is
93+
containing double backslashes (`\\`), they will be replaced by the current
94+
OS's path separator.
95+
96+
Parameters
97+
----------
98+
path1 : str or str-like
99+
The first component of the path to be joined.
100+
path2 : str or str-like
101+
The second component of the path to be joined.
102+
103+
Returns
104+
-------
105+
str
106+
"""
107+
return parse_path(path2, prefix=path1)["full"]
108+
109+
84110
def jython_fiji_exists(path):
85111
"""Wrapper to work around problems with Jython 2.7 in Fiji.
86112

0 commit comments

Comments
 (0)