blob: 4981ac0cad906a83e450462f4905b25a6fb75596 [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
Fred Drake39d4a021999-10-18 14:10:06 +000014\code{normpath(join(os.getcwd(), \var{path}))}.
Fred Drake154d9091999-03-17 22:25:11 +000015\versionadded{1.5.2}
Guido van Rossum1804dc31999-01-29 18:05:05 +000016\end{funcdesc}
17
Fred Drakea9b9bf91999-02-02 18:58:33 +000018\begin{funcdesc}{basename}{path}
19Return the base name of pathname \var{path}. This is the second half
20of the pair returned by \code{split(\var{path})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021\end{funcdesc}
22
23\begin{funcdesc}{commonprefix}{list}
24Return the longest string that is a prefix of all strings in
Fred Drakeb23ee1d1999-02-01 20:20:39 +000025\var{list}. If \var{list} is empty, return the empty string
26(\code{''}).
27\end{funcdesc}
28
Fred Drakea9b9bf91999-02-02 18:58:33 +000029\begin{funcdesc}{dirname}{path}
30Return the directory name of pathname \var{path}. This is the first
31half of the pair returned by \code{split(\var{path})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\end{funcdesc}
33
Fred Drakea9b9bf91999-02-02 18:58:33 +000034\begin{funcdesc}{exists}{path}
35Return true if \var{path} refers to an existing path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\end{funcdesc}
37
Fred Drakea9b9bf91999-02-02 18:58:33 +000038\begin{funcdesc}{expanduser}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039Return the argument with an initial component of \samp{\~} or
40\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
Fred Drake203b4f11998-05-14 15:16:12 +000041initial \samp{\~{}} is replaced by the environment variable
Fred Drake23a16341998-08-06 15:33:55 +000042\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
43password directory through the built-in module
Fred Drakeb23ee1d1999-02-01 20:20:39 +000044\refmodule{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
45path does not begin with a tilde, the path is returned unchanged. On
Fred Drakea9b9bf91999-02-02 18:58:33 +000046the Macintosh, this always returns \var{path} unchanged.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047\end{funcdesc}
48
Fred Drakea9b9bf91999-02-02 18:58:33 +000049\begin{funcdesc}{expandvars}{path}
Guido van Rossum17383111994-04-21 10:32:28 +000050Return the argument with environment variables expanded. Substrings
51of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
52replaced by the value of environment variable \var{name}. Malformed
53variable names and references to non-existing variables are left
Fred Drakea9b9bf91999-02-02 18:58:33 +000054unchanged. On the Macintosh, this always returns \var{path}
55unchanged.
Guido van Rossum17383111994-04-21 10:32:28 +000056\end{funcdesc}
57
Fred Draked8a41e61999-02-19 17:54:10 +000058\begin{funcdesc}{getatime}{path}
59Return the time of last access of \var{filename}. The return
60value is integer giving the number of seconds since the epoch (see the
61\refmodule{time} module). Raise \exception{os.error} if the file does
62not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000063\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000064\end{funcdesc}
65
Fred Drakea9b9bf91999-02-02 18:58:33 +000066\begin{funcdesc}{getmtime}{path}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000067Return the time of last modification of \var{filename}. The return
68value is integer giving the number of seconds since the epoch (see the
Fred Drakeb23ee1d1999-02-01 20:20:39 +000069\refmodule{time} module). Raise \exception{os.error} if the file does
70not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000071\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000072\end{funcdesc}
73
Fred Draked8a41e61999-02-19 17:54:10 +000074\begin{funcdesc}{getsize}{path}
75Return the size, in bytes, of \var{filename}. Raise
76\exception{os.error} if the file does not exist or is inaccessible.
Fred Drakea9b9bf91999-02-02 18:58:33 +000077\versionadded{1.5.2}
Guido van Rossum2babd7b1998-07-24 20:49:39 +000078\end{funcdesc}
79
Fred Drakea9b9bf91999-02-02 18:58:33 +000080\begin{funcdesc}{isabs}{path}
81Return true if \var{path} is an absolute pathname (begins with a
82slash).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000083\end{funcdesc}
84
Fred Drakea9b9bf91999-02-02 18:58:33 +000085\begin{funcdesc}{isfile}{path}
86Return true if \var{path} is an existing regular file. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000087symbolic links, so both \function{islink()} and \function{isfile()}
88can be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\end{funcdesc}
90
Fred Drakea9b9bf91999-02-02 18:58:33 +000091\begin{funcdesc}{isdir}{path}
92Return true if \var{path} is an existing directory. This follows
Fred Drakedb9693e1998-03-11 05:50:42 +000093symbolic links, so both \function{islink()} and \function{isdir()} can
94be true for the same path.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\end{funcdesc}
96
Fred Drakea9b9bf91999-02-02 18:58:33 +000097\begin{funcdesc}{islink}{path}
98Return true if \var{path} refers to a directory entry that is a
99symbolic link. Always false if symbolic links are not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100\end{funcdesc}
101
Fred Drakea9b9bf91999-02-02 18:58:33 +0000102\begin{funcdesc}{ismount}{path}
103Return true if pathname \var{path} is a \dfn{mount point}: a point in
104a file system where a different file system has been mounted. The
105function checks whether \var{path}'s parent, \file{\var{path}/..}, is
106on a different device than \var{path}, or whether \file{\var{path}/..}
107and \var{path} point to the same i-node on the same device --- this
108should detect mount points for all \UNIX{} and \POSIX{} variants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109\end{funcdesc}
110
Fred Drakea9b9bf91999-02-02 18:58:33 +0000111\begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}
Barry Warsaw75745871997-02-18 21:53:53 +0000112Joins one or more path components intelligently. If any component is
113an absolute path, all previous components are thrown away, and joining
Fred Drakea9b9bf91999-02-02 18:58:33 +0000114continues. The return value is the concatenation of \var{path1}, and
115optionally \var{path2}, etc., with exactly one slash (\code{'/'})
116inserted between components, unless \var{path} is empty.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000117\end{funcdesc}
118
Fred Drakea9b9bf91999-02-02 18:58:33 +0000119\begin{funcdesc}{normcase}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000120Normalize the case of a pathname. On \UNIX{}, this returns the path
121unchanged; on case-insensitive filesystems, it converts the path to
122lowercase. On Windows, it also converts forward slashes to backward
123slashes.
124\end{funcdesc}
125
Fred Drakea9b9bf91999-02-02 18:58:33 +0000126\begin{funcdesc}{normpath}{path}
Guido van Rossum1931c0c1998-02-18 14:00:05 +0000127Normalize a pathname. This collapses redundant separators and
128up-level references, e.g. \code{A//B}, \code{A/./B} and
129\code{A/foo/../B} all become \code{A/B}. It does not normalize the
Fred Drake38e5d272000-04-03 20:13:55 +0000130case (use \function{normcase()} for that). On Windows, it converts
131forward slashes to backward slashes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
Fred Drakea9b9bf91999-02-02 18:58:33 +0000134\begin{funcdesc}{samefile}{path1, path2}
Fred Drakedb9693e1998-03-11 05:50:42 +0000135Return true if both pathname arguments refer to the same file or
136directory (as indicated by device number and i-node number).
137Raise an exception if a \function{os.stat()} call on either pathname
138fails.
Fred Drakeaa1afa81999-02-15 16:34:00 +0000139Availability: Macintosh, \UNIX{}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140\end{funcdesc}
141
Fred Draked673d481999-02-03 22:31:30 +0000142\begin{funcdesc}{sameopenfile}{fp1, fp2}
143Return true if the file objects \var{fp1} and \var{fp2} refer to the
144same file. The two file objects may represent different file
145descriptors.
Fred Drakeaa1afa81999-02-15 16:34:00 +0000146Availability: Macintosh, \UNIX{}.
Fred Draked673d481999-02-03 22:31:30 +0000147\end{funcdesc}
148
149\begin{funcdesc}{samestat}{stat1, stat2}
150Return true if the stat tuples \var{stat1} and \var{stat2} refer to
151the same file. These structures may have been returned by
152\function{fstat()}, \function{lstat()}, or \function{stat()}. This
153function implements the underlying comparison used by
154\function{samefile()} and \function{sameopenfile()}.
Fred Drakeaa1afa81999-02-15 16:34:00 +0000155Availability: Macintosh, \UNIX{}.
Fred Draked673d481999-02-03 22:31:30 +0000156\end{funcdesc}
157
Fred Drakea9b9bf91999-02-02 18:58:33 +0000158\begin{funcdesc}{split}{path}
Fred Draked673d481999-02-03 22:31:30 +0000159Split the pathname \var{path} into a pair, \code{(\var{head},
160\var{tail})} where \var{tail} is the last pathname component and
Fred Drakea9b9bf91999-02-02 18:58:33 +0000161\var{head} is everything leading up to that. The \var{tail} part will
162never contain a slash; if \var{path} ends in a slash, \var{tail} will
163be empty. If there is no slash in \var{path}, \var{head} will be
164empty. If \var{path} is empty, both \var{head} and \var{tail} are
165empty. Trailing slashes are stripped from \var{head} unless it is the
166root (one or more slashes only). In nearly all cases,
167\code{join(\var{head}, \var{tail})} equals \var{path} (the only
168exception being when there were multiple slashes separating \var{head}
169from \var{tail}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000170\end{funcdesc}
171
Fred Drake0256c1f1999-02-03 19:24:44 +0000172\begin{funcdesc}{splitdrive}{path}
173Split the pathname \var{path} into a pair \code{(\var{drive},
Fred Draked673d481999-02-03 22:31:30 +0000174\var{tail})} where \var{drive} is either a drive specification or the
Fred Drake0256c1f1999-02-03 19:24:44 +0000175empty string. On systems which do not use drive specifications,
176\var{drive} will always be the empty string. In all cases,
177\code{\var{drive} + \var{tail}} will be the same as \var{path}.
178\end{funcdesc}
179
Fred Drakea9b9bf91999-02-02 18:58:33 +0000180\begin{funcdesc}{splitext}{path}
Fred Drake0256c1f1999-02-03 19:24:44 +0000181Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
Fred Drakea9b9bf91999-02-02 18:58:33 +0000182such that \code{\var{root} + \var{ext} == \var{path}},
Guido van Rossum56b30ea1996-08-19 23:00:50 +0000183and \var{ext} is empty or begins with a period and contains
184at most one period.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000185\end{funcdesc}
186
Fred Drakea9b9bf91999-02-02 18:58:33 +0000187\begin{funcdesc}{walk}{path, visit, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000188Calls the function \var{visit} with arguments
189\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
Fred Drakea9b9bf91999-02-02 18:58:33 +0000190directory tree rooted at \var{path} (including \var{path} itself, if it
191is a directory). The argument \var{dirname} specifies the visited
192directory, the argument \var{names} lists the files in the directory
193(gotten from \code{os.listdir(\var{dirname})}).
Guido van Rossume8e87991997-03-25 15:25:54 +0000194The \var{visit} function may modify \var{names} to
Guido van Rossum470be141995-03-17 16:07:09 +0000195influence the set of directories visited below \var{dirname}, e.g., to
196avoid visiting certain parts of the tree. (The object referred to by
Fred Drakedb9693e1998-03-11 05:50:42 +0000197\var{names} must be modified in place, using \keyword{del} or slice
Guido van Rossum470be141995-03-17 16:07:09 +0000198assignment.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000199\end{funcdesc}