blob: 728fe3613b9a2b36d2cc58e249b20b0fc776534a [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{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
Fred Drake65b32f71998-02-09 20:27:12 +00005This module implements some useful functions on \POSIX{} pathnames.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
Guido van Rossum470be141995-03-17 16:07:09 +00007\strong{Do not import this module directly.} Instead, import the
Fred Drake203b4f11998-05-14 15:16:12 +00008module \module{os}\refstmodindex{os} and use \code{os.path}.
Guido van Rossum470be141995-03-17 16:07:09 +00009
Fred Drake203b4f11998-05-14 15:16:12 +000010\index{path!operations}
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
Fred Drake203b4f11998-05-14 15:16:12 +000036initial \samp{\~{}} is replaced by the environment variable
37\code{\${}HOME}; % $ <-- bow to font-lock
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038an initial \samp{\~\var{user}} is looked up in the password directory through
Fred Drakedb9693e1998-03-11 05:50:42 +000039the built-in module \module{pwd}\refbimodindex{pwd}. If the expansion
40fails, or if the path does not begin with a tilde, the path is
41returned unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{funcdesc}
43
Guido van Rossum17383111994-04-21 10:32:28 +000044\begin{funcdesc}{expandvars}{p}
45Return the argument with environment variables expanded. Substrings
46of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
47replaced by the value of environment variable \var{name}. Malformed
48variable names and references to non-existing variables are left
49unchanged.
50\end{funcdesc}
51
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\begin{funcdesc}{isabs}{p}
53Return true if \var{p} is an absolute pathname (begins with a slash).
54\end{funcdesc}
55
56\begin{funcdesc}{isfile}{p}
57Return true if \var{p} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000058symbolic links, so both \function{islink()} and \function{isfile()}
59can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060\end{funcdesc}
61
62\begin{funcdesc}{isdir}{p}
63Return true if \var{p} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000064symbolic links, so both \function{islink()} and \function{isdir()} can
65be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066\end{funcdesc}
67
68\begin{funcdesc}{islink}{p}
69Return true if
70\var{p}
71refers to a directory entry that is a symbolic link.
72Always false if symbolic links are not supported.
73\end{funcdesc}
74
75\begin{funcdesc}{ismount}{p}
Guido van Rossum470be141995-03-17 16:07:09 +000076Return true if pathname \var{p} is a \dfn{mount point}: a point in a
77file system where a different file system has been mounted. The
78function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
79different device than \var{p}, or whether \file{\var{p}/..} and
80\var{p} point to the same i-node on the same device --- this should
Fred Drake65b32f71998-02-09 20:27:12 +000081detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
Fred Drakecce10901998-03-17 06:33:25 +000084\begin{funcdesc}{join}{p\optional{, q\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +000085Joins one or more path components intelligently. If any component is
86an absolute path, all previous components are thrown away, and joining
87continues. The return value is the concatenation of \var{p}, and
88optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted
89between components, unless \var{p} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\end{funcdesc}
91
92\begin{funcdesc}{normcase}{p}
Guido van Rossum1931c0c1998-02-18 14:00:05 +000093Normalize the case of a pathname. On \UNIX{}, this returns the path
94unchanged; on case-insensitive filesystems, it converts the path to
95lowercase. On Windows, it also converts forward slashes to backward
96slashes.
97\end{funcdesc}
98
99\begin{funcdesc}{normpath}{p}
100Normalize a pathname. This collapses redundant separators and
101up-level references, e.g. \code{A//B}, \code{A/./B} and
102\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drakedb9693e1998-03-11 05:50:42 +0000103case (use \function{normcase()} for that). On Windows, it does
104converts forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\end{funcdesc}
106
Fred Drakecce10901998-03-17 06:33:25 +0000107\begin{funcdesc}{samefile}{p, q}
Fred Drakedb9693e1998-03-11 05:50:42 +0000108Return true if both pathname arguments refer to the same file or
109directory (as indicated by device number and i-node number).
110Raise an exception if a \function{os.stat()} call on either pathname
111fails.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112\end{funcdesc}
113
114\begin{funcdesc}{split}{p}
Guido van Rossum7c2fdda1996-06-26 19:23:26 +0000115Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
116where \var{tail} is the last pathname component and \var{head} is
117everything leading up to that. The \var{tail} part will never contain
118a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
119there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
120empty, both \var{head} and \var{tail} are empty. Trailing slashes are
121stripped from \var{head} unless it is the root (one or more slashes
122only). In nearly all cases, \code{join(\var{head}, \var{tail})}
123equals \var{p} (the only exception being when there were multiple
124slashes separating \var{head} from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{funcdesc}
126
127\begin{funcdesc}{splitext}{p}
128Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
129such that \code{\var{root} + \var{ext} == \var{p}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000130and \var{ext} is empty or begins with a period and contains
131at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
Fred Drakecce10901998-03-17 06:33:25 +0000134\begin{funcdesc}{walk}{p, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135Calls the function \var{visit} with arguments
136\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
137directory tree rooted at \var{p} (including \var{p} itself, if it is a
138directory). The argument \var{dirname} specifies the visited directory,
139the argument \var{names} lists the files in the directory (gotten from
Fred Drakedb9693e1998-03-11 05:50:42 +0000140\code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000141The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000142influence the set of directories visited below \var{dirname}, e.g., to
143avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000144\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000145assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146\end{funcdesc}