Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`os.path` --- Common pathname manipulations |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: os.path |
| 5 | :synopsis: Operations on pathnames. |
| 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. index:: single: path; operations |
| 8 | |
| 9 | This module implements some useful functions on pathnames. To read or |
| 10 | write files see :func:`open`, and for accessing the filesystem see the |
Martin v. Löwis | 651423c | 2008-10-07 07:03:04 +0000 | [diff] [blame] | 11 | :mod:`os` module. The path parameters can be passed as either strings, |
| 12 | or bytes. Applications are encouraged to represent file names as |
| 13 | (Unicode) character strings. Unfortunately, some file names may not be |
| 14 | representable as strings on Unix, so applications that need to support |
| 15 | arbitrary file names on Unix should use bytes objects to represent |
| 16 | path names. Vice versa, using bytes objects cannot represent all file |
| 17 | names on Windows (in the standard ``mbcs`` encoding), hence Windows |
| 18 | applications should use string objects to access all files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
R David Murray | a4e700c | 2013-01-06 16:13:10 -0500 | [diff] [blame] | 20 | Unlike a unix shell, Python does not do any *automatic* path expansions. |
| 21 | Functions such as :func:`expanduser` and :func:`expandvars` can be invoked |
| 22 | explicitly when an application desires shell-like path expansion. (See also |
| 23 | the :mod:`glob` module.) |
| 24 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 25 | |
| 26 | .. seealso:: |
| 27 | The :mod:`pathlib` module offers high-level path objects. |
| 28 | |
| 29 | |
Georg Brandl | 76e5538 | 2008-10-08 16:34:57 +0000 | [diff] [blame] | 30 | .. note:: |
| 31 | |
| 32 | All of these functions accept either only bytes or only string objects as |
| 33 | their parameters. The result is an object of the same type, if a path or |
| 34 | file name is returned. |
| 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 37 | .. note:: |
| 38 | |
| 39 | Since different operating systems have different path name conventions, there |
| 40 | are several versions of this module in the standard library. The |
| 41 | :mod:`os.path` module is always the path module suitable for the operating |
| 42 | system Python is running on, and therefore usable for local paths. However, |
| 43 | you can also import and use the individual modules if you want to manipulate |
| 44 | a path that is *always* in one of the different formats. They all have the |
| 45 | same interface: |
| 46 | |
| 47 | * :mod:`posixpath` for UNIX-style paths |
| 48 | * :mod:`ntpath` for Windows paths |
| 49 | * :mod:`macpath` for old-style MacOS paths |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 50 | |
| 51 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | .. function:: abspath(path) |
| 53 | |
| 54 | Return a normalized absolutized version of the pathname *path*. On most |
Chris Jerdonek | 0b502ff | 2012-11-25 20:38:01 -0800 | [diff] [blame] | 55 | platforms, this is equivalent to calling the function :func:`normpath` as |
| 56 | follows: ``normpath(join(os.getcwd(), path))``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | .. function:: basename(path) |
| 60 | |
Chris Jerdonek | 0b502ff | 2012-11-25 20:38:01 -0800 | [diff] [blame] | 61 | Return the base name of pathname *path*. This is the second element of the |
| 62 | pair returned by passing *path* to the function :func:`split`. Note that |
| 63 | the result of this function is different |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | from the Unix :program:`basename` program; where :program:`basename` for |
| 65 | ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an |
| 66 | empty string (``''``). |
| 67 | |
| 68 | |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 69 | .. function:: commonpath(paths) |
| 70 | |
| 71 | Return the longest common sub-path of each pathname in the sequence |
| 72 | *paths*. Raise ValueError if *paths* contains both absolute and relative |
| 73 | pathnames, or if *paths* is empty. Unlike :func:`commonprefix`, this |
| 74 | returns a valid path. |
| 75 | |
| 76 | Availability: Unix, Windows |
| 77 | |
| 78 | .. versionadded:: 3.5 |
| 79 | |
| 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | .. function:: commonprefix(list) |
| 82 | |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 83 | Return the longest path prefix (taken character-by-character) that is a |
| 84 | prefix of all paths in *list*. If *list* is empty, return the empty string |
Yury Selivanov | 80ac11f | 2015-08-17 23:43:43 -0400 | [diff] [blame] | 85 | (``''``). |
| 86 | |
| 87 | .. note:: |
| 88 | |
| 89 | This function may return invalid paths because it works a |
| 90 | character at a time. To obtain a valid path, see |
| 91 | :func:`commonpath`. |
| 92 | |
| 93 | :: |
| 94 | |
Yury Selivanov | de11561 | 2015-08-19 09:53:28 -0400 | [diff] [blame] | 95 | >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib']) |
| 96 | '/usr/l' |
Yury Selivanov | 80ac11f | 2015-08-17 23:43:43 -0400 | [diff] [blame] | 97 | |
Yury Selivanov | de11561 | 2015-08-19 09:53:28 -0400 | [diff] [blame] | 98 | >>> os.path.commonpath(['/usr/lib', '/usr/local/lib']) |
| 99 | '/usr' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | .. function:: dirname(path) |
| 103 | |
Chris Jerdonek | 0b502ff | 2012-11-25 20:38:01 -0800 | [diff] [blame] | 104 | Return the directory name of pathname *path*. This is the first element of |
| 105 | the pair returned by passing *path* to the function :func:`split`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | .. function:: exists(path) |
| 109 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 110 | Return ``True`` if *path* refers to an existing path or an open |
| 111 | file descriptor. Returns ``False`` for broken symbolic links. On |
| 112 | some platforms, this function may return ``False`` if permission is |
| 113 | not granted to execute :func:`os.stat` on the requested file, even |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | if the *path* physically exists. |
| 115 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 116 | .. versionchanged:: 3.3 |
| 117 | *path* can now be an integer: ``True`` is returned if it is an |
| 118 | open file descriptor, ``False`` otherwise. |
| 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | .. function:: lexists(path) |
| 122 | |
| 123 | Return ``True`` if *path* refers to an existing path. Returns ``True`` for |
| 124 | broken symbolic links. Equivalent to :func:`exists` on platforms lacking |
| 125 | :func:`os.lstat`. |
| 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
| 128 | .. function:: expanduser(path) |
| 129 | |
| 130 | On Unix and Windows, return the argument with an initial component of ``~`` or |
| 131 | ``~user`` replaced by that *user*'s home directory. |
| 132 | |
| 133 | .. index:: module: pwd |
| 134 | |
| 135 | On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME` |
| 136 | if it is set; otherwise the current user's home directory is looked up in the |
| 137 | password directory through the built-in module :mod:`pwd`. An initial ``~user`` |
| 138 | is looked up directly in the password directory. |
| 139 | |
| 140 | On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set, |
| 141 | otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be |
| 142 | used. An initial ``~user`` is handled by stripping the last directory component |
| 143 | from the created user path derived above. |
| 144 | |
| 145 | If the expansion fails or if the path does not begin with a tilde, the path is |
| 146 | returned unchanged. |
| 147 | |
| 148 | |
| 149 | .. function:: expandvars(path) |
| 150 | |
| 151 | Return the argument with environment variables expanded. Substrings of the form |
| 152 | ``$name`` or ``${name}`` are replaced by the value of environment variable |
| 153 | *name*. Malformed variable names and references to non-existing variables are |
| 154 | left unchanged. |
| 155 | |
| 156 | On Windows, ``%name%`` expansions are supported in addition to ``$name`` and |
| 157 | ``${name}``. |
| 158 | |
| 159 | |
| 160 | .. function:: getatime(path) |
| 161 | |
| 162 | Return the time of last access of *path*. The return value is a number giving |
| 163 | the number of seconds since the epoch (see the :mod:`time` module). Raise |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 164 | :exc:`OSError` if the file does not exist or is inaccessible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 166 | If :func:`os.stat_float_times` returns ``True``, the result is a floating point |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 167 | number. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | .. function:: getmtime(path) |
| 171 | |
| 172 | Return the time of last modification of *path*. The return value is a number |
| 173 | giving the number of seconds since the epoch (see the :mod:`time` module). |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 174 | Raise :exc:`OSError` if the file does not exist or is inaccessible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 176 | If :func:`os.stat_float_times` returns ``True``, the result is a floating point |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 177 | number. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | .. function:: getctime(path) |
| 181 | |
| 182 | Return the system's ctime which, on some systems (like Unix) is the time of the |
Georg Brandl | f632494 | 2013-10-06 09:52:55 +0200 | [diff] [blame] | 183 | last metadata change, and, on others (like Windows), is the creation time for *path*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | The return value is a number giving the number of seconds since the epoch (see |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 185 | the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | is inaccessible. |
| 187 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |
| 189 | .. function:: getsize(path) |
| 190 | |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 191 | Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | not exist or is inaccessible. |
| 193 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 | |
| 195 | .. function:: isabs(path) |
| 196 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 197 | Return ``True`` if *path* is an absolute pathname. On Unix, that means it |
| 198 | begins with a slash, on Windows that it begins with a (back)slash after chopping |
| 199 | off a potential drive letter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | .. function:: isfile(path) |
| 203 | |
| 204 | Return ``True`` if *path* is an existing regular file. This follows symbolic |
| 205 | links, so both :func:`islink` and :func:`isfile` can be true for the same path. |
| 206 | |
| 207 | |
| 208 | .. function:: isdir(path) |
| 209 | |
| 210 | Return ``True`` if *path* is an existing directory. This follows symbolic |
| 211 | links, so both :func:`islink` and :func:`isdir` can be true for the same path. |
| 212 | |
| 213 | |
| 214 | .. function:: islink(path) |
| 215 | |
| 216 | Return ``True`` if *path* refers to a directory entry that is a symbolic link. |
Ned Deily | 32db438 | 2016-06-04 09:40:40 -0700 | [diff] [blame^] | 217 | Always ``False`` if symbolic links are not supported by the Python runtime. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | .. function:: ismount(path) |
| 221 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 222 | Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a |
| 223 | file system where a different file system has been mounted. On POSIX, the |
| 224 | function checks whether *path*'s parent, :file:`path/..`, is on a different |
| 225 | device than *path*, or whether :file:`path/..` and *path* point to the same |
| 226 | i-node on the same device --- this should detect mount points for all Unix |
| 227 | and POSIX variants. On Windows, a drive letter root and a share UNC are |
| 228 | always mount points, and for any other path ``GetVolumePathName`` is called |
| 229 | to see if it is different from the input path. |
| 230 | |
| 231 | .. versionadded:: 3.4 |
| 232 | Support for detecting non-root mount points on Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | |
Zachary Ware | a13dab4 | 2014-10-10 16:03:14 -0500 | [diff] [blame] | 235 | .. function:: join(path, *paths) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Zachary Ware | a13dab4 | 2014-10-10 16:03:14 -0500 | [diff] [blame] | 237 | Join one or more path components intelligently. The return value is the |
| 238 | concatenation of *path* and any members of *\*paths* with exactly one |
| 239 | directory separator (``os.sep``) following each non-empty part except the |
| 240 | last, meaning that the result will only end in a separator if the last |
| 241 | part is empty. If a component is an absolute path, all previous |
| 242 | components are thrown away and joining continues from the absolute path |
| 243 | component. |
| 244 | |
| 245 | On Windows, the drive letter is not reset when an absolute path component |
| 246 | (e.g., ``r'\foo'``) is encountered. If a component contains a drive |
| 247 | letter, all previous components are thrown away and the drive letter is |
| 248 | reset. Note that since there is a current directory for each drive, |
| 249 | ``os.path.join("c:", "foo")`` represents a path relative to the current |
| 250 | directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
| 252 | |
| 253 | .. function:: normcase(path) |
| 254 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 255 | Normalize the case of a pathname. On Unix and Mac OS X, this returns the |
| 256 | path unchanged; on case-insensitive filesystems, it converts the path to |
| 257 | lowercase. On Windows, it also converts forward slashes to backward slashes. |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 258 | Raise a TypeError if the type of *path* is not ``str`` or ``bytes``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | .. function:: normpath(path) |
| 262 | |
Terry Jan Reedy | ec6e132 | 2013-03-17 15:21:26 -0400 | [diff] [blame] | 263 | Normalize a pathname by collapsing redundant separators and up-level |
| 264 | references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all |
| 265 | become ``A/B``. This string manipulation may change the meaning of a path |
| 266 | that contains symbolic links. On Windows, it converts forward slashes to |
Terry Jan Reedy | f346041 | 2013-03-17 15:28:10 -0400 | [diff] [blame] | 267 | backward slashes. To normalize case, use :func:`normcase`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | .. function:: realpath(path) |
| 271 | |
| 272 | Return the canonical path of the specified filename, eliminating any symbolic |
| 273 | links encountered in the path (if they are supported by the operating system). |
| 274 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 | |
Benjamin Peterson | 409a1be | 2014-03-20 12:39:53 -0500 | [diff] [blame] | 276 | .. function:: relpath(path, start=os.curdir) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
R David Murray | ce10fab | 2013-07-12 17:43:11 -0400 | [diff] [blame] | 278 | Return a relative filepath to *path* either from the current directory or |
| 279 | from an optional *start* directory. This is a path computation: the |
| 280 | filesystem is not accessed to confirm the existence or nature of *path* or |
| 281 | *start*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | f650e46 | 2010-05-06 23:03:05 +0000 | [diff] [blame] | 283 | *start* defaults to :attr:`os.curdir`. |
| 284 | |
Antoine Pitrou | f10f162 | 2010-12-12 20:17:29 +0000 | [diff] [blame] | 285 | Availability: Unix, Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
| 288 | .. function:: samefile(path1, path2) |
| 289 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 290 | Return ``True`` if both pathname arguments refer to the same file or directory. |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 291 | This is determined by the device number and i-node number and raises an |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 292 | exception if an :func:`os.stat` call on either pathname fails. |
Benjamin Peterson | f650e46 | 2010-05-06 23:03:05 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f10f162 | 2010-12-12 20:17:29 +0000 | [diff] [blame] | 294 | Availability: Unix, Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 295 | |
Georg Brandl | b382337 | 2010-07-10 08:58:37 +0000 | [diff] [blame] | 296 | .. versionchanged:: 3.2 |
| 297 | Added Windows support. |
Brian Curtin | c739569 | 2010-07-09 15:15:09 +0000 | [diff] [blame] | 298 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 299 | .. versionchanged:: 3.4 |
| 300 | Windows now uses the same implementation as all other platforms. |
| 301 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
| 303 | .. function:: sameopenfile(fp1, fp2) |
| 304 | |
| 305 | Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file. |
Benjamin Peterson | f650e46 | 2010-05-06 23:03:05 +0000 | [diff] [blame] | 306 | |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 307 | Availability: Unix, Windows. |
| 308 | |
Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 309 | .. versionchanged:: 3.2 |
| 310 | Added Windows support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | .. function:: samestat(stat1, stat2) |
| 314 | |
| 315 | Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file. |
Serhiy Storchaka | dab8354 | 2013-10-13 20:12:43 +0300 | [diff] [blame] | 316 | These structures may have been returned by :func:`os.fstat`, |
| 317 | :func:`os.lstat`, or :func:`os.stat`. This function implements the |
| 318 | underlying comparison used by :func:`samefile` and :func:`sameopenfile`. |
Benjamin Peterson | f650e46 | 2010-05-06 23:03:05 +0000 | [diff] [blame] | 319 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 320 | Availability: Unix, Windows. |
| 321 | |
| 322 | .. versionchanged:: 3.4 |
| 323 | Added Windows support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 | |
| 325 | |
| 326 | .. function:: split(path) |
| 327 | |
Georg Brandl | 539c165 | 2010-10-14 06:46:08 +0000 | [diff] [blame] | 328 | Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the |
| 329 | last pathname component and *head* is everything leading up to that. The |
| 330 | *tail* part will never contain a slash; if *path* ends in a slash, *tail* |
| 331 | will be empty. If there is no slash in *path*, *head* will be empty. If |
| 332 | *path* is empty, both *head* and *tail* are empty. Trailing slashes are |
| 333 | stripped from *head* unless it is the root (one or more slashes only). In |
| 334 | all cases, ``join(head, tail)`` returns a path to the same location as *path* |
Chris Jerdonek | 0b502ff | 2012-11-25 20:38:01 -0800 | [diff] [blame] | 335 | (but the strings may differ). Also see the functions :func:`dirname` and |
| 336 | :func:`basename`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
| 338 | |
| 339 | .. function:: splitdrive(path) |
| 340 | |
| 341 | Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 342 | a mount point or the empty string. On systems which do not use drive |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | specifications, *drive* will always be the empty string. In all cases, ``drive |
| 344 | + tail`` will be the same as *path*. |
| 345 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 346 | On Windows, splits a pathname into drive/UNC sharepoint and relative path. |
| 347 | |
| 348 | If the path contains a drive letter, drive will contain everything |
| 349 | up to and including the colon. |
| 350 | e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")`` |
| 351 | |
| 352 | If the path contains a UNC path, drive will contain the host name |
| 353 | and share, up to but not including the fourth separator. |
| 354 | e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")`` |
| 355 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | |
| 357 | .. function:: splitext(path) |
| 358 | |
| 359 | Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext == |
| 360 | path``, and *ext* is empty or begins with a period and contains at most one |
| 361 | period. Leading periods on the basename are ignored; ``splitext('.cshrc')`` |
| 362 | returns ``('.cshrc', '')``. |
| 363 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
| 365 | .. function:: splitunc(path) |
| 366 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 367 | .. deprecated:: 3.1 |
| 368 | Use *splitdrive* instead. |
| 369 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 370 | Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC |
| 371 | mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of |
| 372 | the path (such as ``r'\path\file.ext'``). For paths containing drive letters, |
Benjamin Peterson | f650e46 | 2010-05-06 23:03:05 +0000 | [diff] [blame] | 373 | *unc* will always be the empty string. |
| 374 | |
| 375 | Availability: Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | |
| 377 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | .. data:: supports_unicode_filenames |
| 379 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 380 | ``True`` if arbitrary Unicode strings can be used as file names (within limitations |
Victor Stinner | b55e498 | 2010-09-11 00:22:12 +0000 | [diff] [blame] | 381 | imposed by the file system). |