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