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