Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{posixpath}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{posixpath} |
| 3 | |
| 4 | \modulesynopsis{Common \POSIX{} pathname manipulations.} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 5 | |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 6 | This module implements some useful functions on \POSIX{} pathnames. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 8 | \strong{Do not import this module directly.} Instead, import the |
Fred Drake | 203b4f1 | 1998-05-14 15:16:12 +0000 | [diff] [blame] | 9 | module \module{os}\refstmodindex{os} and use \code{os.path}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 10 | |
Fred Drake | 203b4f1 | 1998-05-14 15:16:12 +0000 | [diff] [blame] | 11 | \index{path!operations} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 13 | \begin{funcdesc}{basename}{p} |
| 14 | Return the base name of pathname |
| 15 | \var{p}. |
| 16 | This is the second half of the pair returned by |
| 17 | \code{posixpath.split(\var{p})}. |
| 18 | \end{funcdesc} |
| 19 | |
| 20 | \begin{funcdesc}{commonprefix}{list} |
| 21 | Return the longest string that is a prefix of all strings in |
| 22 | \var{list}. |
| 23 | If |
| 24 | \var{list} |
| 25 | is empty, return the empty string (\code{''}). |
| 26 | \end{funcdesc} |
| 27 | |
| 28 | \begin{funcdesc}{exists}{p} |
| 29 | Return true if |
| 30 | \var{p} |
| 31 | refers to an existing path. |
| 32 | \end{funcdesc} |
| 33 | |
| 34 | \begin{funcdesc}{expanduser}{p} |
| 35 | Return the argument with an initial component of \samp{\~} or |
| 36 | \samp{\~\var{user}} replaced by that \var{user}'s home directory. An |
Fred Drake | 203b4f1 | 1998-05-14 15:16:12 +0000 | [diff] [blame] | 37 | initial \samp{\~{}} is replaced by the environment variable |
| 38 | \code{\${}HOME}; % $ <-- bow to font-lock |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 39 | an initial \samp{\~\var{user}} is looked up in the password directory through |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 40 | the built-in module \module{pwd}\refbimodindex{pwd}. If the expansion |
| 41 | fails, or if the path does not begin with a tilde, the path is |
| 42 | returned unchanged. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{expandvars}{p} |
| 46 | Return the argument with environment variables expanded. Substrings |
| 47 | of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are |
| 48 | replaced by the value of environment variable \var{name}. Malformed |
| 49 | variable names and references to non-existing variables are left |
| 50 | unchanged. |
| 51 | \end{funcdesc} |
| 52 | |
Guido van Rossum | 2babd7b | 1998-07-24 20:49:39 +0000 | [diff] [blame] | 53 | \begin{funcdesc}{getsize}{filename} |
| 54 | Return the size, in bytes, of \var{filename}. Raise |
| 55 | \exception{os.error} if the file does not exist or is inaccessible. |
| 56 | \end{funcdesc} |
| 57 | |
| 58 | \begin{funcdesc}{getmtime}{filename} |
| 59 | Return the time of last modification of \var{filename}. The return |
| 60 | value is integer giving the number of seconds since the epoch (see the |
| 61 | \module{time} module. Raise \exception{os.error} if the file does not |
| 62 | exist or is inaccessible. |
| 63 | \end{funcdesc} |
| 64 | |
| 65 | \begin{funcdesc}{getatime}{filename} |
| 66 | Return the time of last access of \var{filename}. The return |
| 67 | value is integer giving the number of seconds since the epoch (see the |
| 68 | \module{time} module. Raise \exception{os.error} if the file does not |
| 69 | exist or is inaccessible. |
| 70 | \end{funcdesc} |
| 71 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 72 | \begin{funcdesc}{isabs}{p} |
| 73 | Return true if \var{p} is an absolute pathname (begins with a slash). |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | \begin{funcdesc}{isfile}{p} |
| 77 | Return true if \var{p} is an existing regular file. This follows |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 78 | symbolic links, so both \function{islink()} and \function{isfile()} |
| 79 | can be true for the same path. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{isdir}{p} |
| 83 | Return true if \var{p} is an existing directory. This follows |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 84 | symbolic links, so both \function{islink()} and \function{isdir()} can |
| 85 | be true for the same path. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 86 | \end{funcdesc} |
| 87 | |
| 88 | \begin{funcdesc}{islink}{p} |
| 89 | Return true if |
| 90 | \var{p} |
| 91 | refers to a directory entry that is a symbolic link. |
| 92 | Always false if symbolic links are not supported. |
| 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{ismount}{p} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 96 | Return true if pathname \var{p} is a \dfn{mount point}: a point in a |
| 97 | file system where a different file system has been mounted. The |
| 98 | function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a |
| 99 | different device than \var{p}, or whether \file{\var{p}/..} and |
| 100 | \var{p} point to the same i-node on the same device --- this should |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 101 | detect mount points for all \UNIX{} and \POSIX{} variants. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 102 | \end{funcdesc} |
| 103 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 104 | \begin{funcdesc}{join}{p\optional{, q\optional{, ...}}} |
Barry Warsaw | 7574587 | 1997-02-18 21:53:53 +0000 | [diff] [blame] | 105 | Joins one or more path components intelligently. If any component is |
| 106 | an absolute path, all previous components are thrown away, and joining |
| 107 | continues. The return value is the concatenation of \var{p}, and |
| 108 | optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted |
| 109 | between components, unless \var{p} is empty. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 110 | \end{funcdesc} |
| 111 | |
| 112 | \begin{funcdesc}{normcase}{p} |
Guido van Rossum | 1931c0c | 1998-02-18 14:00:05 +0000 | [diff] [blame] | 113 | Normalize the case of a pathname. On \UNIX{}, this returns the path |
| 114 | unchanged; on case-insensitive filesystems, it converts the path to |
| 115 | lowercase. On Windows, it also converts forward slashes to backward |
| 116 | slashes. |
| 117 | \end{funcdesc} |
| 118 | |
| 119 | \begin{funcdesc}{normpath}{p} |
| 120 | Normalize a pathname. This collapses redundant separators and |
| 121 | up-level references, e.g. \code{A//B}, \code{A/./B} and |
| 122 | \code{A/foo/../B} all become \code{A/B}. It does not normalize the |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 123 | case (use \function{normcase()} for that). On Windows, it does |
| 124 | converts forward slashes to backward slashes. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 125 | \end{funcdesc} |
| 126 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 127 | \begin{funcdesc}{samefile}{p, q} |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 128 | Return true if both pathname arguments refer to the same file or |
| 129 | directory (as indicated by device number and i-node number). |
| 130 | Raise an exception if a \function{os.stat()} call on either pathname |
| 131 | fails. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 132 | \end{funcdesc} |
| 133 | |
| 134 | \begin{funcdesc}{split}{p} |
Guido van Rossum | 7c2fdda | 1996-06-26 19:23:26 +0000 | [diff] [blame] | 135 | Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})}, |
| 136 | where \var{tail} is the last pathname component and \var{head} is |
| 137 | everything leading up to that. The \var{tail} part will never contain |
| 138 | a slash; if \var{p} ends in a slash, \var{tail} will be empty. If |
| 139 | there is no slash in \var{p}, \var{head} will be empty. If \var{p} is |
| 140 | empty, both \var{head} and \var{tail} are empty. Trailing slashes are |
| 141 | stripped from \var{head} unless it is the root (one or more slashes |
| 142 | only). In nearly all cases, \code{join(\var{head}, \var{tail})} |
| 143 | equals \var{p} (the only exception being when there were multiple |
| 144 | slashes separating \var{head} from \var{tail}). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 145 | \end{funcdesc} |
| 146 | |
| 147 | \begin{funcdesc}{splitext}{p} |
| 148 | Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})} |
| 149 | such that \code{\var{root} + \var{ext} == \var{p}}, |
Guido van Rossum | 56b30ea | 1996-08-19 23:00:50 +0000 | [diff] [blame] | 150 | and \var{ext} is empty or begins with a period and contains |
| 151 | at most one period. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 152 | \end{funcdesc} |
| 153 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 154 | \begin{funcdesc}{walk}{p, visit, arg} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 155 | Calls the function \var{visit} with arguments |
| 156 | \code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the |
| 157 | directory tree rooted at \var{p} (including \var{p} itself, if it is a |
| 158 | directory). The argument \var{dirname} specifies the visited directory, |
| 159 | the argument \var{names} lists the files in the directory (gotten from |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 160 | \code{os.listdir(\var{dirname})}). |
Guido van Rossum | e8e8799 | 1997-03-25 15:25:54 +0000 | [diff] [blame] | 161 | The \var{visit} function may modify \var{names} to |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 162 | influence the set of directories visited below \var{dirname}, e.g., to |
| 163 | avoid visiting certain parts of the tree. (The object referred to by |
Fred Drake | db9693e | 1998-03-11 05:50:42 +0000 | [diff] [blame] | 164 | \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] | 165 | assignment.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 166 | \end{funcdesc} |