1010
1111
1212def parse_path (path , prefix = "" ):
13- """Parse a path into its components.
13+ r """Parse a path into its components.
1414
1515 If the path doesn't end with the pathsep, it is assumed being a file!
1616 No tests based on existing files are done, as this is supposed to also work
@@ -20,6 +20,11 @@ def parse_path(path, prefix=""):
2020 *Script Parameter* `#@ File`) for either of the parameters, so it is safe to
2121 use this in ImageJ Python scripts without additional measures.
2222
23+ **WARNING**: when passing in **Windows paths** literally, make sure to
24+ declare them as **raw strings** using the `r""` notation, otherwise
25+ unexpected things might happen if the path contains sections that Python
26+ will interpret as escape sequences (e.g. `\n`, `\t`, `\u2324`, ...).
27+
2328 Parameters
2429 ----------
2530 path : str or str-like
@@ -55,50 +60,61 @@ def parse_path(path, prefix=""):
5560 POSIX-style path to a file with a suffix:
5661
5762 >>> parse_path('/tmp/foo/file.suffix')
58- {'dname': 'foo',
59- 'ext': '',
60- 'fname': 'file',
61- 'full': '/tmp/foo/file',
62- 'basename': 'file',
63- 'orig': '/tmp/foo/file',
64- 'parent': '/tmp/',
65- 'path': '/tmp/foo/'}
63+ {
64+ "dname": "foo",
65+ "ext": "",
66+ "fname": "file",
67+ "full": "/tmp/foo/file",
68+ "basename": "file",
69+ "orig": "/tmp/foo/file",
70+ "parent": "/tmp/",
71+ "path": "/tmp/foo/",
72+ }
73+
6674
6775 POSIX-style path to a directory:
6876
6977 >>> parse_path('/tmp/foo/')
70- {'dname': 'foo',
71- 'ext': '',
72- 'fname': '',
73- 'full': '/tmp/foo/',
74- 'basename': '',
75- 'orig': '/tmp/foo/',
76- 'parent': '/tmp/',
77- 'path': '/tmp/foo/'}
78+ {
79+ "dname": "foo",
80+ "ext": "",
81+ "fname": "",
82+ "full": "/tmp/foo/",
83+ "basename": "",
84+ "orig": "/tmp/foo/",
85+ "parent": "/tmp/",
86+ "path": "/tmp/foo/",
87+ }
88+
7889
7990 Windows-style path to a file:
8091
81- >>> parse_path('C:\\ Temp\\ foo\\ file.ext')
82- {'dname': 'foo',
83- 'ext': '.ext',
84- 'fname': 'file.ext',
85- 'full': 'C:/Temp/foo/file.ext',
86- 'basename': 'file',
87- 'orig': 'C:\\ Temp\\ foo\\ file.ext',
88- 'parent': 'C:/Temp/',
89- 'path': 'C:/Temp/foo/'}
92+ >>> parse_path(r'C:\Temp\new\file.ext')
93+ {
94+ "dname": "new",
95+ "ext": ".ext",
96+ "fname": "file.ext",
97+ "full": "C:/Temp/new/file.ext",
98+ "basename": "file",
99+ "orig": "C:\\Temp\\new\\file.ext",
100+ "parent": "C:/Temp",
101+ "path": "C:/Temp/new/",
102+ }
103+
90104
91105 Special treatment for *OME-TIFF* suffixes:
92106
93107 >>> parse_path("/path/to/some/nice.OME.tIf")
94- {'basename': 'nice',
95- 'dname': 'some',
96- 'ext': '.OME.tIf',
97- 'fname': 'nice.OME.tIf',
98- 'full': '/path/to/some/nice.OME.tIf',
99- 'orig': '/path/to/some/nice.OME.tIf',
100- 'parent': '/path/to/',
101- 'path': '/path/to/some/'}
108+ {
109+ "basename": "nice",
110+ "dname": "some",
111+ "ext": ".OME.tIf",
112+ "fname": "nice.OME.tIf",
113+ "full": "/path/to/some/nice.OME.tIf",
114+ "orig": "/path/to/some/nice.OME.tIf",
115+ "parent": "/path/to/",
116+ "path": "/path/to/some/",
117+ }
102118 """
103119 path = str (path )
104120 if prefix :
0 commit comments