Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-101000: Add os.path.splitroot()#101002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
21c0ba9836b85dbc2d1f9ecdc40d6592b2778f42279726ca47a6613c26a8dba0c237d411ed3eb2c9eed88299e9627ffe379beff2abacdee14ebe5452927afeb0aa73e19777d632e212e37cded35a8dfce0e75a553663237053729d694f093e99e3cdf618a001c522c9df17269File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -488,6 +488,26 @@ the :mod:`glob` module.) | ||
| Accepts a :term:`path-like object`. | ||
| .. function:: splitroot(path) | ||
| Split the pathname *path* into a triad ``(drive, root, tail)`` where: | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 1. *drive* is an optional mount point, exactly like :func:`splitdrive`; | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 2. *root* is an optional sequence of separators following the drive; and | ||
| 3. *tail* is anything after the root. | ||
| On Posix, *drive* is always empty. The *root* may be empty (relative path), | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| a single forward slash (absolute path), or two forward slashes | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| (implementation-defined per the POSIX standard). | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| On Windows, *drive* may be a UNC sharepoint or a traditional DOS drive. The | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| *root* may be empty, a forward slash, or a backward slash. | ||
| In all cases, ``drive + root + tail`` will be the same as *path*. | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. versionadded:: 3.12 | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. function:: splitext(path) | ||
| Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext == | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,7 +24,7 @@ | ||
| from genericpath import * | ||
| __all__ = ["normcase","isabs","join","splitdrive","splitroot","split","splitext", | ||
| "basename","dirname","commonprefix","getsize","getmtime", | ||
| "getatime","getctime", "islink","exists","lexists","isdir","isfile", | ||
| "ismount", "expanduser","expandvars","normpath","abspath", | ||
| @@ -169,35 +169,58 @@ def splitdrive(p): | ||
| Paths cannot contain both a drive letter and a UNC path. | ||
| """ | ||
| drive, root, tail = splitroot(p) | ||
| return drive, root + tail | ||
| def splitroot(p): | ||
| """Split a pathname into drive, root and tail. The drive is defined | ||
| exactly as in splitdrive(). On Windows, the root may be a single path | ||
| separator or an empty string. The tail contains anything after the root. | ||
| For example: | ||
| splitroot('//server/share/') == ('//server/share', '/', '') | ||
| splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney') | ||
| splitroot('Windows') == ('', '', 'Windows') | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| """ | ||
| p = os.fspath(p) | ||
| ifisinstance(p, bytes): | ||
| sep = b'\\' | ||
| altsep= b'/' | ||
| colon= b':' | ||
| unc_prefix= b'\\\\?\\UNC\\' | ||
| else: | ||
| sep = '\\' | ||
| altsep= '/' | ||
| colon= ':' | ||
| unc_prefix= '\\\\?\\UNC\\' | ||
| normp = p.replace(altsep, sep) | ||
| ifnormp[:1] ==sep: | ||
| if normp[1:2] == sep: | ||
| # UNC drives, e.g. \\server\share or \\?\UNC\server\share | ||
| # Device drives, e.g. \\.\device or \\?\device | ||
| start = 8 if normp[:8].upper() == unc_prefix else 2 | ||
| index = normp.find(sep, start) | ||
| if index == -1: | ||
| return p, p[:0], p[:0] | ||
| index2 = normp.find(sep, index + 1) | ||
| if index2 == -1: | ||
| return p, p[:0], p[:0] | ||
| return p[:index2], p[index2:index2 + 1], p[index2 + 1:] | ||
| else: | ||
| # Relative path with root, e.g. \Windows | ||
| return p[:0], p[:1], p[1:] | ||
| elif normp[1:2] == colon: | ||
| if normp[2:3] == sep: | ||
| # Absolute drive-letter path, e.g. X:\Windows | ||
| return p[:2], p[2:3], p[3:] | ||
AlexWaygood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| else: | ||
| # Relative path with drive, e.g. X:Windows | ||
| return p[:2], p[:0], p[2:] | ||
| else: | ||
| # Relative path, e.g. Windows | ||
| return p[:0], p[:0], p | ||
| # Split a path in head (everything up to the last '/') and tail (the | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,7 +28,7 @@ | ||
| import genericpath | ||
| from genericpath import * | ||
| __all__ = ["normcase","isabs","join","splitdrive","splitroot","split","splitext", | ||
| "basename","dirname","commonprefix","getsize","getmtime", | ||
| "getatime","getctime","islink","exists","lexists","isdir","isfile", | ||
| "ismount", "expanduser","expandvars","normpath","abspath", | ||
| @@ -135,6 +135,30 @@ def splitdrive(p): | ||
| return p[:0], p | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def splitroot(p): | ||
| """Split a pathname into drive, root and tail. On Posix, drive is always | ||
| empty; the root may be empty, a single slash, or two slashes. The tail | ||
| contains anything after the root. For example: | ||
| splitdrive('foo/bar') == ('', '', 'foo/bar') | ||
| splitdrive('/foo/bar') == ('', '/', 'foo/bar') | ||
barneygale marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| """ | ||
| p = os.fspath(p) | ||
| sep = b'/' if isinstance(p, bytes) else '/' | ||
| if p[:1] != sep: | ||
| # Relative path, e.g.: 'foo' | ||
| return p[:0], p[:0], p | ||
| elif p[1:2] != sep: | ||
| # Absolute path, e.g.: '/foo' | ||
| return p[:0], p[:1], p[1:] | ||
| elif p[2:3] != sep: | ||
| # Implementation defined per POSIX standard, e.g.: '//foo' | ||
| return p[:0], p[:2], p[2:] | ||
| else: | ||
| # Absolute path with extraneous slashes, e.g.: '///foo', '////foo', etc. | ||
| return p[:0], p[:1], p[1:] | ||
| # Return the tail (basename) part of a path, same as split(path)[1]. | ||
| def basename(p): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add:func:`os.path.splitroot()`, which splits a path into a triad of | ||
| ``(drive, root, tail)``. | ||
AlexWaygood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||