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