blob: 87c80109bf384426d40ab6567ad0089787176e84 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{posixpath}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{posixpath}
3
4\modulesynopsis{Common \POSIX{} pathname manipulations.}
Guido van Rossum470be141995-03-17 16:07:09 +00005
Fred Drake65b32f71998-02-09 20:27:12 +00006This module implements some useful functions on \POSIX{} pathnames.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Guido van Rossum470be141995-03-17 16:07:09 +00008\strong{Do not import this module directly.} Instead, import the
Fred Drake203b4f11998-05-14 15:16:12 +00009module \module{os}\refstmodindex{os} and use \code{os.path}.
Guido van Rossum470be141995-03-17 16:07:09 +000010
Fred Drake203b4f11998-05-14 15:16:12 +000011\index{path!operations}
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
Fred Drake203b4f11998-05-14 15:16:12 +000037initial \samp{\~{}} is replaced by the environment variable
Fred Drake23a16341998-08-06 15:33:55 +000038\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
39password directory through the built-in module
40\module{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
41path does not begin with a tilde, the path is returned 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 Rossum2babd7b1998-07-24 20:49:39 +000052\begin{funcdesc}{getsize}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000053\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000054Return the size, in bytes, of \var{filename}. Raise
55\exception{os.error} if the file does not exist or is inaccessible.
56\end{funcdesc}
57
58\begin{funcdesc}{getmtime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000059\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000060Return the time of last modification of \var{filename}. The return
61value is integer giving the number of seconds since the epoch (see the
62\module{time} module. Raise \exception{os.error} if the file does not
63exist or is inaccessible.
64\end{funcdesc}
65
66\begin{funcdesc}{getatime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000067\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000068Return the time of last access of \var{filename}. The return
69value is integer giving the number of seconds since the epoch (see the
70\module{time} module. Raise \exception{os.error} if the file does not
71exist or is inaccessible.
72\end{funcdesc}
73
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074\begin{funcdesc}{isabs}{p}
75Return true if \var{p} is an absolute pathname (begins with a slash).
76\end{funcdesc}
77
78\begin{funcdesc}{isfile}{p}
79Return true if \var{p} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000080symbolic links, so both \function{islink()} and \function{isfile()}
81can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
84\begin{funcdesc}{isdir}{p}
85Return true if \var{p} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000086symbolic links, so both \function{islink()} and \function{isdir()} can
87be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
90\begin{funcdesc}{islink}{p}
91Return true if
92\var{p}
93refers to a directory entry that is a symbolic link.
94Always false if symbolic links are not supported.
95\end{funcdesc}
96
97\begin{funcdesc}{ismount}{p}
Guido van Rossum470be141995-03-17 16:07:09 +000098Return true if pathname \var{p} is a \dfn{mount point}: a point in a
99file system where a different file system has been mounted. The
100function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
101different device than \var{p}, or whether \file{\var{p}/..} and
102\var{p} point to the same i-node on the same device --- this should
Fred Drake65b32f71998-02-09 20:27:12 +0000103detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104\end{funcdesc}
105
Fred Drakecce10901998-03-17 06:33:25 +0000106\begin{funcdesc}{join}{p\optional{, q\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000107Joins one or more path components intelligently. If any component is
108an absolute path, all previous components are thrown away, and joining
109continues. The return value is the concatenation of \var{p}, and
110optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted
111between components, unless \var{p} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112\end{funcdesc}
113
114\begin{funcdesc}{normcase}{p}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000115Normalize the case of a pathname. On \UNIX{}, this returns the path
116unchanged; on case-insensitive filesystems, it converts the path to
117lowercase. On Windows, it also converts forward slashes to backward
118slashes.
119\end{funcdesc}
120
121\begin{funcdesc}{normpath}{p}
122Normalize a pathname. This collapses redundant separators and
123up-level references, e.g. \code{A//B}, \code{A/./B} and
124\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drakedb9693e1998-03-11 05:50:42 +0000125case (use \function{normcase()} for that). On Windows, it does
126converts forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\end{funcdesc}
128
Fred Drakecce10901998-03-17 06:33:25 +0000129\begin{funcdesc}{samefile}{p, q}
Fred Drakedb9693e1998-03-11 05:50:42 +0000130Return true if both pathname arguments refer to the same file or
131directory (as indicated by device number and i-node number).
132Raise an exception if a \function{os.stat()} call on either pathname
133fails.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{funcdesc}
135
136\begin{funcdesc}{split}{p}
Guido van Rossum7c2fdda1996-06-26 19:23:26 +0000137Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
138where \var{tail} is the last pathname component and \var{head} is
139everything leading up to that. The \var{tail} part will never contain
140a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
141there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
142empty, both \var{head} and \var{tail} are empty. Trailing slashes are
143stripped from \var{head} unless it is the root (one or more slashes
144only). In nearly all cases, \code{join(\var{head}, \var{tail})}
145equals \var{p} (the only exception being when there were multiple
146slashes separating \var{head} from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000147\end{funcdesc}
148
149\begin{funcdesc}{splitext}{p}
150Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
151such that \code{\var{root} + \var{ext} == \var{p}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000152and \var{ext} is empty or begins with a period and contains
153at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154\end{funcdesc}
155
Fred Drakecce10901998-03-17 06:33:25 +0000156\begin{funcdesc}{walk}{p, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000157Calls the function \var{visit} with arguments
158\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
159directory tree rooted at \var{p} (including \var{p} itself, if it is a
160directory). The argument \var{dirname} specifies the visited directory,
161the argument \var{names} lists the files in the directory (gotten from
Fred Drakedb9693e1998-03-11 05:50:42 +0000162\code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000163The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000164influence the set of directories visited below \var{dirname}, e.g., to
165avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000166\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000167assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168\end{funcdesc}