1999-02-01 21:20:39 +01:00
|
|
|
\section{\module{os.path} ---
|
|
|
|
Common pathname manipulations}
|
|
|
|
\declaremodule{standard}{os.path}
|
1998-07-23 19:59:49 +02:00
|
|
|
|
1999-02-01 21:20:39 +01:00
|
|
|
\modulesynopsis{Common pathname manipulations.}
|
1995-03-17 17:07:09 +01:00
|
|
|
|
1999-02-01 21:20:39 +01:00
|
|
|
This module implements some useful functions on pathnames.
|
1998-05-14 17:16:12 +02:00
|
|
|
\index{path!operations}
|
1995-03-17 17:07:09 +01:00
|
|
|
|
1999-02-01 21:20:39 +01:00
|
|
|
|
1999-01-29 19:05:05 +01:00
|
|
|
\begin{funcdesc}{abspath}{p}
|
|
|
|
Return a normalized absolutized version of the pathname \var{p}. On
|
|
|
|
most platforms, this is equivalent to
|
|
|
|
\code{normpath(join(os.getcwd()), \var{p})}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1994-01-02 02:22:07 +01:00
|
|
|
\begin{funcdesc}{basename}{p}
|
1999-02-01 21:20:39 +01:00
|
|
|
Return the base name of pathname \var{p}. This is the second half of
|
|
|
|
the pair returned by \code{split(\var{p})}.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{commonprefix}{list}
|
|
|
|
Return the longest string that is a prefix of all strings in
|
1999-02-01 21:20:39 +01:00
|
|
|
\var{list}. If \var{list} is empty, return the empty string
|
|
|
|
(\code{''}).
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{dirname}{p}
|
|
|
|
Return the directory name of pathname \var{p}. This is the first half
|
|
|
|
of the pair returned by \code{split(\var{p})}.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{exists}{p}
|
1999-02-01 21:20:39 +01:00
|
|
|
Return true if \var{p} refers to an existing path.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{expanduser}{p}
|
|
|
|
Return the argument with an initial component of \samp{\~} or
|
|
|
|
\samp{\~\var{user}} replaced by that \var{user}'s home directory. An
|
1998-05-14 17:16:12 +02:00
|
|
|
initial \samp{\~{}} is replaced by the environment variable
|
1998-08-06 17:33:55 +02:00
|
|
|
\envvar{HOME}; an initial \samp{\~\var{user}} is looked up in the
|
|
|
|
password directory through the built-in module
|
1999-02-01 21:20:39 +01:00
|
|
|
\refmodule{pwd}\refbimodindex{pwd}. If the expansion fails, or if the
|
|
|
|
path does not begin with a tilde, the path is returned unchanged. On
|
|
|
|
the Macintosh, this always returns \var{p} unchanged.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1994-04-21 12:32:28 +02:00
|
|
|
\begin{funcdesc}{expandvars}{p}
|
|
|
|
Return the argument with environment variables expanded. Substrings
|
|
|
|
of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
|
|
|
|
replaced by the value of environment variable \var{name}. Malformed
|
|
|
|
variable names and references to non-existing variables are left
|
1999-02-01 21:20:39 +01:00
|
|
|
unchanged. On the Macintosh, this always returns \var{p} unchanged.
|
1994-04-21 12:32:28 +02:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-07-24 22:49:39 +02:00
|
|
|
\begin{funcdesc}{getsize}{filename}
|
1998-07-27 23:11:42 +02:00
|
|
|
\versionadded{1.5.2}
|
1998-07-24 22:49:39 +02:00
|
|
|
Return the size, in bytes, of \var{filename}. Raise
|
|
|
|
\exception{os.error} if the file does not exist or is inaccessible.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{getmtime}{filename}
|
1998-07-27 23:11:42 +02:00
|
|
|
\versionadded{1.5.2}
|
1998-07-24 22:49:39 +02:00
|
|
|
Return the time of last modification of \var{filename}. The return
|
|
|
|
value is integer giving the number of seconds since the epoch (see the
|
1999-02-01 21:20:39 +01:00
|
|
|
\refmodule{time} module). Raise \exception{os.error} if the file does
|
|
|
|
not exist or is inaccessible.
|
1998-07-24 22:49:39 +02:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{getatime}{filename}
|
1998-07-27 23:11:42 +02:00
|
|
|
\versionadded{1.5.2}
|
1998-07-24 22:49:39 +02:00
|
|
|
Return the time of last access of \var{filename}. The return
|
|
|
|
value is integer giving the number of seconds since the epoch (see the
|
1999-02-01 21:20:39 +01:00
|
|
|
\refmodule{time} module). Raise \exception{os.error} if the file does
|
|
|
|
not exist or is inaccessible.
|
1998-07-24 22:49:39 +02:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1994-01-02 02:22:07 +01:00
|
|
|
\begin{funcdesc}{isabs}{p}
|
|
|
|
Return true if \var{p} is an absolute pathname (begins with a slash).
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isfile}{p}
|
|
|
|
Return true if \var{p} is an existing regular file. This follows
|
1998-03-11 06:50:42 +01:00
|
|
|
symbolic links, so both \function{islink()} and \function{isfile()}
|
|
|
|
can be true for the same path.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isdir}{p}
|
|
|
|
Return true if \var{p} is an existing directory. This follows
|
1998-03-11 06:50:42 +01:00
|
|
|
symbolic links, so both \function{islink()} and \function{isdir()} can
|
|
|
|
be true for the same path.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{islink}{p}
|
|
|
|
Return true if
|
|
|
|
\var{p}
|
|
|
|
refers to a directory entry that is a symbolic link.
|
|
|
|
Always false if symbolic links are not supported.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{ismount}{p}
|
1995-03-17 17:07:09 +01:00
|
|
|
Return true if pathname \var{p} is a \dfn{mount point}: a point in a
|
|
|
|
file system where a different file system has been mounted. The
|
|
|
|
function checks whether \var{p}'s parent, \file{\var{p}/..}, is on a
|
|
|
|
different device than \var{p}, or whether \file{\var{p}/..} and
|
|
|
|
\var{p} point to the same i-node on the same device --- this should
|
1998-02-09 21:27:12 +01:00
|
|
|
detect mount points for all \UNIX{} and \POSIX{} variants.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 07:33:25 +01:00
|
|
|
\begin{funcdesc}{join}{p\optional{, q\optional{, ...}}}
|
1997-02-18 22:53:53 +01:00
|
|
|
Joins one or more path components intelligently. If any component is
|
|
|
|
an absolute path, all previous components are thrown away, and joining
|
|
|
|
continues. The return value is the concatenation of \var{p}, and
|
|
|
|
optionally \var{q}, etc., with exactly one slash (\code{'/'}) inserted
|
|
|
|
between components, unless \var{p} is empty.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{normcase}{p}
|
1998-02-18 15:00:05 +01:00
|
|
|
Normalize the case of a pathname. On \UNIX{}, this returns the path
|
|
|
|
unchanged; on case-insensitive filesystems, it converts the path to
|
|
|
|
lowercase. On Windows, it also converts forward slashes to backward
|
|
|
|
slashes.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{normpath}{p}
|
|
|
|
Normalize a pathname. This collapses redundant separators and
|
|
|
|
up-level references, e.g. \code{A//B}, \code{A/./B} and
|
|
|
|
\code{A/foo/../B} all become \code{A/B}. It does not normalize the
|
1998-03-11 06:50:42 +01:00
|
|
|
case (use \function{normcase()} for that). On Windows, it does
|
|
|
|
converts forward slashes to backward slashes.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 07:33:25 +01:00
|
|
|
\begin{funcdesc}{samefile}{p, q}
|
1998-03-11 06:50:42 +01:00
|
|
|
Return true if both pathname arguments refer to the same file or
|
|
|
|
directory (as indicated by device number and i-node number).
|
|
|
|
Raise an exception if a \function{os.stat()} call on either pathname
|
|
|
|
fails.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{split}{p}
|
1996-06-26 21:23:26 +02:00
|
|
|
Split the pathname \var{p} in a pair \code{(\var{head}, \var{tail})},
|
|
|
|
where \var{tail} is the last pathname component and \var{head} is
|
|
|
|
everything leading up to that. The \var{tail} part will never contain
|
|
|
|
a slash; if \var{p} ends in a slash, \var{tail} will be empty. If
|
|
|
|
there is no slash in \var{p}, \var{head} will be empty. If \var{p} is
|
|
|
|
empty, both \var{head} and \var{tail} are empty. Trailing slashes are
|
|
|
|
stripped from \var{head} unless it is the root (one or more slashes
|
|
|
|
only). In nearly all cases, \code{join(\var{head}, \var{tail})}
|
|
|
|
equals \var{p} (the only exception being when there were multiple
|
|
|
|
slashes separating \var{head} from \var{tail}).
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{splitext}{p}
|
|
|
|
Split the pathname \var{p} in a pair \code{(\var{root}, \var{ext})}
|
|
|
|
such that \code{\var{root} + \var{ext} == \var{p}},
|
1996-08-20 01:00:50 +02:00
|
|
|
and \var{ext} is empty or begins with a period and contains
|
|
|
|
at most one period.
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 07:33:25 +01:00
|
|
|
\begin{funcdesc}{walk}{p, visit, arg}
|
1994-01-02 02:22:07 +01:00
|
|
|
Calls the function \var{visit} with arguments
|
|
|
|
\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
|
|
|
|
directory tree rooted at \var{p} (including \var{p} itself, if it is a
|
|
|
|
directory). The argument \var{dirname} specifies the visited directory,
|
|
|
|
the argument \var{names} lists the files in the directory (gotten from
|
1998-03-11 06:50:42 +01:00
|
|
|
\code{os.listdir(\var{dirname})}).
|
1997-03-25 16:25:54 +01:00
|
|
|
The \var{visit} function may modify \var{names} to
|
1995-03-17 17:07:09 +01:00
|
|
|
influence the set of directories visited below \var{dirname}, e.g., to
|
|
|
|
avoid visiting certain parts of the tree. (The object referred to by
|
1998-03-11 06:50:42 +01:00
|
|
|
\var{names} must be modified in place, using \keyword{del} or slice
|
1995-03-17 17:07:09 +01:00
|
|
|
assignment.)
|
1994-01-02 02:22:07 +01:00
|
|
|
\end{funcdesc}
|