blob: f9a3d894e89408fbd2804509b05dcba3a723d22c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{posixpath} ---
2 Common \POSIX{} pathname manipulations.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{posixpath}
4
5\modulesynopsis{Common \POSIX{} pathname manipulations.}
Guido van Rossum470be141995-03-17 16:07:09 +00006
Fred Drake65b32f71998-02-09 20:27:12 +00007This module implements some useful functions on \POSIX{} pathnames.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
Guido van Rossum470be141995-03-17 16:07:09 +00009\strong{Do not import this module directly.} Instead, import the
Fred Drake203b4f11998-05-14 15:16:12 +000010module \module{os}\refstmodindex{os} and use \code{os.path}.
Guido van Rossum470be141995-03-17 16:07:09 +000011
Fred Drake203b4f11998-05-14 15:16:12 +000012\index{path!operations}
Guido van Rossum470be141995-03-17 16:07:09 +000013
Guido van Rossum1804dc31999-01-29 18:05:05 +000014\begin{funcdesc}{abspath}{p}
15Return a normalized absolutized version of the pathname \var{p}. On
16most platforms, this is equivalent to
17\code{normpath(join(os.getcwd()), \var{p})}.
18\end{funcdesc}
19
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\begin{funcdesc}{basename}{p}
21Return the base name of pathname
22\var{p}.
23This is the second half of the pair returned by
24\code{posixpath.split(\var{p})}.
25\end{funcdesc}
26
27\begin{funcdesc}{commonprefix}{list}
28Return the longest string that is a prefix of all strings in
29\var{list}.
30If
31\var{list}
32is empty, return the empty string (\code{''}).
33\end{funcdesc}
34
35\begin{funcdesc}{exists}{p}
36Return true if
37\var{p}
38refers to an existing path.
39\end{funcdesc}
40
41\begin{funcdesc}{expanduser}{p}
42Return the argument with an initial component of \samp{\~} or
43\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
Fred Drake203b4f11998-05-14 15:16:12 +000044initial \samp{\~{}} is replaced by the environment variable
Fred Drake23a16341998-08-06 15:33:55 +000045\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
46password directory through the built-in module
47\module{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
48path does not begin with a tilde, the path is returned unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049\end{funcdesc}
50
Guido van Rossum17383111994-04-21 10:32:28 +000051\begin{funcdesc}{expandvars}{p}
52Return the argument with environment variables expanded. Substrings
53of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
54replaced by the value of environment variable \var{name}. Malformed
55variable names and references to non-existing variables are left
56unchanged.
57\end{funcdesc}
58
Guido van Rossum2babd7b1998-07-24 20:49:39 +000059\begin{funcdesc}{getsize}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000060\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000061Return the size, in bytes, of \var{filename}. Raise
62\exception{os.error} if the file does not exist or is inaccessible.
63\end{funcdesc}
64
65\begin{funcdesc}{getmtime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000066\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000067Return the time of last modification of \var{filename}. The return
68value is integer giving the number of seconds since the epoch (see the
69\module{time} module. Raise \exception{os.error} if the file does not
70exist or is inaccessible.
71\end{funcdesc}
72
73\begin{funcdesc}{getatime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000074\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000075Return the time of last access of \var{filename}. The return
76value is integer giving the number of seconds since the epoch (see the
77\module{time} module. Raise \exception{os.error} if the file does not
78exist or is inaccessible.
79\end{funcdesc}
80
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\begin{funcdesc}{isabs}{p}
82Return true if \var{p} is an absolute pathname (begins with a slash).
83\end{funcdesc}
84
85\begin{funcdesc}{isfile}{p}
86Return true if \var{p} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000087symbolic links, so both \function{islink()} and \function{isfile()}
88can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\end{funcdesc}
90
91\begin{funcdesc}{isdir}{p}
92Return true if \var{p} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000093symbolic links, so both \function{islink()} and \function{isdir()} can
94be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\end{funcdesc}
96
97\begin{funcdesc}{islink}{p}
98Return true if
99\var{p}
100refers to a directory entry that is a symbolic link.
101Always false if symbolic links are not supported.
102\end{funcdesc}
103
104\begin{funcdesc}{ismount}{p}
Guido van Rossum470be141995-03-17 16:07:09 +0000105Return true if pathname \var{p} is a \dfn{mount point}: a point in a
106file system where a different file system has been mounted. The
107function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
108different device than \var{p}, or whether \file{\var{p}/..} and
109\var{p} point to the same i-node on the same device --- this should
Fred Drake65b32f71998-02-09 20:27:12 +0000110detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\end{funcdesc}
112
Fred Drakecce10901998-03-17 06:33:25 +0000113\begin{funcdesc}{join}{p\optional{, q\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000114Joins one or more path components intelligently. If any component is
115an absolute path, all previous components are thrown away, and joining
116continues. The return value is the concatenation of \var{p}, and
117optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted
118between components, unless \var{p} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119\end{funcdesc}
120
121\begin{funcdesc}{normcase}{p}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000122Normalize the case of a pathname. On \UNIX{}, this returns the path
123unchanged; on case-insensitive filesystems, it converts the path to
124lowercase. On Windows, it also converts forward slashes to backward
125slashes.
126\end{funcdesc}
127
128\begin{funcdesc}{normpath}{p}
129Normalize a pathname. This collapses redundant separators and
130up-level references, e.g. \code{A//B}, \code{A/./B} and
131\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drakedb9693e1998-03-11 05:50:42 +0000132case (use \function{normcase()} for that). On Windows, it does
133converts forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{funcdesc}
135
Fred Drakecce10901998-03-17 06:33:25 +0000136\begin{funcdesc}{samefile}{p, q}
Fred Drakedb9693e1998-03-11 05:50:42 +0000137Return true if both pathname arguments refer to the same file or
138directory (as indicated by device number and i-node number).
139Raise an exception if a \function{os.stat()} call on either pathname
140fails.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\end{funcdesc}
142
143\begin{funcdesc}{split}{p}
Guido van Rossum7c2fdda1996-06-26 19:23:26 +0000144Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
145where \var{tail} is the last pathname component and \var{head} is
146everything leading up to that. The \var{tail} part will never contain
147a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
148there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
149empty, both \var{head} and \var{tail} are empty. Trailing slashes are
150stripped from \var{head} unless it is the root (one or more slashes
151only). In nearly all cases, \code{join(\var{head}, \var{tail})}
152equals \var{p} (the only exception being when there were multiple
153slashes separating \var{head} from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154\end{funcdesc}
155
156\begin{funcdesc}{splitext}{p}
157Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
158such that \code{\var{root} + \var{ext} == \var{p}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000159and \var{ext} is empty or begins with a period and contains
160at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161\end{funcdesc}
162
Fred Drakecce10901998-03-17 06:33:25 +0000163\begin{funcdesc}{walk}{p, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000164Calls the function \var{visit} with arguments
165\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
166directory tree rooted at \var{p} (including \var{p} itself, if it is a
167directory). The argument \var{dirname} specifies the visited directory,
168the argument \var{names} lists the files in the directory (gotten from
Fred Drakedb9693e1998-03-11 05:50:42 +0000169\code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000170The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000171influence the set of directories visited below \var{dirname}, e.g., to
172avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000173\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000174assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{funcdesc}