Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`os.path` --- Common pathname manipulations |
| 3 | ================================================ |
| 4 | |
| 5 | .. module:: os.path |
| 6 | :synopsis: Operations on pathnames. |
| 7 | |
| 8 | |
| 9 | .. index:: single: path; operations |
| 10 | |
| 11 | This module implements some useful functions on pathnames. To read or |
| 12 | 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] | 13 | :mod:`os` module. The path parameters can be passed as either strings, |
| 14 | or bytes. Applications are encouraged to represent file names as |
| 15 | (Unicode) character strings. Unfortunately, some file names may not be |
| 16 | representable as strings on Unix, so applications that need to support |
| 17 | arbitrary file names on Unix should use bytes objects to represent |
| 18 | path names. Vice versa, using bytes objects cannot represent all file |
| 19 | names on Windows (in the standard ``mbcs`` encoding), hence Windows |
| 20 | applications should use string objects to access all files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
Georg Brandl | 76e5538 | 2008-10-08 16:34:57 +0000 | [diff] [blame] | 22 | .. note:: |
| 23 | |
| 24 | All of these functions accept either only bytes or only string objects as |
| 25 | their parameters. The result is an object of the same type, if a path or |
| 26 | file name is returned. |
| 27 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | .. warning:: |
| 29 | |
| 30 | On Windows, many of these functions do not properly support UNC pathnames. |
| 31 | :func:`splitunc` and :func:`ismount` do handle them correctly. |
| 32 | |
| 33 | |
| 34 | .. function:: abspath(path) |
| 35 | |
| 36 | Return a normalized absolutized version of the pathname *path*. On most |
| 37 | platforms, this is equivalent to ``normpath(join(os.getcwd(), path))``. |
| 38 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 40 | .. function:: basename(path) |
| 41 | |
| 42 | Return the base name of pathname *path*. This is the second half of the pair |
| 43 | returned by ``split(path)``. Note that the result of this function is different |
| 44 | from the Unix :program:`basename` program; where :program:`basename` for |
| 45 | ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an |
| 46 | empty string (``''``). |
| 47 | |
| 48 | |
| 49 | .. function:: commonprefix(list) |
| 50 | |
| 51 | Return the longest path prefix (taken character-by-character) that is a prefix |
| 52 | of all paths in *list*. If *list* is empty, return the empty string (``''``). |
| 53 | Note that this may return invalid paths because it works a character at a time. |
| 54 | |
| 55 | |
| 56 | .. function:: dirname(path) |
| 57 | |
| 58 | Return the directory name of pathname *path*. This is the first half of the |
| 59 | pair returned by ``split(path)``. |
| 60 | |
| 61 | |
| 62 | .. function:: exists(path) |
| 63 | |
| 64 | Return ``True`` if *path* refers to an existing path. Returns ``False`` for |
| 65 | broken symbolic links. On some platforms, this function may return ``False`` if |
| 66 | permission is not granted to execute :func:`os.stat` on the requested file, even |
| 67 | if the *path* physically exists. |
| 68 | |
| 69 | |
| 70 | .. function:: lexists(path) |
| 71 | |
| 72 | Return ``True`` if *path* refers to an existing path. Returns ``True`` for |
| 73 | broken symbolic links. Equivalent to :func:`exists` on platforms lacking |
| 74 | :func:`os.lstat`. |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | .. function:: expanduser(path) |
| 78 | |
| 79 | On Unix and Windows, return the argument with an initial component of ``~`` or |
| 80 | ``~user`` replaced by that *user*'s home directory. |
| 81 | |
| 82 | .. index:: module: pwd |
| 83 | |
| 84 | On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME` |
| 85 | if it is set; otherwise the current user's home directory is looked up in the |
| 86 | password directory through the built-in module :mod:`pwd`. An initial ``~user`` |
| 87 | is looked up directly in the password directory. |
| 88 | |
| 89 | On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set, |
| 90 | otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be |
| 91 | used. An initial ``~user`` is handled by stripping the last directory component |
| 92 | from the created user path derived above. |
| 93 | |
| 94 | If the expansion fails or if the path does not begin with a tilde, the path is |
| 95 | returned unchanged. |
| 96 | |
| 97 | |
| 98 | .. function:: expandvars(path) |
| 99 | |
| 100 | Return the argument with environment variables expanded. Substrings of the form |
| 101 | ``$name`` or ``${name}`` are replaced by the value of environment variable |
| 102 | *name*. Malformed variable names and references to non-existing variables are |
| 103 | left unchanged. |
| 104 | |
| 105 | On Windows, ``%name%`` expansions are supported in addition to ``$name`` and |
| 106 | ``${name}``. |
| 107 | |
| 108 | |
| 109 | .. function:: getatime(path) |
| 110 | |
| 111 | Return the time of last access of *path*. The return value is a number giving |
| 112 | the number of seconds since the epoch (see the :mod:`time` module). Raise |
| 113 | :exc:`os.error` if the file does not exist or is inaccessible. |
| 114 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 115 | If :func:`os.stat_float_times` returns True, the result is a floating point |
| 116 | number. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | .. function:: getmtime(path) |
| 120 | |
| 121 | Return the time of last modification of *path*. The return value is a number |
| 122 | giving the number of seconds since the epoch (see the :mod:`time` module). |
| 123 | Raise :exc:`os.error` if the file does not exist or is inaccessible. |
| 124 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 125 | If :func:`os.stat_float_times` returns True, the result is a floating point |
| 126 | number. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | .. function:: getctime(path) |
| 130 | |
| 131 | Return the system's ctime which, on some systems (like Unix) is the time of the |
| 132 | last change, and, on others (like Windows), is the creation time for *path*. |
| 133 | The return value is a number giving the number of seconds since the epoch (see |
| 134 | the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or |
| 135 | is inaccessible. |
| 136 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | .. function:: getsize(path) |
| 139 | |
| 140 | Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does |
| 141 | not exist or is inaccessible. |
| 142 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | .. function:: isabs(path) |
| 145 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 146 | Return ``True`` if *path* is an absolute pathname. On Unix, that means it |
| 147 | begins with a slash, on Windows that it begins with a (back)slash after chopping |
| 148 | off a potential drive letter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
| 150 | |
| 151 | .. function:: isfile(path) |
| 152 | |
| 153 | Return ``True`` if *path* is an existing regular file. This follows symbolic |
| 154 | links, so both :func:`islink` and :func:`isfile` can be true for the same path. |
| 155 | |
| 156 | |
| 157 | .. function:: isdir(path) |
| 158 | |
| 159 | Return ``True`` if *path* is an existing directory. This follows symbolic |
| 160 | links, so both :func:`islink` and :func:`isdir` can be true for the same path. |
| 161 | |
| 162 | |
| 163 | .. function:: islink(path) |
| 164 | |
| 165 | Return ``True`` if *path* refers to a directory entry that is a symbolic link. |
| 166 | Always ``False`` if symbolic links are not supported. |
| 167 | |
| 168 | |
| 169 | .. function:: ismount(path) |
| 170 | |
| 171 | Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file |
| 172 | system where a different file system has been mounted. The function checks |
| 173 | whether *path*'s parent, :file:`path/..`, is on a different device than *path*, |
| 174 | or whether :file:`path/..` and *path* point to the same i-node on the same |
| 175 | device --- this should detect mount points for all Unix and POSIX variants. |
| 176 | |
| 177 | |
| 178 | .. function:: join(path1[, path2[, ...]]) |
| 179 | |
| 180 | Join one or more path components intelligently. If any component is an absolute |
| 181 | path, all previous components (on Windows, including the previous drive letter, |
| 182 | if there was one) are thrown away, and joining continues. The return value is |
| 183 | the concatenation of *path1*, and optionally *path2*, etc., with exactly one |
| 184 | directory separator (``os.sep``) inserted between components, unless *path2* is |
| 185 | empty. Note that on Windows, since there is a current directory for each drive, |
| 186 | ``os.path.join("c:", "foo")`` represents a path relative to the current |
| 187 | directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`. |
| 188 | |
| 189 | |
| 190 | .. function:: normcase(path) |
| 191 | |
| 192 | Normalize the case of a pathname. On Unix, this returns the path unchanged; on |
| 193 | case-insensitive filesystems, it converts the path to lowercase. On Windows, it |
| 194 | also converts forward slashes to backward slashes. |
| 195 | |
| 196 | |
| 197 | .. function:: normpath(path) |
| 198 | |
| 199 | Normalize a pathname. This collapses redundant separators and up-level |
| 200 | references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``. |
| 201 | It does not normalize the case (use :func:`normcase` for that). On Windows, it |
| 202 | converts forward slashes to backward slashes. It should be understood that this |
| 203 | may change the meaning of the path if it contains symbolic links! |
| 204 | |
| 205 | |
| 206 | .. function:: realpath(path) |
| 207 | |
| 208 | Return the canonical path of the specified filename, eliminating any symbolic |
| 209 | links encountered in the path (if they are supported by the operating system). |
| 210 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
| 212 | .. function:: relpath(path[, start]) |
| 213 | |
| 214 | Return a relative filepath to *path* either from the current directory or from |
| 215 | an optional *start* point. |
| 216 | |
| 217 | *start* defaults to :attr:`os.curdir`. Availability: Windows, Unix. |
| 218 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
| 220 | .. function:: samefile(path1, path2) |
| 221 | |
| 222 | Return ``True`` if both pathname arguments refer to the same file or directory |
| 223 | (as indicated by device number and i-node number). Raise an exception if a |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 224 | :func:`os.stat` call on either pathname fails. Availability: Unix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | .. function:: sameopenfile(fp1, fp2) |
| 228 | |
| 229 | Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file. |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 230 | Availability: Unix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | .. function:: samestat(stat1, stat2) |
| 234 | |
| 235 | Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file. |
| 236 | These structures may have been returned by :func:`fstat`, :func:`lstat`, or |
| 237 | :func:`stat`. This function implements the underlying comparison used by |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 238 | :func:`samefile` and :func:`sameopenfile`. Availability: Unix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | .. function:: split(path) |
| 242 | |
| 243 | Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last |
| 244 | pathname component and *head* is everything leading up to that. The *tail* part |
| 245 | will never contain a slash; if *path* ends in a slash, *tail* will be empty. If |
| 246 | there is no slash in *path*, *head* will be empty. If *path* is empty, both |
| 247 | *head* and *tail* are empty. Trailing slashes are stripped from *head* unless |
| 248 | it is the root (one or more slashes only). In nearly all cases, ``join(head, |
| 249 | tail)`` equals *path* (the only exception being when there were multiple slashes |
| 250 | separating *head* from *tail*). |
| 251 | |
| 252 | |
| 253 | .. function:: splitdrive(path) |
| 254 | |
| 255 | Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either |
| 256 | a drive specification or the empty string. On systems which do not use drive |
| 257 | specifications, *drive* will always be the empty string. In all cases, ``drive |
| 258 | + tail`` will be the same as *path*. |
| 259 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
| 261 | .. function:: splitext(path) |
| 262 | |
| 263 | Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext == |
| 264 | path``, and *ext* is empty or begins with a period and contains at most one |
| 265 | period. Leading periods on the basename are ignored; ``splitext('.cshrc')`` |
| 266 | returns ``('.cshrc', '')``. |
| 267 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | .. function:: splitunc(path) |
| 270 | |
| 271 | Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC |
| 272 | mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of |
| 273 | the path (such as ``r'\path\file.ext'``). For paths containing drive letters, |
| 274 | *unc* will always be the empty string. Availability: Windows. |
| 275 | |
| 276 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | .. data:: supports_unicode_filenames |
| 278 | |
| 279 | True if arbitrary Unicode strings can be used as file names (within limitations |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 280 | imposed by the file system), and if :func:`os.listdir` returns strings that |
| 281 | contain characters that cannot be represented by ASCII. |