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