blob: 7684fa0587f877ed9490801cf7f45154db4adcc6 [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}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045Return \code{True} if \var{path} refers to an existing path. Returns
46\code{False} for broken symbolic links. On some platforms, this
47function may return \code{False} if permission is not granted to
48execute \function{os.stat()} on the requested file, even if the
49\var{path} physically exists.
Johannes Gijsbersae882f72004-08-30 10:19:56 +000050\end{funcdesc}
51
52\begin{funcdesc}{lexists}{path}
53Return \code{True} if \var{path} refers to an existing path.
54Returns \code{True} for broken symbolic links.
55Equivalent to \function{exists()} on platforms lacking
56\function{os.lstat()}.
57\versionadded{2.4}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{funcdesc}
59
Fred Drakea9b9bf91999-02-02 18:58:33 +000060\begin{funcdesc}{expanduser}{path}
Guido van Rossumd8faa362007-04-27 19:54:29 +000061On \UNIX{} and Windows, return the argument with an initial component of
62\samp{\~} or \samp{\~\var{user}} replaced by that \var{user}'s home directory.
63
64On \UNIX, an initial \samp{\~} is replaced by the environment variable
Georg Brandl6f2bbd32005-08-24 07:26:55 +000065\envvar{HOME} if it is set; otherwise the current user's home directory
66is looked up in the password directory through the built-in module
67\refmodule{pwd}\refbimodindex{pwd}.
68An initial \samp{\~\var{user}} is looked up directly in the
69password directory.
70
Guido van Rossumd8faa362007-04-27 19:54:29 +000071On Windows, \envvar{HOME} and \envvar{USERPROFILE} will be used if set,
72otherwise a combination of \envvar{HOMEPATH} and \envvar{HOMEDRIVE} will be
73used. An initial \samp{\~\var{user}} is handled by stripping the last
74directory component from the created user path derived above.
Georg Brandl6f2bbd32005-08-24 07:26:55 +000075
76If the expansion fails or if the
Brett Cannon19021462004-12-22 05:40:45 +000077path does not begin with a tilde, the path is returned unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\end{funcdesc}
79
Fred Drakea9b9bf91999-02-02 18:58:33 +000080\begin{funcdesc}{expandvars}{path}
Guido van Rossum17383111994-04-21 10:32:28 +000081Return the argument with environment variables expanded. Substrings
82of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
83replaced by the value of environment variable \var{name}. Malformed
84variable names and references to non-existing variables are left
Fred Drakea9b9bf91999-02-02 18:58:33 +000085unchanged.
Guido van Rossumd8faa362007-04-27 19:54:29 +000086
87On Windows, \samp{\%\var{name}\%} expansions are supported in addition to
88\samp{\$\var{name}} and \samp{\$\{\var{name}\}}.
Guido van Rossum17383111994-04-21 10:32:28 +000089\end{funcdesc}
90
Fred Draked8a41e61999-02-19 17:54:10 +000091\begin{funcdesc}{getatime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +000092Return the time of last access of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +000093value is a number giving the number of seconds since the epoch (see the
Fred Draked8a41e61999-02-19 17:54:10 +000094\refmodule{time} module). Raise \exception{os.error} if the file does
95not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000096\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +000097\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 +000098\end{funcdesc}
99
Fred Drakea9b9bf91999-02-02 18:58:33 +0000100\begin{funcdesc}{getmtime}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +0000101Return the time of last modification of \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +0000102value is a number giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +0000103\refmodule{time} module). Raise \exception{os.error} if the file does
104not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +0000105\versionadded{1.5.2}
Martin v. Löwis96a60e42002-12-31 13:11:54 +0000106\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
107\end{funcdesc}
108
109\begin{funcdesc}{getctime}{path}
Raymond Hettingerc43a7e72003-10-29 00:46:19 +0000110Return the system's ctime which, on some systems (like \UNIX) is the
111time of the last change, and, on others (like Windows), is the
112creation time for \var{path}. The return
Martin v. Löwis96a60e42002-12-31 13:11:54 +0000113value is a number giving the number of seconds since the epoch (see the
114\refmodule{time} module). Raise \exception{os.error} if the file does
115not exist or is inaccessible.
116\versionadded{2.3}
Guido van Rossum2babd7b1998-07-24 20:49:39 +0000117\end{funcdesc}
118
Fred Draked8a41e61999-02-19 17:54:10 +0000119\begin{funcdesc}{getsize}{path}
Neal Norwitzf3edea52002-12-31 13:38:28 +0000120Return the size, in bytes, of \var{path}. Raise
Fred Draked8a41e61999-02-19 17:54:10 +0000121\exception{os.error} if the file does not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +0000122\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +0000123\end{funcdesc}
124
Fred Drakea9b9bf91999-02-02 18:58:33 +0000125\begin{funcdesc}{isabs}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000126Return \code{True} if \var{path} is an absolute pathname (begins with a
Fred Drakea9b9bf91999-02-02 18:58:33 +0000127slash).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128\end{funcdesc}
129
Fred Drakea9b9bf91999-02-02 18:58:33 +0000130\begin{funcdesc}{isfile}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000131Return \code{True} if \var{path} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000132symbolic links, so both \function{islink()} and \function{isfile()}
133can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{funcdesc}
135
Fred Drakea9b9bf91999-02-02 18:58:33 +0000136\begin{funcdesc}{isdir}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000137Return \code{True} if \var{path} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +0000138symbolic links, so both \function{islink()} and \function{isdir()} can
139be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140\end{funcdesc}
141
Fred Drakea9b9bf91999-02-02 18:58:33 +0000142\begin{funcdesc}{islink}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000143Return \code{True} if \var{path} refers to a directory entry that is a
144symbolic link. Always \code{False} if symbolic links are not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000145\end{funcdesc}
146
Fred Drakea9b9bf91999-02-02 18:58:33 +0000147\begin{funcdesc}{ismount}{path}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000148Return \code{True} if pathname \var{path} is a \dfn{mount point}: a point in
Fred Drakea9b9bf91999-02-02 18:58:33 +0000149a file system where a different file system has been mounted. The
150function checks whether \var{path}'s parent, \file{\var{path}/..}, is
151on a different device than \var{path}, or whether \file{\var{path}/..}
152and \var{path} point to the same i-node on the same device --- this
153should detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154\end{funcdesc}
155
Fred Drakea9b9bf91999-02-02 18:58:33 +0000156\begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157Join one or more path components intelligently. If any component is
158an absolute path, all previous components (on Windows, including the
159previous drive letter, if there was one) are thrown away, and joining
Fred Drakea9b9bf91999-02-02 18:58:33 +0000160continues. The return value is the concatenation of \var{path1}, and
Fred Drakec1ddc502002-09-12 18:01:26 +0000161optionally \var{path2}, etc., with exactly one directory separator
Martin v. Löwis04791042002-12-11 12:55:53 +0000162(\code{os.sep}) inserted between components, unless \var{path2} is
Fred Drakec1ddc502002-09-12 18:01:26 +0000163empty. Note that on Windows, since there is a current directory for
164each drive, \function{os.path.join("c:", "foo")} represents a path
165relative to the current directory on drive \file{C:} (\file{c:foo}), not
166\file{c:\textbackslash\textbackslash foo}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000167\end{funcdesc}
168
Fred Drakea9b9bf91999-02-02 18:58:33 +0000169\begin{funcdesc}{normcase}{path}
Fred Drakec37b65e2001-11-28 07:26:15 +0000170Normalize the case of a pathname. On \UNIX, this returns the path
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000171unchanged; on case-insensitive filesystems, it converts the path to
172lowercase. On Windows, it also converts forward slashes to backward
173slashes.
174\end{funcdesc}
175
Fred Drakea9b9bf91999-02-02 18:58:33 +0000176\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000177Normalize a pathname. This collapses redundant separators and
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000178up-level references so that \code{A//B}, \code{A/./B} and
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000179\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drake38e5d272000-04-03 20:13:55 +0000180case (use \function{normcase()} for that). On Windows, it converts
Johannes Gijsbersb112d6e2004-08-14 14:41:32 +0000181forward slashes to backward slashes. It should be understood that this may
182change the meaning of the path if it contains symbolic links!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000183\end{funcdesc}
184
Guido van Rossum83eeef42001-09-17 15:16:09 +0000185\begin{funcdesc}{realpath}{path}
186Return the canonical path of the specified filename, eliminating any
Georg Brandlddb73fe2006-03-08 20:59:09 +0000187symbolic links encountered in the path (if they are supported by the
188operating system).
Guido van Rossum83eeef42001-09-17 15:16:09 +0000189\versionadded{2.2}
190\end{funcdesc}
191
Guido van Rossumd8faa362007-04-27 19:54:29 +0000192\begin{funcdesc}{relpath}{path\optional{, start}}
193Return a relative filepath to \var{path} either from the current
194directory or from an optional \var{start} point.
195
196\var{start} defaults to \member{os.curdir}.
197Availability: Windows, \UNIX.
198\versionadded{2.6}
199\end{funcdesc}
200
Fred Drakea9b9bf91999-02-02 18:58:33 +0000201\begin{funcdesc}{samefile}{path1, path2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000202Return \code{True} if both pathname arguments refer to the same file or
Fred Drakedb9693e1998-03-11 05:50:42 +0000203directory (as indicated by device number and i-node number).
204Raise an exception if a \function{os.stat()} call on either pathname
205fails.
Fred Drakec37b65e2001-11-28 07:26:15 +0000206Availability: Macintosh, \UNIX.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207\end{funcdesc}
208
Fred Draked673d481999-02-03 22:31:30 +0000209\begin{funcdesc}{sameopenfile}{fp1, fp2}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210Return \code{True} if the file descriptors \var{fp1} and \var{fp2} refer
211to the same file.
Fred Drakec37b65e2001-11-28 07:26:15 +0000212Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000213\end{funcdesc}
214
215\begin{funcdesc}{samestat}{stat1, stat2}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000216Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer to
Fred Draked673d481999-02-03 22:31:30 +0000217the same file. These structures may have been returned by
218\function{fstat()}, \function{lstat()}, or \function{stat()}. This
219function implements the underlying comparison used by
220\function{samefile()} and \function{sameopenfile()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000221Availability: Macintosh, \UNIX.
Fred Draked673d481999-02-03 22:31:30 +0000222\end{funcdesc}
223
Fred Drakea9b9bf91999-02-02 18:58:33 +0000224\begin{funcdesc}{split}{path}
Fred Draked673d481999-02-03 22:31:30 +0000225Split the pathname \var{path} into a pair, \code{(\var{head},
226\var{tail})} where \var{tail} is the last pathname component and
Fred Drakea9b9bf91999-02-02 18:58:33 +0000227\var{head} is everything leading up to that. The \var{tail} part will
228never contain a slash; if \var{path} ends in a slash, \var{tail} will
229be empty. If there is no slash in \var{path}, \var{head} will be
230empty. If \var{path} is empty, both \var{head} and \var{tail} are
231empty. Trailing slashes are stripped from \var{head} unless it is the
232root (one or more slashes only). In nearly all cases,
233\code{join(\var{head}, \var{tail})} equals \var{path} (the only
234exception being when there were multiple slashes separating \var{head}
235from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236\end{funcdesc}
237
Fred Drake0256c1f1999-02-03 19:24:44 +0000238\begin{funcdesc}{splitdrive}{path}
239Split the pathname \var{path} into a pair \code{(\var{drive},
Fred Draked673d481999-02-03 22:31:30 +0000240\var{tail})} where \var{drive} is either a drive specification or the
Fred Drake0256c1f1999-02-03 19:24:44 +0000241empty string. On systems which do not use drive specifications,
242\var{drive} will always be the empty string. In all cases,
243\code{\var{drive} + \var{tail}} will be the same as \var{path}.
Fred Drake56a71ee2001-05-25 16:21:00 +0000244\versionadded{1.3}
Fred Drake0256c1f1999-02-03 19:24:44 +0000245\end{funcdesc}
246
Fred Drakea9b9bf91999-02-02 18:58:33 +0000247\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000248Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000249such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000250and \var{ext} is empty or begins with a period and contains
Guido van Rossumd8faa362007-04-27 19:54:29 +0000251at most one period. Leading periods on the basename are
252ignored; \code{\var{splitext}.('.cshrc')} returns
253\code{('.cshrc', '')}.
254
255\versionchanged[Earlier versions could produce an empty root when
256the only period was the first character]{2.6}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000257\end{funcdesc}
258
Georg Brandl32252422005-09-14 20:42:00 +0000259\begin{funcdesc}{splitunc}{path}
260Split the pathname \var{path} into a pair \code{(\var{unc}, \var{rest})}
261so that \var{unc} is the UNC mount point (such as \code{r'\e\e host\e mount'}),
262if present, and \var{rest} the rest of the path (such as
263\code{r'\e path\e file.ext'}). For paths containing drive letters, \var{unc}
264will always be the empty string.
265Availability: Windows.
266\end{funcdesc}
267
Fred Drakea9b9bf91999-02-02 18:58:33 +0000268\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000269Calls the function \var{visit} with arguments
270\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000271directory tree rooted at \var{path} (including \var{path} itself, if it
272is a directory). The argument \var{dirname} specifies the visited
273directory, the argument \var{names} lists the files in the directory
274(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000275The \var{visit} function may modify \var{names} to
Georg Brandl6f2bbd32005-08-24 07:26:55 +0000276influence the set of directories visited below \var{dirname}, e.g. to
Guido van Rossum470be141995-03-17 16:07:09 +0000277avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000278\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000279assignment.)
Steve Holden545092b2002-08-06 16:07:07 +0000280
Fred Drake95fa4dd2002-08-07 12:39:33 +0000281\begin{notice}
282Symbolic links to directories are not treated as subdirectories, and
283that \function{walk()} therefore will not visit them. To visit linked
284directories you must identify them with
285\code{os.path.islink(\var{file})} and
286\code{os.path.isdir(\var{file})}, and invoke \function{walk()} as
287necessary.
288\end{notice}
Tim Petersc4e09402003-04-25 07:11:48 +0000289
Fred Drake9f480452003-04-25 15:12:47 +0000290\note{The newer \function{\refmodule{os}.walk()} generator supplies
291 similar functionality and can be easier to use.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000292\end{funcdesc}
Fred Drake604ade42003-02-04 19:13:07 +0000293
294\begin{datadesc}{supports_unicode_filenames}
295True if arbitrary Unicode strings can be used as file names (within
296limitations imposed by the file system), and if
297\function{os.listdir()} returns Unicode strings for a Unicode
298argument.
299\versionadded{2.3}
300\end{datadesc}