blob: 190cf07dc143d4edcbc387d64bdf8e65337da4a0 [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.
Johannes Gijsbersae882f72004-08-30 10:19:56 +000046Returns \code{False} for broken symbolic links.
47\end{funcdesc}
48
49\begin{funcdesc}{lexists}{path}
50Return \code{True} if \var{path} refers to an existing path.
51Returns \code{True} for broken symbolic links.
52Equivalent to \function{exists()} on platforms lacking
53\function{os.lstat()}.
54\versionadded{2.4}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055\end{funcdesc}
56
Fred Drakea9b9bf91999-02-02 18:58:33 +000057\begin{funcdesc}{expanduser}{path}
Georg Brandl6f2bbd32005-08-24 07:26:55 +000058On \UNIX, return the argument with an initial component of \samp{\~} or
59\samp{\~\var{user}} replaced by that \var{user}'s home directory.
60An initial \samp{\~} is replaced by the environment variable
61\envvar{HOME} if it is set; otherwise the current user's home directory
62is looked up in the password directory through the built-in module
63\refmodule{pwd}\refbimodindex{pwd}.
64An initial \samp{\~\var{user}} is looked up directly in the
65password directory.
66
67On Windows, only \samp{\~} is supported; it is replaced by the
68environment variable \envvar{HOME} or by a combination of
69\envvar{HOMEDRIVE} and \envvar{HOMEPATH}.
70
71If the expansion fails or if the
Brett Cannon19021462004-12-22 05:40:45 +000072path does not begin with a tilde, the path is returned unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073\end{funcdesc}
74
Fred Drakea9b9bf91999-02-02 18:58:33 +000075\begin{funcdesc}{expandvars}{path}
Guido van Rossum17383111994-04-21 10:32:28 +000076Return the argument with environment variables expanded. Substrings
77of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
78replaced by the value of environment variable \var{name}. Malformed
79variable names and references to non-existing variables are left
Fred Drakea9b9bf91999-02-02 18:58:33 +000080unchanged.
Guido van Rossum17383111994-04-21 10:32:28 +000081\end{funcdesc}
82
Fred Draked8a41e61999-02-19 17:54:10 +000083\begin{funcdesc}{getatime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000084Return the time of last access of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000085value is a number giving the number of seconds since the epoch (see the
Fred Draked8a41e61999-02-19 17:54:10 +000086\refmodule{time} module). Raise \exception{os.error} if the file does
87not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000088\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +000089\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 +000090\end{funcdesc}
91
Fred Drakea9b9bf91999-02-02 18:58:33 +000092\begin{funcdesc}{getmtime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000093Return the time of last modification of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000094value is a number giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +000095\refmodule{time} module). Raise \exception{os.error} if the file does
96not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000097\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +000098\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
99\end{funcdesc}
100
101\begin{funcdesc}{getctime}{path}
Raymond Hettingerc43a7e72003-10-29 00:46:19 +0000102Return the system's ctime which, on some systems (like \UNIX) is the
103time of the last change, and, on others (like Windows), is the
104creation time for \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +0000105value is a number giving the number of seconds since the epoch (see the
106\refmodule{time} module). Raise \exception{os.error} if the file does
107not exist or is inaccessible.
108\versionadded{2.3}
Guido van Rossum2babd7b1998-07-24 20:49:39 +0000109\end{funcdesc}
110
Fred Draked8a41e61999-02-19 17:54:10 +0000111\begin{funcdesc}{getsize}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +0000112Return the size, in bytes, of \var{path}. Raise
Fred Draked8a41e61999-02-19 17:54:10 +0000113\exception{os.error} if the file does not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +0000114\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +0000115\end{funcdesc}
116
Fred Drakea9b9bf91999-02-02 18:58:33 +0000117\begin{funcdesc}{isabs}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000118Return \code{True} if \var{path} is an absolute pathname (begins with a
Fred Drakea9b9bf91999-02-02 18:58:33 +0000119slash).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
Fred Drakea9b9bf91999-02-02 18:58:33 +0000122\begin{funcdesc}{isfile}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000123Return \code{True} if \var{path} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000124symbolic links, so both \function{islink()} and \function{isfile()}
125can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126\end{funcdesc}
127
Fred Drakea9b9bf91999-02-02 18:58:33 +0000128\begin{funcdesc}{isdir}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000129Return \code{True} if \var{path} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000130symbolic links, so both \function{islink()} and \function{isdir()} can
131be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
Fred Drakea9b9bf91999-02-02 18:58:33 +0000134\begin{funcdesc}{islink}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000135Return \code{True} if \var{path} refers to a directory entry that is a
136symbolic link. Always \code{False} if symbolic links are not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000137\end{funcdesc}
138
Fred Drakea9b9bf91999-02-02 18:58:33 +0000139\begin{funcdesc}{ismount}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000140Return \code{True} if pathname \var{path} is a \dfn{mount point}: a point in
Fred Drakea9b9bf91999-02-02 18:58:33 +0000141a file system where a different file system has been mounted. The
142function checks whether \var{path}'s parent, \file{\var{path}/..}, is
143on a different device than \var{path}, or whether \file{\var{path}/..}
144and \var{path} point to the same i-node on the same device --- this
145should detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146\end{funcdesc}
147
Fred Drakea9b9bf91999-02-02 18:58:33 +0000148\begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000149Joins one or more path components intelligently. If any component is
150an absolute path, all previous components are thrown away, and joining
Fred Drakea9b9bf91999-02-02 18:58:33 +0000151continues. The return value is the concatenation of \var{path1}, and
Fred Drakec1ddc502002-09-12 18:01:26 +0000152optionally \var{path2}, etc., with exactly one directory separator
Martin v. Löwis04791042002-12-11 12:55:53 +0000153(\code{os.sep}) inserted between components, unless \var{path2} is
Fred Drakec1ddc502002-09-12 18:01:26 +0000154empty. Note that on Windows, since there is a current directory for
155each drive, \function{os.path.join("c:", "foo")} represents a path
156relative to the current directory on drive \file{C:} (\file{c:foo}), not
157\file{c:\textbackslash\textbackslash foo}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158\end{funcdesc}
159
Fred Drakea9b9bf91999-02-02 18:58:33 +0000160\begin{funcdesc}{normcase}{path}
Fred Drakec37b65e2001-11-28 07:26:15 +0000161Normalize the case of a pathname. On \UNIX, this returns the path
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000162unchanged; on case-insensitive filesystems, it converts the path to
163lowercase. On Windows, it also converts forward slashes to backward
164slashes.
165\end{funcdesc}
166
Fred Drakea9b9bf91999-02-02 18:58:33 +0000167\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000168Normalize a pathname. This collapses redundant separators and
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000169up-level references so that \code{A//B}, \code{A/./B} and
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000170\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drake38e5d272000-04-03 20:13:55 +0000171case (use \function{normcase()} for that). On Windows, it converts
Johannes Gijsbersb112d6e2004-08-14 14:41:32 +0000172forward slashes to backward slashes. It should be understood that this may
173change the meaning of the path if it contains symbolic links!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000174\end{funcdesc}
175
Guido van Rossum83eeef42001-09-17 15:16:09 +0000176\begin{funcdesc}{realpath}{path}
177Return the canonical path of the specified filename, eliminating any
178symbolic links encountered in the path.
Fred Drakec37b65e2001-11-28 07:26:15 +0000179Availability: \UNIX.
Guido van Rossum83eeef42001-09-17 15:16:09 +0000180\versionadded{2.2}
181\end{funcdesc}
182
Fred Drakea9b9bf91999-02-02 18:58:33 +0000183\begin{funcdesc}{samefile}{path1, path2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000184Return \code{True} if both pathname arguments refer to the same file or
Fred Drakedb9693e1998-03-11 05:50:42 +0000185directory (as indicated by device number and i-node number).
186Raise an exception if a \function{os.stat()} call on either pathname
187fails.
Fred Drakec37b65e2001-11-28 07:26:15 +0000188Availability: Macintosh, \UNIX.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000189\end{funcdesc}
190
Fred Draked673d481999-02-03 22:31:30 +0000191\begin{funcdesc}{sameopenfile}{fp1, fp2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000192Return \code{True} if the file objects \var{fp1} and \var{fp2} refer to the
Fred Draked673d481999-02-03 22:31:30 +0000193same file. The two file objects may represent different file
194descriptors.
Fred Drakec37b65e2001-11-28 07:26:15 +0000195Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000196\end{funcdesc}
197
198\begin{funcdesc}{samestat}{stat1, stat2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000199Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer to
Fred Draked673d481999-02-03 22:31:30 +0000200the same file. These structures may have been returned by
201\function{fstat()}, \function{lstat()}, or \function{stat()}. This
202function implements the underlying comparison used by
203\function{samefile()} and \function{sameopenfile()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000204Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000205\end{funcdesc}
206
Fred Drakea9b9bf91999-02-02 18:58:33 +0000207\begin{funcdesc}{split}{path}
Fred Draked673d481999-02-03 22:31:30 +0000208Split the pathname \var{path} into a pair, \code{(\var{head},
209\var{tail})} where \var{tail} is the last pathname component and
Fred Drakea9b9bf91999-02-02 18:58:33 +0000210\var{head} is everything leading up to that. The \var{tail} part will
211never contain a slash; if \var{path} ends in a slash, \var{tail} will
212be empty. If there is no slash in \var{path}, \var{head} will be
213empty. If \var{path} is empty, both \var{head} and \var{tail} are
214empty. Trailing slashes are stripped from \var{head} unless it is the
215root (one or more slashes only). In nearly all cases,
216\code{join(\var{head}, \var{tail})} equals \var{path} (the only
217exception being when there were multiple slashes separating \var{head}
218from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219\end{funcdesc}
220
Fred Drake0256c1f1999-02-03 19:24:44 +0000221\begin{funcdesc}{splitdrive}{path}
222Split the pathname \var{path} into a pair \code{(\var{drive},
Fred Draked673d481999-02-03 22:31:30 +0000223\var{tail})} where \var{drive} is either a drive specification or the
Fred Drake0256c1f1999-02-03 19:24:44 +0000224empty string. On systems which do not use drive specifications,
225\var{drive} will always be the empty string. In all cases,
226\code{\var{drive} + \var{tail}} will be the same as \var{path}.
Fred Drake56a71ee2001-05-25 16:21:00 +0000227\versionadded{1.3}
Fred Drake0256c1f1999-02-03 19:24:44 +0000228\end{funcdesc}
229
Fred Drakea9b9bf91999-02-02 18:58:33 +0000230\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000231Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000232such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000233and \var{ext} is empty or begins with a period and contains
234at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000235\end{funcdesc}
236
Georg Brandl32252422005-09-14 20:42:00 +0000237\begin{funcdesc}{splitunc}{path}
238Split the pathname \var{path} into a pair \code{(\var{unc}, \var{rest})}
239so that \var{unc} is the UNC mount point (such as \code{r'\e\e host\e mount'}),
240if present, and \var{rest} the rest of the path (such as
241\code{r'\e path\e file.ext'}). For paths containing drive letters, \var{unc}
242will always be the empty string.
243Availability: Windows.
244\end{funcdesc}
245
Fred Drakea9b9bf91999-02-02 18:58:33 +0000246\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000247Calls the function \var{visit} with arguments
248\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000249directory tree rooted at \var{path} (including \var{path} itself, if it
250is a directory). The argument \var{dirname} specifies the visited
251directory, the argument \var{names} lists the files in the directory
252(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000253The \var{visit} function may modify \var{names} to
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000254influence the set of directories visited below \var{dirname}, e.g. to
Guido van Rossum470be141995-03-17 16:07:09 +0000255avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000256\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000257assignment.)
Steve Holden545092b2002-08-06 16:07:07 +0000258
Fred Drake95fa4dd2002-08-07 12:39:33 +0000259\begin{notice}
260Symbolic links to directories are not treated as subdirectories, and
261that \function{walk()} therefore will not visit them. To visit linked
262directories you must identify them with
263\code{os.path.islink(\var{file})} and
264\code{os.path.isdir(\var{file})}, and invoke \function{walk()} as
265necessary.
266\end{notice}
Tim Petersc4e09402003-04-25 07:11:48 +0000267
Fred Drake9f480452003-04-25 15:12:47 +0000268\note{The newer \function{\refmodule{os}.walk()} generator supplies
269 similar functionality and can be easier to use.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000270\end{funcdesc}
Fred Drake604ade42003-02-04 19:13:07 +0000271
272\begin{datadesc}{supports_unicode_filenames}
273True if arbitrary Unicode strings can be used as file names (within
274limitations imposed by the file system), and if
275\function{os.listdir()} returns Unicode strings for a Unicode
276argument.
277\versionadded{2.3}
278\end{datadesc}