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