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