Skip to content

Commit 3cee76e

Browse files
committed
Add key basename providing the filename w/o extension
1 parent a0f03fb commit 3cee76e

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
* `strtools.filename` and `pathtools.parse_path` can now also work on
3030
`java.io.File` objects, which happen to be the type when using ImageJ2's
3131
*Script Parameter* `@# File`.
32+
* The dict returned by `pathtools.parse_path` now also contains the key
33+
`basename` that provides the filename without extension.
3234
* Many improvements / clarifications in function docstrings.

src/imcflibs/pathtools.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def parse_path(path, prefix=""):
3838
- `path` : The same as `full`, up to (including) the last separator.
3939
- `dname` : The segment between the last two separators (directory).
4040
- `fname` : The segment after the last separator (filename).
41+
- `basename` : The filename without extension.
4142
- `ext` : The filename extension, containing max 1 dot (included).
4243
4344
Examples
@@ -50,6 +51,7 @@ def parse_path(path, prefix=""):
5051
'ext': '',
5152
'fname': 'file',
5253
'full': '/tmp/foo/file',
54+
'basename': 'file',
5355
'orig': '/tmp/foo/file',
5456
'path': '/tmp/foo/'}
5557
@@ -60,6 +62,7 @@ def parse_path(path, prefix=""):
6062
'ext': '',
6163
'fname': '',
6264
'full': '/tmp/foo/',
65+
'basename': '',
6366
'orig': '/tmp/foo/',
6467
'path': '/tmp/foo/'}
6568
@@ -70,6 +73,7 @@ def parse_path(path, prefix=""):
7073
'ext': '.ext',
7174
'fname': 'file.ext',
7275
'full': 'C:/Temp/foo/file.ext',
76+
'basename': 'file',
7377
'orig': 'C:\\Temp\\foo\\file.ext',
7478
'path': 'C:/Temp/foo/'}
7579
"""
@@ -86,7 +90,10 @@ def parse_path(path, prefix=""):
8690
parsed["path"] = os.path.dirname(path) + sep
8791
parsed["fname"] = os.path.basename(path)
8892
parsed["dname"] = os.path.basename(os.path.dirname(parsed["path"]))
89-
parsed["ext"] = os.path.splitext(parsed["fname"])[1]
93+
split_ext = os.path.splitext(parsed["fname"])
94+
parsed["ext"] = split_ext[1]
95+
parsed["basename"] = split_ext[0]
96+
9097
return parsed
9198

9299

0 commit comments

Comments
 (0)