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