blob: 9f0de1f14f594b2afee0d70b20eb8afc32d31dbd [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{, ...}}}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149Join one or more path components intelligently. If any component is
150an absolute path, all previous components (on Windows, including the
151previous drive letter, if there was one) are thrown away, and joining
Fred Drakea9b9bf91999-02-02 18:58:33 +0000152continues. The return value is the concatenation of \var{path1}, and
Fred Drakec1ddc502002-09-12 18:01:26 +0000153optionally \var{path2}, etc., with exactly one directory separator
Martin v. Löwis04791042002-12-11 12:55:53 +0000154(\code{os.sep}) inserted between components, unless \var{path2} is
Fred Drakec1ddc502002-09-12 18:01:26 +0000155empty. Note that on Windows, since there is a current directory for
156each drive, \function{os.path.join("c:", "foo")} represents a path
157relative to the current directory on drive \file{C:} (\file{c:foo}), not
158\file{c:\textbackslash\textbackslash foo}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000159\end{funcdesc}
160
Fred Drakea9b9bf91999-02-02 18:58:33 +0000161\begin{funcdesc}{normcase}{path}
Fred Drakec37b65e2001-11-28 07:26:15 +0000162Normalize the case of a pathname. On \UNIX, this returns the path
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000163unchanged; on case-insensitive filesystems, it converts the path to
164lowercase. On Windows, it also converts forward slashes to backward
165slashes.
166\end{funcdesc}
167
Fred Drakea9b9bf91999-02-02 18:58:33 +0000168\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000169Normalize a pathname. This collapses redundant separators and
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000170up-level references so that \code{A//B}, \code{A/./B} and
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000171\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drake38e5d272000-04-03 20:13:55 +0000172case (use \function{normcase()} for that). On Windows, it converts
Johannes Gijsbersb112d6e2004-08-14 14:41:32 +0000173forward slashes to backward slashes. It should be understood that this may
174change the meaning of the path if it contains symbolic links!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{funcdesc}
176
Guido van Rossum83eeef42001-09-17 15:16:09 +0000177\begin{funcdesc}{realpath}{path}
178Return the canonical path of the specified filename, eliminating any
Georg Brandlddb73fe2006-03-08 20:59:09 +0000179symbolic links encountered in the path (if they are supported by the
180operating system).
Guido van Rossum83eeef42001-09-17 15:16:09 +0000181\versionadded{2.2}
182\end{funcdesc}
183
Fred Drakea9b9bf91999-02-02 18:58:33 +0000184\begin{funcdesc}{samefile}{path1, path2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000185Return \code{True} if both pathname arguments refer to the same file or
Fred Drakedb9693e1998-03-11 05:50:42 +0000186directory (as indicated by device number and i-node number).
187Raise an exception if a \function{os.stat()} call on either pathname
188fails.
Fred Drakec37b65e2001-11-28 07:26:15 +0000189Availability: Macintosh, \UNIX.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000190\end{funcdesc}
191
Fred Draked673d481999-02-03 22:31:30 +0000192\begin{funcdesc}{sameopenfile}{fp1, fp2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000193Return \code{True} if the file objects \var{fp1} and \var{fp2} refer to the
Fred Draked673d481999-02-03 22:31:30 +0000194same file. The two file objects may represent different file
195descriptors.
Fred Drakec37b65e2001-11-28 07:26:15 +0000196Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000197\end{funcdesc}
198
199\begin{funcdesc}{samestat}{stat1, stat2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000200Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer to
Fred Draked673d481999-02-03 22:31:30 +0000201the same file. These structures may have been returned by
202\function{fstat()}, \function{lstat()}, or \function{stat()}. This
203function implements the underlying comparison used by
204\function{samefile()} and \function{sameopenfile()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000205Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000206\end{funcdesc}
207
Fred Drakea9b9bf91999-02-02 18:58:33 +0000208\begin{funcdesc}{split}{path}
Fred Draked673d481999-02-03 22:31:30 +0000209Split the pathname \var{path} into a pair, \code{(\var{head},
210\var{tail})} where \var{tail} is the last pathname component and
Fred Drakea9b9bf91999-02-02 18:58:33 +0000211\var{head} is everything leading up to that. The \var{tail} part will
212never contain a slash; if \var{path} ends in a slash, \var{tail} will
213be empty. If there is no slash in \var{path}, \var{head} will be
214empty. If \var{path} is empty, both \var{head} and \var{tail} are
215empty. Trailing slashes are stripped from \var{head} unless it is the
216root (one or more slashes only). In nearly all cases,
217\code{join(\var{head}, \var{tail})} equals \var{path} (the only
218exception being when there were multiple slashes separating \var{head}
219from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000220\end{funcdesc}
221
Fred Drake0256c1f1999-02-03 19:24:44 +0000222\begin{funcdesc}{splitdrive}{path}
223Split the pathname \var{path} into a pair \code{(\var{drive},
Fred Draked673d481999-02-03 22:31:30 +0000224\var{tail})} where \var{drive} is either a drive specification or the
Fred Drake0256c1f1999-02-03 19:24:44 +0000225empty string. On systems which do not use drive specifications,
226\var{drive} will always be the empty string. In all cases,
227\code{\var{drive} + \var{tail}} will be the same as \var{path}.
Fred Drake56a71ee2001-05-25 16:21:00 +0000228\versionadded{1.3}
Fred Drake0256c1f1999-02-03 19:24:44 +0000229\end{funcdesc}
230
Fred Drakea9b9bf91999-02-02 18:58:33 +0000231\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000232Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000233such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000234and \var{ext} is empty or begins with a period and contains
235at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236\end{funcdesc}
237
Georg Brandl32252422005-09-14 20:42:00 +0000238\begin{funcdesc}{splitunc}{path}
239Split the pathname \var{path} into a pair \code{(\var{unc}, \var{rest})}
240so that \var{unc} is the UNC mount point (such as \code{r'\e\e host\e mount'}),
241if present, and \var{rest} the rest of the path (such as
242\code{r'\e path\e file.ext'}). For paths containing drive letters, \var{unc}
243will always be the empty string.
244Availability: Windows.
245\end{funcdesc}
246
Fred Drakea9b9bf91999-02-02 18:58:33 +0000247\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000248Calls the function \var{visit} with arguments
249\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000250directory tree rooted at \var{path} (including \var{path} itself, if it
251is a directory). The argument \var{dirname} specifies the visited
252directory, the argument \var{names} lists the files in the directory
253(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000254The \var{visit} function may modify \var{names} to
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000255influence the set of directories visited below \var{dirname}, e.g. to
Guido van Rossum470be141995-03-17 16:07:09 +0000256avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000257\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000258assignment.)
Steve Holden545092b2002-08-06 16:07:07 +0000259
Fred Drake95fa4dd2002-08-07 12:39:33 +0000260\begin{notice}
261Symbolic links to directories are not treated as subdirectories, and
262that \function{walk()} therefore will not visit them. To visit linked
263directories you must identify them with
264\code{os.path.islink(\var{file})} and
265\code{os.path.isdir(\var{file})}, and invoke \function{walk()} as
266necessary.
267\end{notice}
Tim Petersc4e09402003-04-25 07:11:48 +0000268
Fred Drake9f480452003-04-25 15:12:47 +0000269\note{The newer \function{\refmodule{os}.walk()} generator supplies
270 similar functionality and can be easier to use.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000271\end{funcdesc}
Fred Drake604ade42003-02-04 19:13:07 +0000272
273\begin{datadesc}{supports_unicode_filenames}
274True if arbitrary Unicode strings can be used as file names (within
275limitations imposed by the file system), and if
276\function{os.listdir()} returns Unicode strings for a Unicode
277argument.
278\versionadded{2.3}
279\end{datadesc}