blob: d43d3e13d9f21958a77d7559b379a7d4fb1f26d8 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{posixpath}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00002\stmodindex{posixpath}
Guido van Rossum470be141995-03-17 16:07:09 +00003
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00004This module implements some useful functions on POSIX pathnames.
5
Guido van Rossum470be141995-03-17 16:07:09 +00006\strong{Do not import this module directly.} Instead, import the
7module \code{os} and use \code{os.path}.
8\stmodindex{os}
9
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010\renewcommand{\indexsubitem}{(in module posixpath)}
Guido van Rossum470be141995-03-17 16:07:09 +000011
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012\begin{funcdesc}{basename}{p}
13Return the base name of pathname
14\var{p}.
15This is the second half of the pair returned by
16\code{posixpath.split(\var{p})}.
17\end{funcdesc}
18
19\begin{funcdesc}{commonprefix}{list}
20Return the longest string that is a prefix of all strings in
21\var{list}.
22If
23\var{list}
24is empty, return the empty string (\code{''}).
25\end{funcdesc}
26
27\begin{funcdesc}{exists}{p}
28Return true if
29\var{p}
30refers to an existing path.
31\end{funcdesc}
32
33\begin{funcdesc}{expanduser}{p}
34Return the argument with an initial component of \samp{\~} or
35\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
36initial \samp{\~{}} is replaced by the environment variable \code{\${}HOME};
37an initial \samp{\~\var{user}} is looked up in the password directory through
38the built-in module \code{pwd}. If the expansion fails, or if the
39path does not begin with a tilde, the path is returned unchanged.
40\end{funcdesc}
41
Guido van Rossum17383111994-04-21 10:32:28 +000042\begin{funcdesc}{expandvars}{p}
43Return the argument with environment variables expanded. Substrings
44of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
45replaced by the value of environment variable \var{name}. Malformed
46variable names and references to non-existing variables are left
47unchanged.
48\end{funcdesc}
49
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050\begin{funcdesc}{isabs}{p}
51Return true if \var{p} is an absolute pathname (begins with a slash).
52\end{funcdesc}
53
54\begin{funcdesc}{isfile}{p}
55Return true if \var{p} is an existing regular file. This follows
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000056symbolic links, so both \code{islink()} and \code{isfile()} can be true for the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057path.
58\end{funcdesc}
59
60\begin{funcdesc}{isdir}{p}
61Return true if \var{p} is an existing directory. This follows
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000062symbolic links, so both \code{islink()} and \code{isdir()} can be true for the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063path.
64\end{funcdesc}
65
66\begin{funcdesc}{islink}{p}
67Return true if
68\var{p}
69refers to a directory entry that is a symbolic link.
70Always false if symbolic links are not supported.
71\end{funcdesc}
72
73\begin{funcdesc}{ismount}{p}
Guido van Rossum470be141995-03-17 16:07:09 +000074Return true if pathname \var{p} is a \dfn{mount point}: a point in a
75file system where a different file system has been mounted. The
76function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
77different device than \var{p}, or whether \file{\var{p}/..} and
78\var{p} point to the same i-node on the same device --- this should
79detect mount points for all \UNIX{} and POSIX variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{funcdesc}
81
82\begin{funcdesc}{join}{p\, q}
83Join the paths
84\var{p}
85and
86\var{q} intelligently:
87If
88\var{q}
89is an absolute path, the return value is
90\var{q}.
91Otherwise, the concatenation of
92\var{p}
93and
94\var{q}
95is returned, with a slash (\code{'/'}) inserted unless
96\var{p}
97is empty or ends in a slash.
98\end{funcdesc}
99
100\begin{funcdesc}{normcase}{p}
101Normalize the case of a pathname. This returns the path unchanged;
102however, a similar function in \code{macpath} converts upper case to
103lower case.
104\end{funcdesc}
105
106\begin{funcdesc}{samefile}{p\, q}
107Return true if both pathname arguments refer to the same file or directory
108(as indicated by device number and i-node number).
109Raise an exception if a stat call on either pathname fails.
110\end{funcdesc}
111
112\begin{funcdesc}{split}{p}
Guido van Rossum7c2fdda1996-06-26 19:23:26 +0000113Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
114where \var{tail} is the last pathname component and \var{head} is
115everything leading up to that. The \var{tail} part will never contain
116a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
117there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
118empty, both \var{head} and \var{tail} are empty. Trailing slashes are
119stripped from \var{head} unless it is the root (one or more slashes
120only). In nearly all cases, \code{join(\var{head}, \var{tail})}
121equals \var{p} (the only exception being when there were multiple
122slashes separating \var{head} from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\end{funcdesc}
124
125\begin{funcdesc}{splitext}{p}
126Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
127such that \code{\var{root} + \var{ext} == \var{p}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000128and \var{ext} is empty or begins with a period and contains
129at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000130\end{funcdesc}
131
132\begin{funcdesc}{walk}{p\, visit\, arg}
133Calls the function \var{visit} with arguments
134\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
135directory tree rooted at \var{p} (including \var{p} itself, if it is a
136directory). The argument \var{dirname} specifies the visited directory,
137the argument \var{names} lists the files in the directory (gotten from
Guido van Rossum470be141995-03-17 16:07:09 +0000138\code{posix.listdir(\var{dirname})}, so including \samp{.} and
139\samp{..}). The \var{visit} function may modify \var{names} to
140influence the set of directories visited below \var{dirname}, e.g., to
141avoid visiting certain parts of the tree. (The object referred to by
142\var{names} must be modified in place, using \code{del} or slice
143assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\end{funcdesc}