2019-04-21 11:50:48 +08:00

5 lines
9.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-posix"><h1><span class="yiyi-st" id="yiyi-10">35.1. <a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a> - 最常见的POSIX系统调用</span></h1><p><span class="yiyi-st" id="yiyi-11">此模块提供对由C标准和POSIX标准一个伪装的Unix接口标准化的操作系统功能的访问。</span></p><p id="index-0"><span class="yiyi-st" id="yiyi-12"><strong>不要直接导入此模块。</strong></span><span class="yiyi-st" id="yiyi-13">请改为导入模块<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>,该模块提供此界面的<em>便携式</em>版本。</span><span class="yiyi-st" id="yiyi-14">在Unix上<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>模块提供<a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a>接口的超集。</span><span class="yiyi-st" id="yiyi-15">在非Unix操作系统上<a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a>模块不可用,但通过<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>接口始终可以使用子集。</span><span class="yiyi-st" id="yiyi-16">导入<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>后,使用它而不是<a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a>时,存在<em>没有</em>性能损失。</span><span class="yiyi-st" id="yiyi-17">此外,<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>提供了一些额外的功能,例如当<code class="docutils literal"><span class="pre">os.environ</span></code>中的条目更改时自动调用<a class="reference internal" href="os.html#os.putenv" title="os.putenv"><code class="xref py py-func docutils literal"><span class="pre">putenv()</span></code></a></span></p><p><span class="yiyi-st" id="yiyi-18">错误报告为异常;对类型错误给出通常的异常,而由系统报告的错误调用引发<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a></span></p><div class="section" id="large-file-support"><h2><span class="yiyi-st" id="yiyi-19">35.1.1. </span><span class="yiyi-st" id="yiyi-20">Large File Support</span></h2><p id="index-1"><span class="yiyi-st" id="yiyi-21">对于<code class="xref c c-type docutils literal"><span class="pre">int</span></code><code class="xref c c-type docutils literal"><span class="pre">long</span></code>的C编程模型多个操作系统包括AIXHP-UXIrix和Solaris支持大于2 GiB的文件位值。</span><span class="yiyi-st" id="yiyi-22">这通常通过将相关大小和偏移类型定义为64位值来实现。</span><span class="yiyi-st" id="yiyi-23">此类文件有时称为<em class="dfn">大文件</em></span></p><p><span class="yiyi-st" id="yiyi-24"><code class="xref c c-type docutils literal"><span class="pre">off_t</span></code>的大小大于<code class="xref c c-type docutils literal"><span class="pre">long</span></code><code class="xref c c-type docutils literal"><span class="pre"></span> <span class="pre">长时在Python中启用大文件支持。 / t6&gt;</span></code>类型可用,并且至少与<code class="xref c c-type docutils literal"><span class="pre">off_t</span></code>一样大。</span><span class="yiyi-st" id="yiyi-25">可能需要使用某些编译器标志来配置和编译Python以启用此模式。</span><span class="yiyi-st" id="yiyi-26">例如默认情况下它使用最新版本的Irix启用但是对于Solaris 2.6和2.7,您需要执行以下操作:</span></p><pre><code class="language-python"><span></span><span class="n">CFLAGS</span><span class="o">=</span><span class="s2">"`getconf LFS_CFLAGS`"</span> <span class="n">OPT</span><span class="o">=</span><span class="s2">"-g -O2 $CFLAGS"</span> \
<span class="o">./</span><span class="n">configure</span>
</code></pre><p><span class="yiyi-st" id="yiyi-27">在大型文件的Linux系统上这可能工作</span></p><pre><code class="language-python"><span></span><span class="n">CFLAGS</span><span class="o">=</span><span class="s1">'-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64'</span> <span class="n">OPT</span><span class="o">=</span><span class="s2">"-g -O2 $CFLAGS"</span> \
<span class="o">./</span><span class="n">configure</span>
</code></pre></div><div class="section" id="notable-module-contents"><h2><span class="yiyi-st" id="yiyi-28">35.1.2.注释模块内容</span></h2><p><span class="yiyi-st" id="yiyi-29">除了<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>模块文档中描述的许多函数,<a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a>定义了以下数据项:</span></p><dl class="data"><dt id="posix.environ"><span class="yiyi-st" id="yiyi-30"> <code class="descclassname">posix.</code><code class="descname">environ</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-31">表示解释器启动时的字符串环境的字典。</span><span class="yiyi-st" id="yiyi-32">键和值是Unix和str上的字节。</span><span class="yiyi-st" id="yiyi-33">例如在Windows上的<code class="docutils literal"><span class="pre">environ[b'HOME']</span></code><code class="docutils literal"><span class="pre">environ['HOME']</span></code>)是您的主目录的路径名,相当于<code class="docutils literal"><span class="pre">getenv("HOME")</span></code></span></p><p><span class="yiyi-st" id="yiyi-34">修改此字典不会影响<a class="reference internal" href="os.html#os.execv" title="os.execv"><code class="xref py py-func docutils literal"><span class="pre">execv()</span></code></a><a class="reference internal" href="os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal"><span class="pre">popen()</span></code></a><a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal"><span class="pre">system()</span></code></a>传递的字符串环境;如果您需要更改环境,请将<code class="docutils literal"><span class="pre">environ</span></code>传递给<a class="reference internal" href="os.html#os.execve" title="os.execve"><code class="xref py py-func docutils literal"><span class="pre">execve()</span></code></a>或将变量分配和导出语句添加到<a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal"><span class="pre">system()</span></code></a><a class="reference internal" href="os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal"><span class="pre">popen()</span></code></a></span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-35"><span class="versionmodified">在版本3.2中更改:</span>在Unix上键和值是字节。</span></p></div><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-36">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-37"><a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>模块提供了<code class="docutils literal"><span class="pre">environ</span></code>的替代实现,它在修改时更新环境。</span><span class="yiyi-st" id="yiyi-38">还要注意,更新<a class="reference internal" href="os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal"><span class="pre">os.environ</span></code></a>会使此字典过时。</span><span class="yiyi-st" id="yiyi-39">建议使用<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a>模块版本,直接访问<a class="reference internal" href="#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">posix</span></code></a>模块。</span></p></div></dd></dl></div></div></div>