blob: 763dd35ab87ce204df477151f67ce0f9e9463f99 [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
Fred Drakea9b9bf91999-02-02 18:58:33 +000011\begin{funcdesc}{abspath}{path}
12Return a normalized absolutized version of the pathname \var{path}.
13On most platforms, this is equivalent to
14\code{normpath(join(os.getcwd()), \var{path})}.
Guido van Rossum1804dc31999-01-29 18:05:05 +000015\end{funcdesc}
16
Fred Drakea9b9bf91999-02-02 18:58:33 +000017\begin{funcdesc}{basename}{path}
18Return the base name of pathname \var{path}. This is the second half
19of the pair returned by \code{split(\var{path})}.
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
Fred Drakea9b9bf91999-02-02 18:58:33 +000028\begin{funcdesc}{dirname}{path}
29Return the directory name of pathname \var{path}. This is the first
30half of the pair returned by \code{split(\var{path})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\end{funcdesc}
32
Fred Drakea9b9bf91999-02-02 18:58:33 +000033\begin{funcdesc}{exists}{path}
34Return true if \var{path} refers to an existing path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035\end{funcdesc}
36
Fred Drakea9b9bf91999-02-02 18:58:33 +000037\begin{funcdesc}{expanduser}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038Return 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
Fred Drakea9b9bf91999-02-02 18:58:33 +000045the Macintosh, this always returns \var{path} unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{funcdesc}
47
Fred Drakea9b9bf91999-02-02 18:58:33 +000048\begin{funcdesc}{expandvars}{path}
Guido van Rossum17383111994-04-21 10:32:28 +000049Return 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 Drakea9b9bf91999-02-02 18:58:33 +000053unchanged. On the Macintosh, this always returns \var{path}
54unchanged.
Guido van Rossum17383111994-04-21 10:32:28 +000055\end{funcdesc}
56
Fred Drakea9b9bf91999-02-02 18:58:33 +000057\begin{funcdesc}{getsize}{path}
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.
Fred Drakea9b9bf91999-02-02 18:58:33 +000060\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000061\end{funcdesc}
62
Fred Drakea9b9bf91999-02-02 18:58:33 +000063\begin{funcdesc}{getmtime}{path}
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.
Fred Drakea9b9bf91999-02-02 18:58:33 +000068\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000069\end{funcdesc}
70
Fred Drakea9b9bf91999-02-02 18:58:33 +000071\begin{funcdesc}{getatime}{path}
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.
Fred Drakea9b9bf91999-02-02 18:58:33 +000076\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000077\end{funcdesc}
78
Fred Drakea9b9bf91999-02-02 18:58:33 +000079\begin{funcdesc}{isabs}{path}
80Return true if \var{path} is an absolute pathname (begins with a
81slash).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
Fred Drakea9b9bf91999-02-02 18:58:33 +000084\begin{funcdesc}{isfile}{path}
85Return true if \var{path} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000086symbolic links, so both \function{islink()} and \function{isfile()}
87can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
Fred Drakea9b9bf91999-02-02 18:58:33 +000090\begin{funcdesc}{isdir}{path}
91Return true if \var{path} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000092symbolic links, so both \function{islink()} and \function{isdir()} can
93be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000094\end{funcdesc}
95
Fred Drakea9b9bf91999-02-02 18:58:33 +000096\begin{funcdesc}{islink}{path}
97Return true if \var{path} refers to a directory entry that is a
98symbolic link. Always false if symbolic links are not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\end{funcdesc}
100
Fred Drakea9b9bf91999-02-02 18:58:33 +0000101\begin{funcdesc}{ismount}{path}
102Return true if pathname \var{path} is a \dfn{mount point}: a point in
103a file system where a different file system has been mounted. The
104function checks whether \var{path}'s parent, \file{\var{path}/..}, is
105on a different device than \var{path}, or whether \file{\var{path}/..}
106and \var{path} point to the same i-node on the same device --- this
107should detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108\end{funcdesc}
109
Fred Drakea9b9bf91999-02-02 18:58:33 +0000110\begin{funcdesc}{join}{path1\optional{, path2\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
Fred Drakea9b9bf91999-02-02 18:58:33 +0000113continues. The return value is the concatenation of \var{path1}, and
114optionally \var{path2}, etc., with exactly one slash (\code{'/'})
115inserted between components, unless \var{path} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116\end{funcdesc}
117
Fred Drakea9b9bf91999-02-02 18:58:33 +0000118\begin{funcdesc}{normcase}{path}
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
Fred Drakea9b9bf91999-02-02 18:58:33 +0000125\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000126Normalize 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 Drakea9b9bf91999-02-02 18:58:33 +0000133\begin{funcdesc}{samefile}{path1, path2}
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
Fred Drakea9b9bf91999-02-02 18:58:33 +0000140\begin{funcdesc}{split}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000141Split the pathname \var{path} into a pair \code{(\var{head},
Fred Drakea9b9bf91999-02-02 18:58:33 +0000142\var{tail})}, where \var{tail} is the last pathname component and
143\var{head} is everything leading up to that. The \var{tail} part will
144never contain a slash; if \var{path} ends in a slash, \var{tail} will
145be empty. If there is no slash in \var{path}, \var{head} will be
146empty. If \var{path} is empty, both \var{head} and \var{tail} are
147empty. Trailing slashes are stripped from \var{head} unless it is the
148root (one or more slashes only). In nearly all cases,
149\code{join(\var{head}, \var{tail})} equals \var{path} (the only
150exception being when there were multiple slashes separating \var{head}
151from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000152\end{funcdesc}
153
Fred Drake0256c1f1999-02-03 19:24:44 +0000154\begin{funcdesc}{splitdrive}{path}
155Split the pathname \var{path} into a pair \code{(\var{drive},
156\var{tail})}, where \var{drive} is either a drive specification or the
157empty string. On systems which do not use drive specifications,
158\var{drive} will always be the empty string. In all cases,
159\code{\var{drive} + \var{tail}} will be the same as \var{path}.
160\end{funcdesc}
161
Fred Drakea9b9bf91999-02-02 18:58:33 +0000162\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000163Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000164such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000165and \var{ext} is empty or begins with a period and contains
166at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000167\end{funcdesc}
168
Fred Drakea9b9bf91999-02-02 18:58:33 +0000169\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000170Calls the function \var{visit} with arguments
171\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000172directory tree rooted at \var{path} (including \var{path} itself, if it
173is a directory). The argument \var{dirname} specifies the visited
174directory, the argument \var{names} lists the files in the directory
175(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000176The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000177influence the set of directories visited below \var{dirname}, e.g., to
178avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000179\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000180assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000181\end{funcdesc}