📄 module-os.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.1 os -- Miscellaneous OS interfaces</title>
<META NAME="description" CONTENT="6.1 os -- Miscellaneous OS interfaces">
<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-os.path.html" tppabs="http://www.python.org/doc/current/lib/module-os.path.html">
<LINK REL="previous" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">
<LINK REL="up" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">
<LINK REL="next" href="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.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="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.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="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">6. Generic Operating System</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="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.html">6.1.1 Process Parameters</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION008100000000000000000">
6.1 <tt class="module">os</tt> --
Miscellaneous OS interfaces</A>
</H1>
<P>
<P>
This module provides a more portable way of using operating system
(OS) dependent functionality than importing an OS dependent built-in
module like <tt class='module'><a href="module-posix.html" tppabs="http://www.python.org/doc/current/lib/module-posix.html">posix</a></tt> or <tt class="module">nt</tt>.
<P>
This module searches for an OS dependent built-in module like
<tt class="module">mac</tt> or <tt class='module'><a href="module-posix.html" tppabs="http://www.python.org/doc/current/lib/module-posix.html">posix</a></tt> and exports the same functions and data
as found there. The design of all Python's built-in OS dependent
modules is such that as long as the same functionality is available,
it uses the same interface; e.g., the function
<code>os.stat(<var>path</var>)</code> returns stat information about <var>path</var> in
the same format (which happens to have originated with the
POSIX interface).
<P>
Extensions peculiar to a particular OS are also available through the
<tt class="module">os</tt> module, but using them is of course a threat to
portability!
<P>
Note that after the first time <tt class="module">os</tt> is imported, there is
<i>no</i> performance penalty in using functions from <tt class="module">os</tt>
instead of directly from the OS dependent built-in module, so there
should be <i>no</i> reason not to use <tt class="module">os</tt>!
<P>
The <tt class="module">os</tt> module contains many functions and data values.
The items below and in the following sub-sections are all available
directly from the <tt class="module">os</tt> module.
<P>
<dl><dt><b><a name='l2h-866'><tt class='exception'>error</tt></a></b>
<dd>
This exception is raised when a function returns a
system-related error (e.g., not for illegal argument types). This is
also known as the built-in exception <tt class="exception">OSError</tt>. The
accompanying value is a pair containing the numeric error code from
<tt class="cdata">errno</tt> and the corresponding string, as would be printed by the
C function <tt class="cfunction">perror()</tt>. See the module
<tt class='module'><a href="module-errno.html" tppabs="http://www.python.org/doc/current/lib/module-errno.html">errno</a></tt>, which contains names for the
error codes defined by the underlying operating system.
<P>
When exceptions are classes, this exception carries two attributes,
<tt class="member">errno</tt> and <tt class="member">strerror</tt>. The first holds the value of
the C <tt class="cdata">errno</tt> variable, and the latter holds the corresponding
error message from <tt class="cfunction">strerror()</tt>. For exceptions that
involve a file system path (e.g. <tt class="function">chdir()</tt> or
<tt class="function">unlink()</tt>), the exception instance will contain a third
attribute, <tt class="member">filename</tt>, which is the file name passed to the
function.
<P>
When exceptions are strings, the string for the exception is
<code>'OSError'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-867'><tt>name</tt></a></b>
<dd>
The name of the OS dependent module imported. The following names
have currently been registered: <code>'posix'</code>, <code>'nt'</code>,
<code>'dos'</code>, <code>'mac'</code>, <code>'os2'</code>, <code>'ce'</code>, <code>'java'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-868'><tt>path</tt></a></b>
<dd>
The corresponding OS dependent standard module for pathname
operations, e.g., <tt class="module">posixpath</tt> or <tt class="module">macpath</tt>. Thus, given
the proper imports, <code>os.path.split(<var>file</var>)</code> is equivalent to but
more portable than <code>posixpath.split(<var>file</var>)</code>. Note that this
is also a valid module: it may be imported directly as
<tt class='module'><a href="module-os.path.html" tppabs="http://www.python.org/doc/current/lib/module-os.path.html">os.path</a></tt>.
</dl>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2176"
href="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.html">6.1.1 Process Parameters </A>
<LI><A NAME="tex2html2177"
href="os-newstreams.html" tppabs="http://www.python.org/doc/current/lib/os-newstreams.html">6.1.2 File Object Creation </A>
<LI><A NAME="tex2html2178"
href="os-fd-ops.html" tppabs="http://www.python.org/doc/current/lib/os-fd-ops.html">6.1.3 File Descriptor Operations </A>
<LI><A NAME="tex2html2179"
href="os-file-dir.html" tppabs="http://www.python.org/doc/current/lib/os-file-dir.html">6.1.4 Files and Directories </A>
<LI><A NAME="tex2html2180"
href="os-process.html" tppabs="http://www.python.org/doc/current/lib/os-process.html">6.1.5 Process Management </A>
<LI><A NAME="tex2html2181"
href="os-path.html" tppabs="http://www.python.org/doc/current/lib/os-path.html">6.1.6 Miscellaneous System Information </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.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="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.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="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">6. Generic Operating System</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="os-procinfo.html" tppabs="http://www.python.org/doc/current/lib/os-procinfo.html">6.1.1 Process Parameters</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -