Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{posixpath}} |
| 2 | |
| 3 | \stmodindex{posixpath} |
| 4 | This module implements some useful functions on POSIX pathnames. |
| 5 | |
| 6 | \renewcommand{\indexsubitem}{(in module posixpath)} |
| 7 | \begin{funcdesc}{basename}{p} |
| 8 | Return the base name of pathname |
| 9 | \var{p}. |
| 10 | This is the second half of the pair returned by |
| 11 | \code{posixpath.split(\var{p})}. |
| 12 | \end{funcdesc} |
| 13 | |
| 14 | \begin{funcdesc}{commonprefix}{list} |
| 15 | Return the longest string that is a prefix of all strings in |
| 16 | \var{list}. |
| 17 | If |
| 18 | \var{list} |
| 19 | is empty, return the empty string (\code{''}). |
| 20 | \end{funcdesc} |
| 21 | |
| 22 | \begin{funcdesc}{exists}{p} |
| 23 | Return true if |
| 24 | \var{p} |
| 25 | refers to an existing path. |
| 26 | \end{funcdesc} |
| 27 | |
| 28 | \begin{funcdesc}{expanduser}{p} |
| 29 | Return the argument with an initial component of \samp{\~} or |
| 30 | \samp{\~\var{user}} replaced by that \var{user}'s home directory. An |
| 31 | initial \samp{\~{}} is replaced by the environment variable \code{\${}HOME}; |
| 32 | an initial \samp{\~\var{user}} is looked up in the password directory through |
| 33 | the built-in module \code{pwd}. If the expansion fails, or if the |
| 34 | path does not begin with a tilde, the path is returned unchanged. |
| 35 | \end{funcdesc} |
| 36 | |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 37 | \begin{funcdesc}{expandvars}{p} |
| 38 | Return the argument with environment variables expanded. Substrings |
| 39 | of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are |
| 40 | replaced by the value of environment variable \var{name}. Malformed |
| 41 | variable names and references to non-existing variables are left |
| 42 | unchanged. |
| 43 | \end{funcdesc} |
| 44 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{isabs}{p} |
| 46 | Return true if \var{p} is an absolute pathname (begins with a slash). |
| 47 | \end{funcdesc} |
| 48 | |
| 49 | \begin{funcdesc}{isfile}{p} |
| 50 | Return true if \var{p} is an existing regular file. This follows |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame^] | 51 | symbolic links, so both \code{islink()} and \code{isfile()} can be true for the same |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 52 | path. |
| 53 | \end{funcdesc} |
| 54 | |
| 55 | \begin{funcdesc}{isdir}{p} |
| 56 | Return true if \var{p} is an existing directory. This follows |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame^] | 57 | symbolic links, so both \code{islink()} and \code{isdir()} can be true for the same |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | path. |
| 59 | \end{funcdesc} |
| 60 | |
| 61 | \begin{funcdesc}{islink}{p} |
| 62 | Return true if |
| 63 | \var{p} |
| 64 | refers to a directory entry that is a symbolic link. |
| 65 | Always false if symbolic links are not supported. |
| 66 | \end{funcdesc} |
| 67 | |
| 68 | \begin{funcdesc}{ismount}{p} |
| 69 | Return true if \var{p} is a mount point. (This currently checks whether |
| 70 | \code{\var{p}/..} is on a different device from \var{p} or whether |
| 71 | \code{\var{p}/..} and \var{p} point to the same i-node on the same |
| 72 | device --- is this test correct for all \UNIX{} and POSIX variants?) |
| 73 | \end{funcdesc} |
| 74 | |
| 75 | \begin{funcdesc}{join}{p\, q} |
| 76 | Join the paths |
| 77 | \var{p} |
| 78 | and |
| 79 | \var{q} intelligently: |
| 80 | If |
| 81 | \var{q} |
| 82 | is an absolute path, the return value is |
| 83 | \var{q}. |
| 84 | Otherwise, the concatenation of |
| 85 | \var{p} |
| 86 | and |
| 87 | \var{q} |
| 88 | is returned, with a slash (\code{'/'}) inserted unless |
| 89 | \var{p} |
| 90 | is empty or ends in a slash. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{normcase}{p} |
| 94 | Normalize the case of a pathname. This returns the path unchanged; |
| 95 | however, a similar function in \code{macpath} converts upper case to |
| 96 | lower case. |
| 97 | \end{funcdesc} |
| 98 | |
| 99 | \begin{funcdesc}{samefile}{p\, q} |
| 100 | Return true if both pathname arguments refer to the same file or directory |
| 101 | (as indicated by device number and i-node number). |
| 102 | Raise an exception if a stat call on either pathname fails. |
| 103 | \end{funcdesc} |
| 104 | |
| 105 | \begin{funcdesc}{split}{p} |
| 106 | Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})}, where |
| 107 | \var{tail} is the last pathname component and \var{head} is |
| 108 | everything leading up to that. If \var{p} ends in a slash (except if |
| 109 | it is the root), the trailing slash is removed and the operation |
| 110 | applied to the result; otherwise, \code{join(\var{head}, \var{tail})} equals |
| 111 | \var{p}. The \var{tail} part never contains a slash. Some boundary |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame^] | 112 | cases:\ if \var{p} is the root, \var{head} equals \var{p} and |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 113 | \var{tail} is empty; if \var{p} is empty, both \var{head} and |
| 114 | \var{tail} are empty; if \var{p} contains no slash, \var{head} is |
| 115 | empty and \var{tail} equals \var{p}. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{splitext}{p} |
| 119 | Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})} |
| 120 | such that \code{\var{root} + \var{ext} == \var{p}}, |
| 121 | the last component of \var{root} contains no periods, |
| 122 | and \var{ext} is empty or begins with a period. |
| 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{funcdesc}{walk}{p\, visit\, arg} |
| 126 | Calls the function \var{visit} with arguments |
| 127 | \code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the |
| 128 | directory tree rooted at \var{p} (including \var{p} itself, if it is a |
| 129 | directory). The argument \var{dirname} specifies the visited directory, |
| 130 | the argument \var{names} lists the files in the directory (gotten from |
| 131 | \code{posix.listdir(\var{dirname})}). The \var{visit} function may |
| 132 | modify \var{names} to influence the set of directories visited below |
| 133 | \var{dirname}, e.g., to avoid visiting certain parts of the tree. (The |
| 134 | object referred to by \var{names} must be modified in place, using |
| 135 | \code{del} or slice assignment.) |
| 136 | \end{funcdesc} |