cinap_lenrekadd hg and python4bb9e2215 years agoHistory
281 lines · 11.3 KB · TeX
RawDownload
\section{\module{os.path} ---         Common pathname manipulations}\declaremodule{standard}{os.path} \modulesynopsis{Common pathname manipulations.} This module implements some useful functions on pathnames.\index{path!operations} \warning{On Windows, many of these functions do not properlysupport UNC pathnames.  \function{splitunc()} and \function{ismount()}do handle them correctly.}  \begin{funcdesc}{abspath}{path}Return a normalized absolutized version of the pathname \var{path}.On most platforms, this is equivalent to\code{normpath(join(os.getcwd(), \var{path}))}.\versionadded{1.5.2}\end{funcdesc} \begin{funcdesc}{basename}{path}Return the base name of pathname \var{path}.  This is the second halfof the pair returned by \code{split(\var{path})}.  Note that theresult of this function is different from the\UNIX{} \program{basename} program; where \program{basename} for\code{'/foo/bar/'} returns \code{'bar'}, the \function{basename()}function returns an empty string (\code{''}).\end{funcdesc} \begin{funcdesc}{commonprefix}{list}Return the longest path prefix (taken character-by-character) that is aprefix of all paths in \var{list}.  If \var{list} is empty, return the empty string(\code{''}).  Note that this may return invalid paths because it works acharacter at a time.\end{funcdesc} \begin{funcdesc}{dirname}{path}Return the directory name of pathname \var{path}.  This is the firsthalf of the pair returned by \code{split(\var{path})}.\end{funcdesc} \begin{funcdesc}{exists}{path}Return \code{True} if \var{path} refers to an existing path.  Returns\code{False} for broken symbolic links. On some platforms, thisfunction may return \code{False} if permission is not granted toexecute \function{os.stat()} on the requested file, even if the\var{path} physically exists.\end{funcdesc} \begin{funcdesc}{lexists}{path}Return \code{True} if \var{path} refers to an existing path.Returns \code{True} for broken symbolic links.  Equivalent to \function{exists()} on platforms lacking\function{os.lstat()}.\versionadded{2.4}\end{funcdesc} \begin{funcdesc}{expanduser}{path}On \UNIX, return the argument with an initial component of \samp{\~} or\samp{\~\var{user}} replaced by that \var{user}'s home directory.An initial \samp{\~} is replaced by the environment variable\envvar{HOME} if it is set; otherwise the current user's home directoryis looked up in the password directory through the built-in module\refmodule{pwd}\refbimodindex{pwd}.An initial \samp{\~\var{user}} is looked up directly in thepassword directory. On Windows, only \samp{\~} is supported; it is replaced by theenvironment variable \envvar{HOME} or by a combination of\envvar{HOMEDRIVE} and \envvar{HOMEPATH}. If the expansion fails or if thepath does not begin with a tilde, the path is returned unchanged.\end{funcdesc} \begin{funcdesc}{expandvars}{path}Return the argument with environment variables expanded.  Substringsof the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} arereplaced by the value of environment variable \var{name}.  Malformedvariable names and references to non-existing variables are leftunchanged.\end{funcdesc} \begin{funcdesc}{getatime}{path}Return the time of last access of \var{path}.  The returnvalue is a number giving the number of seconds since the epoch (see the \refmodule{time} module).  Raise \exception{os.error} if the file doesnot exist or is inaccessible.\versionadded{1.5.2}\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}\end{funcdesc} \begin{funcdesc}{getmtime}{path}Return the time of last modification of \var{path}.  The returnvalue is a number giving the number of seconds since the epoch (see the \refmodule{time} module).  Raise \exception{os.error} if the file doesnot exist or is inaccessible.\versionadded{1.5.2}\versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}\end{funcdesc} \begin{funcdesc}{getctime}{path}Return the system's ctime which, on some systems (like \UNIX) is thetime of the last change, and, on others (like Windows), is thecreation time for \var{path}.  The returnvalue is a number giving the number of seconds since the epoch (see the \refmodule{time} module).  Raise \exception{os.error} if the file doesnot exist or is inaccessible.\versionadded{2.3}\end{funcdesc} \begin{funcdesc}{getsize}{path}Return the size, in bytes, of \var{path}.  Raise\exception{os.error} if the file does not exist or is inaccessible.\versionadded{1.5.2}\end{funcdesc} \begin{funcdesc}{isabs}{path}Return \code{True} if \var{path} is an absolute pathname (begins with aslash).\end{funcdesc} \begin{funcdesc}{isfile}{path}Return \code{True} if \var{path} is an existing regular file.  This followssymbolic links, so both \function{islink()} and \function{isfile()}can be true for the same path.\end{funcdesc} \begin{funcdesc}{isdir}{path}Return \code{True} if \var{path} is an existing directory.  This followssymbolic links, so both \function{islink()} and \function{isdir()} canbe true for the same path.\end{funcdesc} \begin{funcdesc}{islink}{path}Return \code{True} if \var{path} refers to a directory entry that is asymbolic link.  Always \code{False} if symbolic links are not supported.\end{funcdesc} \begin{funcdesc}{ismount}{path}Return \code{True} if pathname \var{path} is a \dfn{mount point}: a point ina file system where a different file system has been mounted.  Thefunction checks whether \var{path}'s parent, \file{\var{path}/..}, ison a different device than \var{path}, or whether \file{\var{path}/..}and \var{path} point to the same i-node on the same device --- thisshould detect mount points for all \UNIX{} and \POSIX{} variants.\end{funcdesc} \begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}Join one or more path components intelligently.  If any component isan absolute path, all previous components (on Windows, including theprevious drive letter, if there was one) are thrown away, and joiningcontinues.  The return value is the concatenation of \var{path1}, andoptionally \var{path2}, etc., with exactly one directory separator(\code{os.sep}) inserted between components, unless \var{path2} isempty.  Note that on Windows, since there is a current directory foreach drive, \function{os.path.join("c:", "foo")} represents a pathrelative to the current directory on drive \file{C:} (\file{c:foo}), not\file{c:\textbackslash\textbackslash foo}.\end{funcdesc} \begin{funcdesc}{normcase}{path}Normalize the case of a pathname.  On \UNIX, this returns the pathunchanged; on case-insensitive filesystems, it converts the path tolowercase.  On Windows, it also converts forward slashes to backwardslashes.\end{funcdesc} \begin{funcdesc}{normpath}{path}Normalize a pathname.  This collapses redundant separators andup-level references so that \code{A//B}, \code{A/./B} and\code{A/foo/../B} all become \code{A/B}.  It does not normalize thecase (use \function{normcase()} for that).  On Windows, it convertsforward slashes to backward slashes. It should be understood that this maychange the meaning of the path if it contains symbolic links! \end{funcdesc} \begin{funcdesc}{realpath}{path}Return the canonical path of the specified filename, eliminating anysymbolic links encountered in the path (if they are supported by theoperating system).\versionadded{2.2}\end{funcdesc} \begin{funcdesc}{samefile}{path1, path2}Return \code{True} if both pathname arguments refer to the same file ordirectory (as indicated by device number and i-node number).Raise an exception if a \function{os.stat()} call on either pathnamefails.Availability:  Macintosh, \UNIX.\end{funcdesc} \begin{funcdesc}{sameopenfile}{fp1, fp2}Return \code{True} if the file descriptors \var{fp1} and \var{fp2} referto the same file.Availability:  Macintosh, \UNIX.\end{funcdesc} \begin{funcdesc}{samestat}{stat1, stat2}Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer tothe same file.  These structures may have been returned by\function{fstat()}, \function{lstat()}, or \function{stat()}.  Thisfunction implements the underlying comparison used by\function{samefile()} and \function{sameopenfile()}.Availability:  Macintosh, \UNIX.\end{funcdesc} \begin{funcdesc}{split}{path}Split the pathname \var{path} into 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 willnever contain a slash; if \var{path} ends in a slash, \var{tail} willbe empty.  If there is no slash in \var{path}, \var{head} will beempty.  If \var{path} is empty, both \var{head} and \var{tail} areempty.  Trailing slashes are stripped from \var{head} unless it is theroot (one or more slashes only).  In nearly all cases,\code{join(\var{head}, \var{tail})} equals \var{path} (the onlyexception being when there were multiple slashes separating \var{head}from \var{tail}).\end{funcdesc} \begin{funcdesc}{splitdrive}{path}Split the pathname \var{path} into a pair \code{(\var{drive},\var{tail})} where \var{drive} is either a drive specification or theempty string.  On systems which do not use drive specifications,\var{drive} will always be the empty string.  In all cases,\code{\var{drive} + \var{tail}} will be the same as \var{path}.\versionadded{1.3}\end{funcdesc} \begin{funcdesc}{splitext}{path}Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})} such that \code{\var{root} + \var{ext} == \var{path}},and \var{ext} is empty or begins with a period and containsat most one period.\end{funcdesc} \begin{funcdesc}{splitunc}{path}Split the pathname \var{path} into a pair \code{(\var{unc}, \var{rest})}so that \var{unc} is the UNC mount point (such as \code{r'\e\e host\e mount'}),if present, and \var{rest} the rest of the path (such as \code{r'\e path\e file.ext'}).  For paths containing drive letters, \var{unc}will always be the empty string.Availability:  Windows.\end{funcdesc} \begin{funcdesc}{walk}{path, visit, arg}Calls the function \var{visit} with arguments\code{(\var{arg}, \var{dirname}, \var{names})} for each directory in thedirectory tree rooted at \var{path} (including \var{path} itself, if itis a directory).  The argument \var{dirname} specifies the visiteddirectory, the argument \var{names} lists the files in the directory(gotten from \code{os.listdir(\var{dirname})}).The \var{visit} function may modify \var{names} toinfluence the set of directories visited below \var{dirname}, e.g. toavoid visiting certain parts of the tree.  (The object referred to by\var{names} must be modified in place, using \keyword{del} or sliceassignment.) \begin{notice}Symbolic links to directories are not treated as subdirectories, andthat \function{walk()} therefore will not visit them. To visit linkeddirectories you must identify them with\code{os.path.islink(\var{file})} and\code{os.path.isdir(\var{file})}, and invoke \function{walk()} asnecessary.\end{notice} \note{The newer \function{\refmodule{os}.walk()} generator supplies      similar functionality and can be easier to use.}\end{funcdesc} \begin{datadesc}{supports_unicode_filenames}True if arbitrary Unicode strings can be used as file names (withinlimitations imposed by the file system), and if\function{os.listdir()} returns Unicode strings for a Unicodeargument.\versionadded{2.3}\end{datadesc}