blob: 169641c9508520430b00015ab22e6af9fb54fb8f [file] [log] [blame]
Fred Drakeb23ee1d1999-02-01 20:20:39 +00001\section{\module{os.path} ---
2 Common pathname manipulations}
3\declaremodule{standard}{os.path}
Fred Drakeb91e9341998-07-23 17:59:49 +00004
Fred Drakeb23ee1d1999-02-01 20:20:39 +00005\modulesynopsis{Common pathname manipulations.}
Guido van Rossum470be141995-03-17 16:07:09 +00006
Fred Drakeb23ee1d1999-02-01 20:20:39 +00007This module implements some useful functions on pathnames.
Fred Drake203b4f11998-05-14 15:16:12 +00008\index{path!operations}
Guido van Rossum470be141995-03-17 16:07:09 +00009
Fred Drakeb23ee1d1999-02-01 20:20:39 +000010
Guido van Rossum1804dc31999-01-29 18:05:05 +000011\begin{funcdesc}{abspath}{p}
12Return a normalized absolutized version of the pathname \var{p}. On
13most platforms, this is equivalent to
14\code{normpath(join(os.getcwd()), \var{p})}.
15\end{funcdesc}
16
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017\begin{funcdesc}{basename}{p}
Fred Drakeb23ee1d1999-02-01 20:20:39 +000018Return the base name of pathname \var{p}. This is the second half of
19the pair returned by \code{split(\var{p})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\end{funcdesc}
21
22\begin{funcdesc}{commonprefix}{list}
23Return the longest string that is a prefix of all strings in
Fred Drakeb23ee1d1999-02-01 20:20:39 +000024\var{list}. If \var{list} is empty, return the empty string
25(\code{''}).
26\end{funcdesc}
27
28\begin{funcdesc}{dirname}{p}
29Return the directory name of pathname \var{p}. This is the first half
30of the pair returned by \code{split(\var{p})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\end{funcdesc}
32
33\begin{funcdesc}{exists}{p}
Fred Drakeb23ee1d1999-02-01 20:20:39 +000034Return true if \var{p} refers to an existing path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035\end{funcdesc}
36
37\begin{funcdesc}{expanduser}{p}
38Return the argument with an initial component of \samp{\~} or
39\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
Fred Drake203b4f11998-05-14 15:16:12 +000040initial \samp{\~{}} is replaced by the environment variable
Fred Drake23a16341998-08-06 15:33:55 +000041\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
42password directory through the built-in module
Fred Drakeb23ee1d1999-02-01 20:20:39 +000043\refmodule{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
44path does not begin with a tilde, the path is returned unchanged. On
45the Macintosh, this always returns \var{p} unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{funcdesc}
47
Guido van Rossum17383111994-04-21 10:32:28 +000048\begin{funcdesc}{expandvars}{p}
49Return the argument with environment variables expanded. Substrings
50of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
51replaced by the value of environment variable \var{name}. Malformed
52variable names and references to non-existing variables are left
Fred Drakeb23ee1d1999-02-01 20:20:39 +000053unchanged. On the Macintosh, this always returns \var{p} unchanged.
Guido van Rossum17383111994-04-21 10:32:28 +000054\end{funcdesc}
55
Guido van Rossum2babd7b1998-07-24 20:49:39 +000056\begin{funcdesc}{getsize}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000057\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000058Return the size, in bytes, of \var{filename}. Raise
59\exception{os.error} if the file does not exist or is inaccessible.
60\end{funcdesc}
61
62\begin{funcdesc}{getmtime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000063\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000064Return the time of last modification of \var{filename}. The return
65value is integer giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +000066\refmodule{time} module). Raise \exception{os.error} if the file does
67not exist or is inaccessible.
Guido van Rossum2babd7b1998-07-24 20:49:39 +000068\end{funcdesc}
69
70\begin{funcdesc}{getatime}{filename}
Fred Drakeb44e7531998-07-27 21:11:42 +000071\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000072Return the time of last access of \var{filename}. The return
73value is integer giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +000074\refmodule{time} module). Raise \exception{os.error} if the file does
75not exist or is inaccessible.
Guido van Rossum2babd7b1998-07-24 20:49:39 +000076\end{funcdesc}
77
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\begin{funcdesc}{isabs}{p}
79Return true if \var{p} is an absolute pathname (begins with a slash).
80\end{funcdesc}
81
82\begin{funcdesc}{isfile}{p}
83Return true if \var{p} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000084symbolic links, so both \function{islink()} and \function{isfile()}
85can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\end{funcdesc}
87
88\begin{funcdesc}{isdir}{p}
89Return true if \var{p} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000090symbolic links, so both \function{islink()} and \function{isdir()} can
91be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\end{funcdesc}
93
94\begin{funcdesc}{islink}{p}
95Return true if
96\var{p}
97refers to a directory entry that is a symbolic link.
98Always false if symbolic links are not supported.
99\end{funcdesc}
100
101\begin{funcdesc}{ismount}{p}
Guido van Rossum470be141995-03-17 16:07:09 +0000102Return true if pathname \var{p} is a \dfn{mount point}: a point in a
103file system where a different file system has been mounted. The
104function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
105different device than \var{p}, or whether \file{\var{p}/..} and
106\var{p} point to the same i-node on the same device --- this should
Fred Drake65b32f71998-02-09 20:27:12 +0000107detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108\end{funcdesc}
109
Fred Drakecce10901998-03-17 06:33:25 +0000110\begin{funcdesc}{join}{p\optional{, q\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000111Joins one or more path components intelligently. If any component is
112an absolute path, all previous components are thrown away, and joining
113continues. The return value is the concatenation of \var{p}, and
114optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted
115between components, unless \var{p} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116\end{funcdesc}
117
118\begin{funcdesc}{normcase}{p}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000119Normalize the case of a pathname. On \UNIX{}, this returns the path
120unchanged; on case-insensitive filesystems, it converts the path to
121lowercase. On Windows, it also converts forward slashes to backward
122slashes.
123\end{funcdesc}
124
125\begin{funcdesc}{normpath}{p}
126Normalize a pathname. This collapses redundant separators and
127up-level references, e.g. \code{A//B}, \code{A/./B} and
128\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drakedb9693e1998-03-11 05:50:42 +0000129case (use \function{normcase()} for that). On Windows, it does
130converts forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131\end{funcdesc}
132
Fred Drakecce10901998-03-17 06:33:25 +0000133\begin{funcdesc}{samefile}{p, q}
Fred Drakedb9693e1998-03-11 05:50:42 +0000134Return true if both pathname arguments refer to the same file or
135directory (as indicated by device number and i-node number).
136Raise an exception if a \function{os.stat()} call on either pathname
137fails.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138\end{funcdesc}
139
140\begin{funcdesc}{split}{p}
Guido van Rossum7c2fdda1996-06-26 19:23:26 +0000141Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
142where \var{tail} is the last pathname component and \var{head} is
143everything leading up to that. The \var{tail} part will never contain
144a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
145there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
146empty, both \var{head} and \var{tail} are empty. Trailing slashes are
147stripped from \var{head} unless it is the root (one or more slashes
148only). In nearly all cases, \code{join(\var{head}, \var{tail})}
149equals \var{p} (the only exception being when there were multiple
150slashes separating \var{head} from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151\end{funcdesc}
152
153\begin{funcdesc}{splitext}{p}
154Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
155such that \code{\var{root} + \var{ext} == \var{p}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000156and \var{ext} is empty or begins with a period and contains
157at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158\end{funcdesc}
159
Fred Drakecce10901998-03-17 06:33:25 +0000160\begin{funcdesc}{walk}{p, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161Calls the function \var{visit} with arguments
162\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
163directory tree rooted at \var{p} (including \var{p} itself, if it is a
164directory). The argument \var{dirname} specifies the visited directory,
165the argument \var{names} lists the files in the directory (gotten from
Fred Drakedb9693e1998-03-11 05:50:42 +0000166\code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000167The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000168influence the set of directories visited below \var{dirname}, e.g., to
169avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000170\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000171assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172\end{funcdesc}