Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 1 | \section{\module{os.path} --- |
| 2 | Common pathname manipulations} |
| 3 | \declaremodule{standard}{os.path} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 5 | \modulesynopsis{Common pathname manipulations.} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 6 | |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 7 | This module implements some useful functions on pathnames. |
Fred Drake | 203b4f1 | 1998-05-14 15:16:12 +0000 | [diff] [blame] | 8 | \index{path!operations} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 9 | |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 10 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 11 | \begin{funcdesc}{abspath}{path} |
| 12 | Return a normalized absolutized version of the pathname \var{path}. |
| 13 | On most platforms, this is equivalent to |
Fred Drake | 39d4a02 | 1999-10-18 14:10:06 +0000 | [diff] [blame] | 14 | \code{normpath(join(os.getcwd(), \var{path}))}. |
Fred Drake | 154d909 | 1999-03-17 22:25:11 +0000 | [diff] [blame] | 15 | \versionadded{1.5.2} |
Guido van Rossum | 1804dc3 | 1999-01-29 18:05:05 +0000 | [diff] [blame] | 16 | \end{funcdesc} |
| 17 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 18 | \begin{funcdesc}{basename}{path} |
| 19 | Return the base name of pathname \var{path}. This is the second half |
Fred Drake | 3aecfc9 | 2000-10-26 21:38:23 +0000 | [diff] [blame] | 20 | of the pair returned by \code{split(\var{path})}. Note that the |
| 21 | result of this function is different from the |
| 22 | \UNIX{} \program{basename} program; where \program{basename} for |
| 23 | \code{'/foo/bar/'} returns \code{'bar'}, the \function{basename()} |
| 24 | function returns an empty string (\code{''}). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 | \end{funcdesc} |
| 26 | |
| 27 | \begin{funcdesc}{commonprefix}{list} |
Skip Montanaro | 297bf7c | 2000-08-23 16:58:32 +0000 | [diff] [blame] | 28 | Return the longest path prefix (taken character-by-character) that is a |
| 29 | prefix of all paths in |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 30 | \var{list}. If \var{list} is empty, return the empty string |
Skip Montanaro | 297bf7c | 2000-08-23 16:58:32 +0000 | [diff] [blame] | 31 | (\code{''}). Note that this may return invalid paths because it works a |
| 32 | character at a time. |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 33 | \end{funcdesc} |
| 34 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 35 | \begin{funcdesc}{dirname}{path} |
| 36 | Return the directory name of pathname \var{path}. This is the first |
| 37 | half of the pair returned by \code{split(\var{path})}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 38 | \end{funcdesc} |
| 39 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 40 | \begin{funcdesc}{exists}{path} |
| 41 | Return true if \var{path} refers to an existing path. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 44 | \begin{funcdesc}{expanduser}{path} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 45 | Return the argument with an initial component of \samp{\~} or |
| 46 | \samp{\~\var{user}} replaced by that \var{user}'s home directory. An |
Fred Drake | 203b4f1 | 1998-05-14 15:16:12 +0000 | [diff] [blame] | 47 | initial \samp{\~{}} is replaced by the environment variable |
Fred Drake | 23a1634 | 1998-08-06 15:33:55 +0000 | [diff] [blame] | 48 | \envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the |
| 49 | password directory through the built-in module |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 50 | \refmodule{pwd}\refbimodindex{pwd}. If the expansion fails, or if the |
| 51 | path does not begin with a tilde, the path is returned unchanged. On |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 52 | the Macintosh, this always returns \var{path} unchanged. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 55 | \begin{funcdesc}{expandvars}{path} |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 56 | Return the argument with environment variables expanded. Substrings |
| 57 | of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are |
| 58 | replaced by the value of environment variable \var{name}. Malformed |
| 59 | variable names and references to non-existing variables are left |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 60 | unchanged. On the Macintosh, this always returns \var{path} |
| 61 | unchanged. |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 62 | \end{funcdesc} |
| 63 | |
Fred Drake | d8a41e6 | 1999-02-19 17:54:10 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{getatime}{path} |
| 65 | Return the time of last access of \var{filename}. The return |
| 66 | value is integer giving the number of seconds since the epoch (see the |
| 67 | \refmodule{time} module). Raise \exception{os.error} if the file does |
| 68 | not exist or is inaccessible. |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 69 | \versionadded{1.5.2} |
Guido van Rossum | 2babd7b | 1998-07-24 20:49:39 +0000 | [diff] [blame] | 70 | \end{funcdesc} |
| 71 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 72 | \begin{funcdesc}{getmtime}{path} |
Guido van Rossum | 2babd7b | 1998-07-24 20:49:39 +0000 | [diff] [blame] | 73 | Return the time of last modification of \var{filename}. The return |
| 74 | value is integer giving the number of seconds since the epoch (see the |
Fred Drake | b23ee1d | 1999-02-01 20:20:39 +0000 | [diff] [blame] | 75 | \refmodule{time} module). Raise \exception{os.error} if the file does |
| 76 | not exist or is inaccessible. |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 77 | \versionadded{1.5.2} |
Guido van Rossum | 2babd7b | 1998-07-24 20:49:39 +0000 | [diff] [blame] | 78 | \end{funcdesc} |
| 79 | |
Fred Drake | d8a41e6 | 1999-02-19 17:54:10 +0000 | [diff] [blame] | 80 | \begin{funcdesc}{getsize}{path} |
| 81 | Return the size, in bytes, of \var{filename}. Raise |
| 82 | \exception{os.error} if the file does not exist or is inaccessible. |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 83 | \versionadded{1.5.2} |
Guido van Rossum | 2babd7b | 1998-07-24 20:49:39 +0000 | [diff] [blame] | 84 | \end{funcdesc} |
| 85 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 86 | \begin{funcdesc}{isabs}{path} |
| 87 | Return true if \var{path} is an absolute pathname (begins with a |
| 88 | slash). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 89 | \end{funcdesc} |
| 90 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 91 | \begin{funcdesc}{isfile}{path} |
| 92 | Return true if \var{path} is an existing regular file. This follows |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 93 | symbolic links, so both \function{islink()} and \function{isfile()} |
| 94 | can be true for the same path. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 95 | \end{funcdesc} |
| 96 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 97 | \begin{funcdesc}{isdir}{path} |
| 98 | Return true if \var{path} is an existing directory. This follows |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 99 | symbolic links, so both \function{islink()} and \function{isdir()} can |
| 100 | be true for the same path. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 101 | \end{funcdesc} |
| 102 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 103 | \begin{funcdesc}{islink}{path} |
| 104 | Return true if \var{path} refers to a directory entry that is a |
| 105 | symbolic link. Always false if symbolic links are not supported. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 106 | \end{funcdesc} |
| 107 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 108 | \begin{funcdesc}{ismount}{path} |
| 109 | Return true if pathname \var{path} is a \dfn{mount point}: a point in |
| 110 | a file system where a different file system has been mounted. The |
| 111 | function checks whether \var{path}'s parent, \file{\var{path}/..}, is |
| 112 | on a different device than \var{path}, or whether \file{\var{path}/..} |
| 113 | and \var{path} point to the same i-node on the same device --- this |
| 114 | should detect mount points for all \UNIX{} and \POSIX{} variants. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 115 | \end{funcdesc} |
| 116 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}} |
Barry Warsaw | 7574587 | 1997-02-18 21:53:53 +0000 | [diff] [blame] | 118 | Joins one or more path components intelligently. If any component is |
| 119 | an absolute path, all previous components are thrown away, and joining |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 120 | continues. The return value is the concatenation of \var{path1}, and |
| 121 | optionally \var{path2}, etc., with exactly one slash (\code{'/'}) |
| 122 | inserted between components, unless \var{path} is empty. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 123 | \end{funcdesc} |
| 124 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 125 | \begin{funcdesc}{normcase}{path} |
Guido van Rossum | 1931c0c | 1998-02-18 14:00:05 +0000 | [diff] [blame] | 126 | Normalize the case of a pathname. On \UNIX{}, this returns the path |
| 127 | unchanged; on case-insensitive filesystems, it converts the path to |
| 128 | lowercase. On Windows, it also converts forward slashes to backward |
| 129 | slashes. |
| 130 | \end{funcdesc} |
| 131 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 132 | \begin{funcdesc}{normpath}{path} |
Guido van Rossum | 1931c0c | 1998-02-18 14:00:05 +0000 | [diff] [blame] | 133 | Normalize a pathname. This collapses redundant separators and |
| 134 | up-level references, e.g. \code{A//B}, \code{A/./B} and |
| 135 | \code{A/foo/../B} all become \code{A/B}. It does not normalize the |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 136 | case (use \function{normcase()} for that). On Windows, it converts |
| 137 | forward slashes to backward slashes. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 138 | \end{funcdesc} |
| 139 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 140 | \begin{funcdesc}{samefile}{path1, path2} |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 141 | Return true if both pathname arguments refer to the same file or |
| 142 | directory (as indicated by device number and i-node number). |
| 143 | Raise an exception if a \function{os.stat()} call on either pathname |
| 144 | fails. |
Fred Drake | aa1afa8 | 1999-02-15 16:34:00 +0000 | [diff] [blame] | 145 | Availability: Macintosh, \UNIX{}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 146 | \end{funcdesc} |
| 147 | |
Fred Drake | d673d48 | 1999-02-03 22:31:30 +0000 | [diff] [blame] | 148 | \begin{funcdesc}{sameopenfile}{fp1, fp2} |
| 149 | Return true if the file objects \var{fp1} and \var{fp2} refer to the |
| 150 | same file. The two file objects may represent different file |
| 151 | descriptors. |
Fred Drake | aa1afa8 | 1999-02-15 16:34:00 +0000 | [diff] [blame] | 152 | Availability: Macintosh, \UNIX{}. |
Fred Drake | d673d48 | 1999-02-03 22:31:30 +0000 | [diff] [blame] | 153 | \end{funcdesc} |
| 154 | |
| 155 | \begin{funcdesc}{samestat}{stat1, stat2} |
| 156 | Return true if the stat tuples \var{stat1} and \var{stat2} refer to |
| 157 | the same file. These structures may have been returned by |
| 158 | \function{fstat()}, \function{lstat()}, or \function{stat()}. This |
| 159 | function implements the underlying comparison used by |
| 160 | \function{samefile()} and \function{sameopenfile()}. |
Fred Drake | aa1afa8 | 1999-02-15 16:34:00 +0000 | [diff] [blame] | 161 | Availability: Macintosh, \UNIX{}. |
Fred Drake | d673d48 | 1999-02-03 22:31:30 +0000 | [diff] [blame] | 162 | \end{funcdesc} |
| 163 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 164 | \begin{funcdesc}{split}{path} |
Fred Drake | d673d48 | 1999-02-03 22:31:30 +0000 | [diff] [blame] | 165 | Split the pathname \var{path} into a pair, \code{(\var{head}, |
| 166 | \var{tail})} where \var{tail} is the last pathname component and |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 167 | \var{head} is everything leading up to that. The \var{tail} part will |
| 168 | never contain a slash; if \var{path} ends in a slash, \var{tail} will |
| 169 | be empty. If there is no slash in \var{path}, \var{head} will be |
| 170 | empty. If \var{path} is empty, both \var{head} and \var{tail} are |
| 171 | empty. Trailing slashes are stripped from \var{head} unless it is the |
| 172 | root (one or more slashes only). In nearly all cases, |
| 173 | \code{join(\var{head}, \var{tail})} equals \var{path} (the only |
| 174 | exception being when there were multiple slashes separating \var{head} |
| 175 | from \var{tail}). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 176 | \end{funcdesc} |
| 177 | |
Fred Drake | 0256c1f | 1999-02-03 19:24:44 +0000 | [diff] [blame] | 178 | \begin{funcdesc}{splitdrive}{path} |
| 179 | Split the pathname \var{path} into a pair \code{(\var{drive}, |
Fred Drake | d673d48 | 1999-02-03 22:31:30 +0000 | [diff] [blame] | 180 | \var{tail})} where \var{drive} is either a drive specification or the |
Fred Drake | 0256c1f | 1999-02-03 19:24:44 +0000 | [diff] [blame] | 181 | empty string. On systems which do not use drive specifications, |
| 182 | \var{drive} will always be the empty string. In all cases, |
| 183 | \code{\var{drive} + \var{tail}} will be the same as \var{path}. |
| 184 | \end{funcdesc} |
| 185 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 186 | \begin{funcdesc}{splitext}{path} |
Fred Drake | 0256c1f | 1999-02-03 19:24:44 +0000 | [diff] [blame] | 187 | Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})} |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 188 | such that \code{\var{root} + \var{ext} == \var{path}}, |
Guido van Rossum | 56b30ea | 1996-08-19 23:00:50 +0000 | [diff] [blame] | 189 | and \var{ext} is empty or begins with a period and contains |
| 190 | at most one period. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 191 | \end{funcdesc} |
| 192 | |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 193 | \begin{funcdesc}{walk}{path, visit, arg} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 194 | Calls the function \var{visit} with arguments |
| 195 | \code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the |
Fred Drake | a9b9bf9 | 1999-02-02 18:58:33 +0000 | [diff] [blame] | 196 | directory tree rooted at \var{path} (including \var{path} itself, if it |
| 197 | is a directory). The argument \var{dirname} specifies the visited |
| 198 | directory, the argument \var{names} lists the files in the directory |
| 199 | (gotten from \code{os.listdir(\var{dirname})}). |
Guido van Rossum | e8e8799 | 1997-03-25 15:25:54 +0000 | [diff] [blame] | 200 | The \var{visit} function may modify \var{names} to |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 201 | influence the set of directories visited below \var{dirname}, e.g., to |
| 202 | avoid visiting certain parts of the tree. (The object referred to by |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 203 | \var{names} must be modified in place, using \keyword{del} or slice |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 204 | assignment.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 205 | \end{funcdesc} |