blob: aa790e7693b65b518b6c3b2994d4074384e94a1f [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 Drake0aa811c2001-10-20 04:24:09 +000010\warning{On Windows, many of these functions do not properly
Fred Drakebbf7a402001-09-28 16:14:18 +000011support UNC pathnames. \function{splitunc()} and \function{ismount()}
Fred Drake0aa811c2001-10-20 04:24:09 +000012do handle them correctly.}
Fred Drakebbf7a402001-09-28 16:14:18 +000013
Fred Drakeb23ee1d1999-02-01 20:20:39 +000014
Fred Drakea9b9bf91999-02-02 18:58:33 +000015\begin{funcdesc}{abspath}{path}
16Return a normalized absolutized version of the pathname \var{path}.
17On most platforms, this is equivalent to
Fred Drake39d4a021999-10-18 14:10:06 +000018\code{normpath(join(os.getcwd(), \var{path}))}.
Fred Drake154d9091999-03-17 22:25:11 +000019\versionadded{1.5.2}
Guido van Rossum1804dc31999-01-29 18:05:05 +000020\end{funcdesc}
21
Fred Drakea9b9bf91999-02-02 18:58:33 +000022\begin{funcdesc}{basename}{path}
23Return the base name of pathname \var{path}. This is the second half
Fred Drake3aecfc92000-10-26 21:38:23 +000024of the pair returned by \code{split(\var{path})}. Note that the
25result of this function is different from the
26\UNIX{} \program{basename} program; where \program{basename} for
27\code{'/foo/bar/'} returns \code{'bar'}, the \function{basename()}
28function returns an empty string (\code{''}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029\end{funcdesc}
30
31\begin{funcdesc}{commonprefix}{list}
Skip Montanaro297bf7c2000-08-23 16:58:32 +000032Return the longest path prefix (taken character-by-character) that is a
33prefix of all paths in
Fred Drakeb23ee1d1999-02-01 20:20:39 +000034\var{list}. If \var{list} is empty, return the empty string
Skip Montanaro297bf7c2000-08-23 16:58:32 +000035(\code{''}). Note that this may return invalid paths because it works a
36character at a time.
Fred Drakeb23ee1d1999-02-01 20:20:39 +000037\end{funcdesc}
38
Fred Drakea9b9bf91999-02-02 18:58:33 +000039\begin{funcdesc}{dirname}{path}
40Return the directory name of pathname \var{path}. This is the first
41half of the pair returned by \code{split(\var{path})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{funcdesc}
43
Fred Drakea9b9bf91999-02-02 18:58:33 +000044\begin{funcdesc}{exists}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000045Return \code{True} if \var{path} refers to an existing path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{funcdesc}
47
Fred Drakea9b9bf91999-02-02 18:58:33 +000048\begin{funcdesc}{expanduser}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049Return the argument with an initial component of \samp{\~} or
50\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
Fred Drake203b4f11998-05-14 15:16:12 +000051initial \samp{\~{}} is replaced by the environment variable
Fred Drake23a16341998-08-06 15:33:55 +000052\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
53password directory through the built-in module
Fred Drakeb23ee1d1999-02-01 20:20:39 +000054\refmodule{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
55path does not begin with a tilde, the path is returned unchanged. On
Fred Drakea9b9bf91999-02-02 18:58:33 +000056the Macintosh, this always returns \var{path} unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057\end{funcdesc}
58
Fred Drakea9b9bf91999-02-02 18:58:33 +000059\begin{funcdesc}{expandvars}{path}
Guido van Rossum17383111994-04-21 10:32:28 +000060Return the argument with environment variables expanded. Substrings
61of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
62replaced by the value of environment variable \var{name}. Malformed
63variable names and references to non-existing variables are left
Fred Drakea9b9bf91999-02-02 18:58:33 +000064unchanged. On the Macintosh, this always returns \var{path}
65unchanged.
Guido van Rossum17383111994-04-21 10:32:28 +000066\end{funcdesc}
67
Fred Draked8a41e61999-02-19 17:54:10 +000068\begin{funcdesc}{getatime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000069Return the time of last access of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000070value is a number giving the number of seconds since the epoch (see the
Fred Draked8a41e61999-02-19 17:54:10 +000071\refmodule{time} module). Raise \exception{os.error} if the file does
72not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000073\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +000074\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000075\end{funcdesc}
76
Fred Drakea9b9bf91999-02-02 18:58:33 +000077\begin{funcdesc}{getmtime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000078Return the time of last modification of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000079value is a number giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +000080\refmodule{time} module). Raise \exception{os.error} if the file does
81not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000082\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +000083\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
84\end{funcdesc}
85
86\begin{funcdesc}{getctime}{path}
Raymond Hettingerc43a7e72003-10-29 00:46:19 +000087Return the system's ctime which, on some systems (like \UNIX) is the
88time of the last change, and, on others (like Windows), is the
89creation time for \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000090value is a number giving the number of seconds since the epoch (see the
91\refmodule{time} module). Raise \exception{os.error} if the file does
92not exist or is inaccessible.
93\versionadded{2.3}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000094\end{funcdesc}
95
Fred Draked8a41e61999-02-19 17:54:10 +000096\begin{funcdesc}{getsize}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000097Return the size, in bytes, of \var{path}. Raise
Fred Draked8a41e61999-02-19 17:54:10 +000098\exception{os.error} if the file does not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000099\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +0000100\end{funcdesc}
101
Fred Drakea9b9bf91999-02-02 18:58:33 +0000102\begin{funcdesc}{isabs}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000103Return \code{True} if \var{path} is an absolute pathname (begins with a
Fred Drakea9b9bf91999-02-02 18:58:33 +0000104slash).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\end{funcdesc}
106
Fred Drakea9b9bf91999-02-02 18:58:33 +0000107\begin{funcdesc}{isfile}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000108Return \code{True} if \var{path} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000109symbolic links, so both \function{islink()} and \function{isfile()}
110can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\end{funcdesc}
112
Fred Drakea9b9bf91999-02-02 18:58:33 +0000113\begin{funcdesc}{isdir}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000114Return \code{True} if \var{path} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000115symbolic links, so both \function{islink()} and \function{isdir()} can
116be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000117\end{funcdesc}
118
Fred Drakea9b9bf91999-02-02 18:58:33 +0000119\begin{funcdesc}{islink}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000120Return \code{True} if \var{path} refers to a directory entry that is a
121symbolic link. Always \code{False} if symbolic links are not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\end{funcdesc}
123
Fred Drakea9b9bf91999-02-02 18:58:33 +0000124\begin{funcdesc}{ismount}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000125Return \code{True} if pathname \var{path} is a \dfn{mount point}: a point in
Fred Drakea9b9bf91999-02-02 18:58:33 +0000126a file system where a different file system has been mounted. The
127function checks whether \var{path}'s parent, \file{\var{path}/..}, is
128on a different device than \var{path}, or whether \file{\var{path}/..}
129and \var{path} point to the same i-node on the same device --- this
130should detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131\end{funcdesc}
132
Fred Drakea9b9bf91999-02-02 18:58:33 +0000133\begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000134Joins one or more path components intelligently. If any component is
135an absolute path, all previous components are thrown away, and joining
Fred Drakea9b9bf91999-02-02 18:58:33 +0000136continues. The return value is the concatenation of \var{path1}, and
Fred Drakec1ddc502002-09-12 18:01:26 +0000137optionally \var{path2}, etc., with exactly one directory separator
Martin v. Löwis04791042002-12-11 12:55:53 +0000138(\code{os.sep}) inserted between components, unless \var{path2} is
Fred Drakec1ddc502002-09-12 18:01:26 +0000139empty. Note that on Windows, since there is a current directory for
140each drive, \function{os.path.join("c:", "foo")} represents a path
141relative to the current directory on drive \file{C:} (\file{c:foo}), not
142\file{c:\textbackslash\textbackslash foo}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000143\end{funcdesc}
144
Fred Drakea9b9bf91999-02-02 18:58:33 +0000145\begin{funcdesc}{normcase}{path}
Fred Drakec37b65e2001-11-28 07:26:15 +0000146Normalize the case of a pathname. On \UNIX, this returns the path
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000147unchanged; on case-insensitive filesystems, it converts the path to
148lowercase. On Windows, it also converts forward slashes to backward
149slashes.
150\end{funcdesc}
151
Fred Drakea9b9bf91999-02-02 18:58:33 +0000152\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000153Normalize a pathname. This collapses redundant separators and
154up-level references, e.g. \code{A//B}, \code{A/./B} and
155\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drake38e5d272000-04-03 20:13:55 +0000156case (use \function{normcase()} for that). On Windows, it converts
157forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158\end{funcdesc}
159
Guido van Rossum83eeef42001-09-17 15:16:09 +0000160\begin{funcdesc}{realpath}{path}
161Return the canonical path of the specified filename, eliminating any
162symbolic links encountered in the path.
Fred Drakec37b65e2001-11-28 07:26:15 +0000163Availability: \UNIX.
Guido van Rossum83eeef42001-09-17 15:16:09 +0000164\versionadded{2.2}
165\end{funcdesc}
166
Fred Drakea9b9bf91999-02-02 18:58:33 +0000167\begin{funcdesc}{samefile}{path1, path2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000168Return \code{True} if both pathname arguments refer to the same file or
Fred Drakedb9693e1998-03-11 05:50:42 +0000169directory (as indicated by device number and i-node number).
170Raise an exception if a \function{os.stat()} call on either pathname
171fails.
Fred Drakec37b65e2001-11-28 07:26:15 +0000172Availability: Macintosh, \UNIX.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000173\end{funcdesc}
174
Fred Draked673d481999-02-03 22:31:30 +0000175\begin{funcdesc}{sameopenfile}{fp1, fp2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000176Return \code{True} if the file objects \var{fp1} and \var{fp2} refer to the
Fred Draked673d481999-02-03 22:31:30 +0000177same file. The two file objects may represent different file
178descriptors.
Fred Drakec37b65e2001-11-28 07:26:15 +0000179Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000180\end{funcdesc}
181
182\begin{funcdesc}{samestat}{stat1, stat2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000183Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer to
Fred Draked673d481999-02-03 22:31:30 +0000184the same file. These structures may have been returned by
185\function{fstat()}, \function{lstat()}, or \function{stat()}. This
186function implements the underlying comparison used by
187\function{samefile()} and \function{sameopenfile()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000188Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000189\end{funcdesc}
190
Fred Drakea9b9bf91999-02-02 18:58:33 +0000191\begin{funcdesc}{split}{path}
Fred Draked673d481999-02-03 22:31:30 +0000192Split the pathname \var{path} into a pair, \code{(\var{head},
193\var{tail})} where \var{tail} is the last pathname component and
Fred Drakea9b9bf91999-02-02 18:58:33 +0000194\var{head} is everything leading up to that. The \var{tail} part will
195never contain a slash; if \var{path} ends in a slash, \var{tail} will
196be empty. If there is no slash in \var{path}, \var{head} will be
197empty. If \var{path} is empty, both \var{head} and \var{tail} are
198empty. Trailing slashes are stripped from \var{head} unless it is the
199root (one or more slashes only). In nearly all cases,
200\code{join(\var{head}, \var{tail})} equals \var{path} (the only
201exception being when there were multiple slashes separating \var{head}
202from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\end{funcdesc}
204
Fred Drake0256c1f1999-02-03 19:24:44 +0000205\begin{funcdesc}{splitdrive}{path}
206Split the pathname \var{path} into a pair \code{(\var{drive},
Fred Draked673d481999-02-03 22:31:30 +0000207\var{tail})} where \var{drive} is either a drive specification or the
Fred Drake0256c1f1999-02-03 19:24:44 +0000208empty string. On systems which do not use drive specifications,
209\var{drive} will always be the empty string. In all cases,
210\code{\var{drive} + \var{tail}} will be the same as \var{path}.
Fred Drake56a71ee2001-05-25 16:21:00 +0000211\versionadded{1.3}
Fred Drake0256c1f1999-02-03 19:24:44 +0000212\end{funcdesc}
213
Fred Drakea9b9bf91999-02-02 18:58:33 +0000214\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000215Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000216such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000217and \var{ext} is empty or begins with a period and contains
218at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219\end{funcdesc}
220
Fred Drakea9b9bf91999-02-02 18:58:33 +0000221\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000222Calls the function \var{visit} with arguments
223\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000224directory tree rooted at \var{path} (including \var{path} itself, if it
225is a directory). The argument \var{dirname} specifies the visited
226directory, the argument \var{names} lists the files in the directory
227(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000228The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000229influence the set of directories visited below \var{dirname}, e.g., to
230avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000231\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000232assignment.)
Steve Holden545092b2002-08-06 16:07:07 +0000233
Fred Drake95fa4dd2002-08-07 12:39:33 +0000234\begin{notice}
235Symbolic links to directories are not treated as subdirectories, and
236that \function{walk()} therefore will not visit them. To visit linked
237directories you must identify them with
238\code{os.path.islink(\var{file})} and
239\code{os.path.isdir(\var{file})}, and invoke \function{walk()} as
240necessary.
241\end{notice}
Tim Petersc4e09402003-04-25 07:11:48 +0000242
Fred Drake9f480452003-04-25 15:12:47 +0000243\note{The newer \function{\refmodule{os}.walk()} generator supplies
244 similar functionality and can be easier to use.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245\end{funcdesc}
Fred Drake604ade42003-02-04 19:13:07 +0000246
247\begin{datadesc}{supports_unicode_filenames}
248True if arbitrary Unicode strings can be used as file names (within
249limitations imposed by the file system), and if
250\function{os.listdir()} returns Unicode strings for a Unicode
251argument.
252\versionadded{2.3}
253\end{datadesc}