88
99
1010def parse_path (path , prefix = "" ):
11- """Parse a path into its components.
11+ r """Parse a path into its components.
1212
1313 If the path doesn't end with the pathsep, it is assumed being a file!
1414 No tests based on existing files are done, as this is supposed to also work
@@ -28,41 +28,50 @@ def parse_path(path, prefix=""):
2828
2929 Returns
3030 -------
31- parsed = {
32- 'orig' : str # string as passed into this function
33- 'full' : str # separators adjusted to current platform
34- 'path' : str # like previous, up to (including) the last separator
35- 'dname' : str # segment between the last two separators (directory)
36- 'fname' : str # segment after the last separator (filename)
37- 'ext' : str # filename extension, containing max 1 dot (included)
38- }
39-
40- Example
41- -------
42-
43- >>> path_to_file = parse_path('/tmp/foo/file')
44- >>> path_to_dir = parse_path('/tmp/foo/')
45-
46- orig : The full path string as given to this function.
47- full : Backslashes replaced by the current separator.
48-
49- path : 'full' up to the last separator (included)
50- >>> path_to_file['path']
51- '/tmp/foo/'
52- >>> path_to_dir['path']
53- '/tmp/foo/'
54-
55- dname : The last directory of the path in 'full'.
56- >>> path_to_file['dname']
57- 'foo'
58- >>> path_to_dir['dname']
59- 'foo'
60-
61- fname : The filename of 'full', empty in case of a directory.
62- >>> path_to_file['fname']
63- 'file'
64- >>> path_to_dir['fname']
65- ''
31+ dict
32+ The parsed (and possibly combined) path split into its components, with
33+ the following keys:
34+ - `orig` : The full string as passed into this function (possibly
35+ combined with the prefix in case one was specified).
36+ - `full` : The same as `orig` with separators adjusted to the current
37+ platform.
38+ - `path` : The same as `full`, up to (including) the last separator.
39+ - `dname` : The segment between the last two separators (directory).
40+ - `fname` : The segment after the last separator (filename).
41+ - `ext` : The filename extension, containing max 1 dot (included).
42+
43+ Examples
44+ --------
45+
46+ POSIX-style path to a file with a suffix:
47+
48+ >>> parse_path('/tmp/foo/file.suffix')
49+ {'dname': 'foo',
50+ 'ext': '',
51+ 'fname': 'file',
52+ 'full': '/tmp/foo/file',
53+ 'orig': '/tmp/foo/file',
54+ 'path': '/tmp/foo/'}
55+
56+ POSIX-style path to a directory:
57+
58+ >>> parse_path('/tmp/foo/')
59+ {'dname': 'foo',
60+ 'ext': '',
61+ 'fname': '',
62+ 'full': '/tmp/foo/',
63+ 'orig': '/tmp/foo/',
64+ 'path': '/tmp/foo/'}
65+
66+ Windows-style path to a file:
67+
68+ >>> parse_path('C:\\Temp\\foo\\file.ext')
69+ {'dname': 'foo',
70+ 'ext': '.ext',
71+ 'fname': 'file.ext',
72+ 'full': 'C:/Temp/foo/file.ext',
73+ 'orig': 'C:\\Temp\\foo\\file.ext',
74+ 'path': 'C:/Temp/foo/'}
6675 """
6776 path = str (path )
6877 if prefix :
0 commit comments