📄 module-ospath.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.2 os.path -- Common pathname manipulations</title>
<META NAME="description" CONTENT="6.2 os.path -- Common pathname manipulations">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-dircache.html" tppabs="http://www.python.org/doc/current/lib/module-dircache.html">
<LINK REL="previous" href="module-os.html" tppabs="http://www.python.org/doc/current/lib/module-os.html">
<LINK REL="up" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">
<LINK REL="next" href="module-dircache.html" tppabs="http://www.python.org/doc/current/lib/module-dircache.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="os-path.html" tppabs="http://www.python.org/doc/current/lib/os-path.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-dircache.html" tppabs="http://www.python.org/doc/current/lib/module-dircache.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="os-path.html" tppabs="http://www.python.org/doc/current/lib/os-path.html">6.1.6 Miscellaneous System Information</A>
<b class="navlabel">Up:</b> <a class="sectref" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">6. Generic Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-dircache.html" tppabs="http://www.python.org/doc/current/lib/module-dircache.html">6.3 dircache </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION008200000000000000000">
6.2 <tt class="module">os.path</tt> --
Common pathname manipulations</A>
</H1>
<P>
<P>
This module implements some useful functions on pathnames.
<P>
<dl><dt><b><a name='l2h-1028'><tt class='function'>abspath</tt></a></b> (<var>path</var>)
<dd>
Return a normalized absolutized version of the pathname <var>path</var>.
On most platforms, this is equivalent to
<code>normpath(join(os.getcwd(), <var>path</var>))</code>.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-1029'><tt class='function'>basename</tt></a></b> (<var>path</var>)
<dd>
Return the base name of pathname <var>path</var>. This is the second half
of the pair returned by <code>split(<var>path</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-1030'><tt class='function'>commonprefix</tt></a></b> (<var>list</var>)
<dd>
Return the longest path prefix (taken character-by-character) that is a
prefix of all paths in
<var>list</var>. If <var>list</var> is empty, return the empty string
(<code>''</code>). Note that this may return invalid paths because it works a
character at a time.
</dl>
<P>
<dl><dt><b><a name='l2h-1031'><tt class='function'>dirname</tt></a></b> (<var>path</var>)
<dd>
Return the directory name of pathname <var>path</var>. This is the first
half of the pair returned by <code>split(<var>path</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-1032'><tt class='function'>exists</tt></a></b> (<var>path</var>)
<dd>
Return true if <var>path</var> refers to an existing path.
</dl>
<P>
<dl><dt><b><a name='l2h-1033'><tt class='function'>expanduser</tt></a></b> (<var>path</var>)
<dd>
Return the argument with an initial component of "<tt class="samp">~</tt>" or
"<tt class="samp">~<var>user</var></tt>" replaced by that <var>user</var>'s home directory. An
initial "<tt class="samp">~</tt>" is replaced by the environment variable
<a class="envvar" name='l2h-1054'>$HOME</a>; an initial "<tt class="samp">~<var>user</var></tt>" is looked up in the
password directory through the built-in module
<tt class='module'><a href="module-pwd.html" tppabs="http://www.python.org/doc/current/lib/module-pwd.html">pwd</a></tt>. 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>path</var> unchanged.
</dl>
<P>
<dl><dt><b><a name='l2h-1034'><tt class='function'>expandvars</tt></a></b> (<var>path</var>)
<dd>
Return the argument with environment variables expanded. Substrings
of the form "<tt class="samp">$<var>name</var></tt>" or "<tt class="samp">${<var>name</var>}</tt>" are
replaced by the value of environment variable <var>name</var>. Malformed
variable names and references to non-existing variables are left
unchanged. On the Macintosh, this always returns <var>path</var>
unchanged.
</dl>
<P>
<dl><dt><b><a name='l2h-1035'><tt class='function'>getatime</tt></a></b> (<var>path</var>)
<dd>
Return the time of last access of <var>filename</var>. The return
value is integer giving the number of seconds since the epoch (see the
<tt class='module'><a href="module-time.html" tppabs="http://www.python.org/doc/current/lib/module-time.html">time</a></tt> module). Raise <tt class="exception">os.error</tt> if the file does
not exist or is inaccessible.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-1036'><tt class='function'>getmtime</tt></a></b> (<var>path</var>)
<dd>
Return the time of last modification of <var>filename</var>. The return
value is integer giving the number of seconds since the epoch (see the
<tt class='module'><a href="module-time.html" tppabs="http://www.python.org/doc/current/lib/module-time.html">time</a></tt> module). Raise <tt class="exception">os.error</tt> if the file does
not exist or is inaccessible.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-1037'><tt class='function'>getsize</tt></a></b> (<var>path</var>)
<dd>
Return the size, in bytes, of <var>filename</var>. Raise
<tt class="exception">os.error</tt> if the file does not exist or is inaccessible.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-1038'><tt class='function'>isabs</tt></a></b> (<var>path</var>)
<dd>
Return true if <var>path</var> is an absolute pathname (begins with a
slash).
</dl>
<P>
<dl><dt><b><a name='l2h-1039'><tt class='function'>isfile</tt></a></b> (<var>path</var>)
<dd>
Return true if <var>path</var> is an existing regular file. This follows
symbolic links, so both <tt class="function">islink()</tt> and <tt class="function">isfile()</tt>
can be true for the same path.
</dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -