1385 lines
170 KiB
HTML
1385 lines
170 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>subprocess --- 子进程管理 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="sched --- 事件调度器" href="sched.html" />
|
||
<link rel="prev" title="concurrent.futures --- 启动并行任务" href="concurrent.futures.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/subprocess.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> --- 子进程管理</a><ul>
|
||
<li><a class="reference internal" href="#using-the-subprocess-module">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> 模块</a><ul>
|
||
<li><a class="reference internal" href="#frequently-used-arguments">常用参数</a></li>
|
||
<li><a class="reference internal" href="#popen-constructor">Popen 构造函数</a></li>
|
||
<li><a class="reference internal" href="#exceptions">异常</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#security-considerations">安全考量</a></li>
|
||
<li><a class="reference internal" href="#popen-objects">Popen 对象</a></li>
|
||
<li><a class="reference internal" href="#windows-popen-helpers">Windows Popen 助手</a><ul>
|
||
<li><a class="reference internal" href="#windows-constants">Windows 常数</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#older-high-level-api">较旧的高阶 API</a></li>
|
||
<li><a class="reference internal" href="#replacing-older-functions-with-the-subprocess-module">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> 模块替换旧函数</a><ul>
|
||
<li><a class="reference internal" href="#replacing-bin-sh-shell-command-substitution">替代 <strong class="program">/bin/sh</strong> shell 命令替换</a></li>
|
||
<li><a class="reference internal" href="#replacing-shell-pipeline">替代 shell 管道</a></li>
|
||
<li><a class="reference internal" href="#replacing-os-system">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a></li>
|
||
<li><a class="reference internal" href="#replacing-the-os-spawn-family">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawn</span></code> 函数族</a></li>
|
||
<li><a class="reference internal" href="#replacing-os-popen-os-popen2-os-popen3">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen2()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen3()</span></code></a></li>
|
||
<li><a class="reference internal" href="#replacing-functions-from-the-popen2-module">来自 <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> 模块的替代函数</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#legacy-shell-invocation-functions">旧式的 Shell 发起函数</a></li>
|
||
<li><a class="reference internal" href="#notes">备注</a><ul>
|
||
<li><a class="reference internal" href="#converting-an-argument-sequence-to-a-string-on-windows">在 Windows 上将参数列表转换为一个字符串</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="concurrent.futures.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> --- 启动并行任务</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="sched.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> --- 事件调度器</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/subprocess.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/subprocess.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="sched.html" title="sched --- 事件调度器"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="concurrent.futures.html" title="concurrent.futures --- 启动并行任务"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">并发执行</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-subprocess">
|
||
<span id="subprocess-subprocess-management"></span><h1><a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> --- 子进程管理<a class="headerlink" href="#module-subprocess" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/subprocess.py">Lib/subprocess.py</a></p>
|
||
<hr class="docutils" />
|
||
<p><a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块允许你生成新的进程,连接它们的输入、输出、错误管道,并且获取它们的返回码。此模块打算代替一些老旧的模块与功能:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">system</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">spawn</span><span class="o">*</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>在下面的段落中,你可以找到关于 <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块如何代替这些模块和功能的相关信息。</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<p><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0324"><strong>PEP 324</strong></a> -- 提出 subprocess 模块的 PEP</p>
|
||
</div>
|
||
<section id="using-the-subprocess-module">
|
||
<h2>使用 <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块<a class="headerlink" href="#using-the-subprocess-module" title="永久链接至标题">¶</a></h2>
|
||
<p>推荐的调用子进程的方式是在任何它支持的用例中使用 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 函数。对于更进阶的用例,也可以使用底层的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 接口。</p>
|
||
<p><a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 函数是在 Python 3.5 被添加的;如果你需要与旧版本保持兼容,查看 <a class="reference internal" href="#call-function-trio"><span class="std std-ref">较旧的高阶 API</span></a> 段落。</p>
|
||
<dl class="function">
|
||
<dt id="subprocess.run">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">run</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">*</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">input=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">capture_output=False</em>, <em class="sig-param">shell=False</em>, <em class="sig-param">cwd=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">check=False</em>, <em class="sig-param">encoding=None</em>, <em class="sig-param">errors=None</em>, <em class="sig-param">text=None</em>, <em class="sig-param">env=None</em>, <em class="sig-param">universal_newlines=None</em>, <em class="sig-param">**other_popen_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.run" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>运行被 <em>arg</em> 描述的指令. 等待指令完成, 然后返回一个 <a class="reference internal" href="#subprocess.CompletedProcess" title="subprocess.CompletedProcess"><code class="xref py py-class docutils literal notranslate"><span class="pre">CompletedProcess</span></code></a> 实例.</p>
|
||
<p>以上显示的参数仅仅是最简单的一些,下面 <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">常用参数</span></a> 描述(因此在缩写签名中使用仅关键字标示)。完整的函数头和 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的构造函数一样,此函数接受的大多数参数都被传递给该接口。(<em>timeout</em>, <em>input</em>, <em>check</em> 和 <em>capture_output</em> 除外)。</p>
|
||
<p>如果 <em>capture_output</em> 设为 true,stdout 和 stderr 将会被捕获。在使用时,内置的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 对象将自动用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 和 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> 创建。<em>stdout</em> 和 <em>stderr</em> 参数不应当与 <em>capture_output</em> 同时提供。如果你希望捕获并将两个流合并在一起,使用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 和 <code class="docutils literal notranslate"><span class="pre">stderr=STDOUT</span></code> 来代替 <em>capture_output</em>。</p>
|
||
<p><em>timeout</em> 参数将被传递给 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a>。如果发生超时,子进程将被杀死并等待。 <a class="reference internal" href="#subprocess.TimeoutExpired" title="subprocess.TimeoutExpired"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutExpired</span></code></a> 异常将在子进程中断后被抛出。</p>
|
||
<p><em>input</em> 参数将被传递给 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a> 以及子进程的 stdin。 如果使用此参数,它必须是一个字节序列。 如果指定了 <em>encoding</em> 或 <em>errors</em> 或者将 <em>text</em> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,那么也可以是一个字符串。 当使用此参数时,在创建内部 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 对象时将自动带上 <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code>,并且不能再手动指定 <em>stdin</em> 参数。</p>
|
||
<p>如果 <em>check</em> 设为 True, 并且进程以非零状态码退出, 一个 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a> 异常将被抛出. 这个异常的属性将设置为参数, 退出码, 以及标准输出和标准错误, 如果被捕获到.</p>
|
||
<p>如果指定了 <em>encoding</em> 或 <em>error</em>,或者 <em>text</em> 被设为真值,标准输入、标准输出和标准错误的文件对象将使用指定的 <em>encoding</em> 和 <em>errors</em> 或者 <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> 默认值以文本模式打开。 <em>universal_newlines</em> 参数等同于 <em>text</em> 并且提供了向后兼容性。 默认情况下, 文件对象是以二进制模式打开的。</p>
|
||
<p>如果 <em>env</em> 不是 <code class="docutils literal notranslate"><span class="pre">None</span></code>, 它必须是一个字典, 为新的进程设置环境变量; 它用于替换继承的当前进程的环境的默认行为. 它将直接被传递给 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a>.</p>
|
||
<p>示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">"ls"</span><span class="p">,</span> <span class="s2">"-l"</span><span class="p">])</span> <span class="c1"># doesn't capture output</span>
|
||
<span class="go">CompletedProcess(args=['ls', '-l'], returncode=0)</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"exit 1"</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="w"> </span><span class="c">...</span>
|
||
<span class="gr">subprocess.CalledProcessError</span>: <span class="n">Command 'exit 1' returned non-zero exit status 1</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">"ls"</span><span class="p">,</span> <span class="s2">"-l"</span><span class="p">,</span> <span class="s2">"/dev/null"</span><span class="p">],</span> <span class="n">capture_output</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="go">CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,</span>
|
||
<span class="go">stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.5 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>添加了 <em>encoding</em> 和 <em>errors</em> 形参.</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加了 <em>text</em> 形参,作为 <em>universal_newlines</em> 的一个更好理解的别名。 添加了 <em>capture_output</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>针对 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 改变的 Windows shell 搜索顺序。 当前目录和 <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> 会被替换为 <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>。 因此,在当前目录中投放一个命名为 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> 的恶意程序不会再起作用。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="subprocess.CompletedProcess">
|
||
<em class="property">class </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CompletedProcess</code><a class="headerlink" href="#subprocess.CompletedProcess" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 的返回值, 代表一个进程已经结束.</p>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CompletedProcess.args">
|
||
<code class="sig-name descname">args</code><a class="headerlink" href="#subprocess.CompletedProcess.args" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>被用作启动进程的参数. 可能是一个列表或字符串.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CompletedProcess.returncode">
|
||
<code class="sig-name descname">returncode</code><a class="headerlink" href="#subprocess.CompletedProcess.returncode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的退出状态码. 通常来说, 一个为 0 的退出码表示进程运行正常.</p>
|
||
<p>一个负值 <code class="docutils literal notranslate"><span class="pre">-N</span></code> 表示子进程被信号 <code class="docutils literal notranslate"><span class="pre">N</span></code> 中断 (仅 POSIX).</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CompletedProcess.stdout">
|
||
<code class="sig-name descname">stdout</code><a class="headerlink" href="#subprocess.CompletedProcess.stdout" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从子进程捕获到的标准输出. 一个字节序列, 或一个字符串, 如果 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 是设置了 <em>encoding</em>, <em>errors</em> 或者 <code class="docutils literal notranslate"><span class="pre">text=True</span></code> 来运行的. 如果未有捕获, 则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
|
||
<p>如果你通过 <code class="docutils literal notranslate"><span class="pre">stderr=subprocess.STDOUT</span></code> 运行进程,标准输入和标准错误将被组合在这个属性中,并且 <a class="reference internal" href="#subprocess.CompletedProcess.stderr" title="subprocess.CompletedProcess.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stderr</span></code></a> 将为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CompletedProcess.stderr">
|
||
<code class="sig-name descname">stderr</code><a class="headerlink" href="#subprocess.CompletedProcess.stderr" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>捕获到的子进程的标准错误. 一个字节序列, 或者一个字符串, 如果 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 是设置了参数 <em>encoding</em>, <em>errors</em> 或者 <code class="docutils literal notranslate"><span class="pre">text=True</span></code> 运行的. 如果未有捕获, 则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.CompletedProcess.check_returncode">
|
||
<code class="sig-name descname">check_returncode</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.CompletedProcess.check_returncode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <a class="reference internal" href="#subprocess.CompletedProcess.returncode" title="subprocess.CompletedProcess.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 非零, 抛出 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a>.</p>
|
||
</dd></dl>
|
||
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.5 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.DEVNULL">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">DEVNULL</code><a class="headerlink" href="#subprocess.DEVNULL" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>可被 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的 <em>stdin</em>, <em>stdout</em> 或者 <em>stderr</em> 参数使用的特殊值, 表示使用特殊文件 <a class="reference internal" href="os.html#os.devnull" title="os.devnull"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.devnull</span></code></a>.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.PIPE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">PIPE</code><a class="headerlink" href="#subprocess.PIPE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>可被 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的 <em>stdin</em>, <em>stdout</em> 或者 <em>stderr</em> 参数使用的特殊值, 表示打开标准流的管道. 常用于 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a>.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.STDOUT">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STDOUT</code><a class="headerlink" href="#subprocess.STDOUT" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>可被 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的 <em>stderr</em> 参数使用的特殊值, 表示标准错误与标准输出使用同一句柄。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="subprocess.SubprocessError">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">SubprocessError</code><a class="headerlink" href="#subprocess.SubprocessError" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>此模块的其他异常的基类。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="subprocess.TimeoutExpired">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">TimeoutExpired</code><a class="headerlink" href="#subprocess.TimeoutExpired" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#subprocess.SubprocessError" title="subprocess.SubprocessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SubprocessError</span></code></a> 的子类,等待子进程的过程中发生超时时被抛出。</p>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.TimeoutExpired.cmd">
|
||
<code class="sig-name descname">cmd</code><a class="headerlink" href="#subprocess.TimeoutExpired.cmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于创建子进程的指令。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.TimeoutExpired.timeout">
|
||
<code class="sig-name descname">timeout</code><a class="headerlink" href="#subprocess.TimeoutExpired.timeout" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>超时秒数。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.TimeoutExpired.output">
|
||
<code class="sig-name descname">output</code><a class="headerlink" href="#subprocess.TimeoutExpired.output" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的输出, 如果被 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 或 <a class="reference internal" href="#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> 捕获。否则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.TimeoutExpired.stdout">
|
||
<code class="sig-name descname">stdout</code><a class="headerlink" href="#subprocess.TimeoutExpired.stdout" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>对 output 的别名,对应的有 <a class="reference internal" href="#subprocess.TimeoutExpired.stderr" title="subprocess.TimeoutExpired.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stderr</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.TimeoutExpired.stderr">
|
||
<code class="sig-name descname">stderr</code><a class="headerlink" href="#subprocess.TimeoutExpired.stderr" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的标准错误输出,如果被 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 捕获。 否则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>添加了 <em>stdout</em> 和 <em>stderr</em> 属性。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="subprocess.CalledProcessError">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CalledProcessError</code><a class="headerlink" href="#subprocess.CalledProcessError" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#subprocess.SubprocessError" title="subprocess.SubprocessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SubprocessError</span></code></a> 的子类,当一个被 <a class="reference internal" href="#subprocess.check_call" title="subprocess.check_call"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_call()</span></code></a> 或 <a class="reference internal" href="#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> 函数运行的子进程返回了非零退出码时被抛出。</p>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CalledProcessError.returncode">
|
||
<code class="sig-name descname">returncode</code><a class="headerlink" href="#subprocess.CalledProcessError.returncode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的退出状态。如果程序由一个信号终止,这将会被设为一个负的信号码。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CalledProcessError.cmd">
|
||
<code class="sig-name descname">cmd</code><a class="headerlink" href="#subprocess.CalledProcessError.cmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于创建子进程的指令。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CalledProcessError.output">
|
||
<code class="sig-name descname">output</code><a class="headerlink" href="#subprocess.CalledProcessError.output" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的输出, 如果被 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 或 <a class="reference internal" href="#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> 捕获。否则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CalledProcessError.stdout">
|
||
<code class="sig-name descname">stdout</code><a class="headerlink" href="#subprocess.CalledProcessError.stdout" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>对 output 的别名,对应的有 <a class="reference internal" href="#subprocess.CalledProcessError.stderr" title="subprocess.CalledProcessError.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stderr</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.CalledProcessError.stderr">
|
||
<code class="sig-name descname">stderr</code><a class="headerlink" href="#subprocess.CalledProcessError.stderr" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的标准错误输出,如果被 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 捕获。 否则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>添加了 <em>stdout</em> 和 <em>stderr</em> 属性。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<section id="frequently-used-arguments">
|
||
<span id="id1"></span><h3>常用参数<a class="headerlink" href="#frequently-used-arguments" title="永久链接至标题">¶</a></h3>
|
||
<p>为了支持丰富的使用案例, <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的构造函数(以及方便的函数)接受大量可选的参数。对于大多数典型的用例,许多参数可以被安全地留以它们的默认值。通常需要的参数有:</p>
|
||
<blockquote>
|
||
<div><p><em>args</em> 被所有调用需要,应当为一个字符串,或者一个程序参数序列。提供一个参数序列通常更好,它可以更小心地使用参数中的转义字符以及引用(例如允许文件名中的空格)。如果传递一个简单的字符串,则 <em>shell</em> 参数必须为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> (见下文)或者该字符串中将被运行的程序名必须用简单的命名而不指定任何参数。</p>
|
||
<p><em>stdin</em>, <em>stdout</em> 和 <em>stderr</em> 分别指定了执行的程序的标准输入、输出和标准错误文件句柄。合法的值有 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a> 、 <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> 、 一个现存的文件描述符(一个正整数)、一个现存的文件对象以及 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a> 表示应该新建一个对子进程的管道。 <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> 表示使用特殊的文件 <a class="reference internal" href="os.html#os.devnull" title="os.devnull"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.devnull</span></code></a>。当使用默认设置 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时,将不会进行重定向,子进程的文件流将继承自父进程。另外, <em>stderr</em> 可能为 <a class="reference internal" href="#subprocess.STDOUT" title="subprocess.STDOUT"><code class="xref py py-data docutils literal notranslate"><span class="pre">STDOUT</span></code></a>,表示来自于子进程的标准错误数据应该被 <em>stdout</em> 相同的句柄捕获。</p>
|
||
<p id="index-1">如果指定了 <em>encoding</em> 或 <em>errors</em>,或者 <em>text</em> (也称 <em>universal_newlines</em>) 为真,则文件对象 <em>stdin</em>、 <em>stdout</em> 与 <em>stderr</em> 将会使用在此次调用中指定的 <em>encoding</em> 和 <em>errors</em> 或者 <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> 的默认值以文本模式打开。</p>
|
||
<p>当构造函数的 <em>newline</em> 参数为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时。对于 <em>stdin</em>, 输入的换行符 <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> 将被转换为默认的换行符 <a class="reference internal" href="os.html#os.linesep" title="os.linesep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.linesep</span></code></a>。对于 <em>stdout</em> 和 <em>stderr</em>, 所有输出的换行符都被转换为 <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>。更多信息,查看 <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> 类的文档。</p>
|
||
<p>如果文本模式未被使用, <em>stdin</em>, <em>stdout</em> 和 <em>stderr</em> 将会以二进制流模式打开。没有编码与换行符转换发生。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.6 新版功能: </span>添加了 <em>encoding</em> 和 <em>errors</em> 形参。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能: </span>添加了 <em>text</em> 形参作为 <em>universal_newlines</em> 的别名。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>文件对象 <a class="reference internal" href="#subprocess.Popen.stdin" title="subprocess.Popen.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Popen.stdin</span></code></a> 、 <a class="reference internal" href="#subprocess.Popen.stdout" title="subprocess.Popen.stdout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Popen.stdout</span></code></a> 和 <a class="reference internal" href="#subprocess.Popen.stderr" title="subprocess.Popen.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Popen.stderr</span></code></a> 的换行符属性不会被 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a> 方法更新。</p>
|
||
</div>
|
||
<p>如果 <em>shell</em> 设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,,则使用 shell 执行指定的指令。如果您主要使用 Python 增强的控制流(它比大多数系统 shell 提供的强大),并且仍然希望方便地使用其他 shell 功能,如 shell 管道、文件通配符、环境变量展开以及 <code class="docutils literal notranslate"><span class="pre">~</span></code> 展开到用户家目录,这将非常有用。但是,注意 Python 自己也实现了许多类似 shell 的特性(例如 <a class="reference internal" href="glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code></a>, <a class="reference internal" href="fnmatch.html#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code></a>, <a class="reference internal" href="os.html#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.walk()</span></code></a>, <a class="reference internal" href="os.path.html#os.path.expandvars" title="os.path.expandvars"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expandvars()</span></code></a>, <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a> 和 <a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a>)。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>当 <em>universal_newlines</em> 被设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则类将使用 <a class="reference internal" href="locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getpreferredencoding(False)</span></code></a> 编码格式来代替 <code class="docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code>。 关于它们的区别的更多信息,见 <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a>。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>在使用 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 之前, 请阅读 <a class="reference internal" href="#security-considerations">Security Considerations</a> 段落。</p>
|
||
</div>
|
||
</div></blockquote>
|
||
<p>这些选项以及所有其他选项在 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 构造函数文档中有更详细的描述。</p>
|
||
</section>
|
||
<section id="popen-constructor">
|
||
<h3>Popen 构造函数<a class="headerlink" href="#popen-constructor" title="永久链接至标题">¶</a></h3>
|
||
<p>此模块的底层的进程创建与管理由 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 类处理。它提供了很大的灵活性,因此开发者能够处理未被便利函数覆盖的不常见用例。</p>
|
||
<dl class="class">
|
||
<dt id="subprocess.Popen">
|
||
<em class="property">class </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">Popen</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">bufsize=-1</em>, <em class="sig-param">executable=None</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">preexec_fn=None</em>, <em class="sig-param">close_fds=True</em>, <em class="sig-param">shell=False</em>, <em class="sig-param">cwd=None</em>, <em class="sig-param">env=None</em>, <em class="sig-param">universal_newlines=None</em>, <em class="sig-param">startupinfo=None</em>, <em class="sig-param">creationflags=0</em>, <em class="sig-param">restore_signals=True</em>, <em class="sig-param">start_new_session=False</em>, <em class="sig-param">pass_fds=()</em>, <em class="sig-param">*</em>, <em class="sig-param">encoding=None</em>, <em class="sig-param">errors=None</em>, <em class="sig-param">text=None</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在一个新的进程中执行子程序。在 POSIX,此类使用类似于 <a class="reference internal" href="os.html#os.execvp" title="os.execvp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.execvp()</span></code></a> 的行为来执行子程序。在 Windows,此类使用了 Windows <code class="docutils literal notranslate"><span class="pre">CreateProcess()</span></code> 函数。 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 的参数如下:</p>
|
||
<p><em>args</em> 应当是一个程序参数的序列或者是一个单独的字符串或 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。 默认情况下,如果 <em>args</em> 是序列则要运行的程序为 <em>args</em> 中的第一项。 如果 <em>args</em> 是字符串,则其解读依赖于具体平台,如下所述。 请查看 <em>shell</em> 和 <em>executable</em> 参数了解其与默认行为的其他差异。 除非另有说明,否则推荐以序列形式传入 <em>args</em>。</p>
|
||
<p>向外部函数传入序列形式参数的一个例子如下:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Popen</span><span class="p">([</span><span class="s2">"/usr/bin/git"</span><span class="p">,</span> <span class="s2">"commit"</span><span class="p">,</span> <span class="s2">"-m"</span><span class="p">,</span> <span class="s2">"Fixes a bug."</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>在 POSIX,如果 <em>args</em> 是一个字符串,此字符串被作为将被执行的程序的命名或路径解释。但是,只有在不传递任何参数给程序的情况下才能这么做。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>将 shell 命令拆分为参数序列的方式可能并不很直观,特别是在复杂的情况下。 <a class="reference internal" href="shlex.html#shlex.split" title="shlex.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shlex.split()</span></code></a> 可以演示如何确定 <em>args</em> 适当的拆分形式:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">shlex</span><span class="o">,</span> <span class="nn">subprocess</span>
|
||
<span class="gp">>>> </span><span class="n">command_line</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
|
||
<span class="go">/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"</span>
|
||
<span class="gp">>>> </span><span class="n">args</span> <span class="o">=</span> <span class="n">shlex</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">command_line</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
||
<span class="go">['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]</span>
|
||
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="c1"># Success!</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>特别注意,由 shell 中的空格分隔的选项(例如 <em>-input</em>)和参数(例如 <em>eggs.txt</em> )位于分开的列表元素中,而在需要时使用引号或反斜杠转义的参数在 shell (例如包含空格的文件名或上面显示的 <em>echo</em> 命令)是单独的列表元素。</p>
|
||
</div>
|
||
<p>在 Windows,如果 <em>args</em> 是一个序列,他将通过一个在 <a class="reference internal" href="#converting-argument-sequence"><span class="std std-ref">在 Windows 上将参数列表转换为一个字符串</span></a> 描述的方式被转换为一个字符串。这是因为底层的 <code class="docutils literal notranslate"><span class="pre">CreateProcess()</span></code> 只处理字符串。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>在 POSIX 上如果 <em>shell</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 并且序列包含路径类对象则 <em>args</em> 形参可以接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>如果在Windows 上 <em>shell</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 并且序列包含字节串和路径类对象则 <em>args</em> 形参可以接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<p>参数 <em>shell</em> (默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>)指定是否使用 shell 执行程序。如果 <em>shell</em> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,更推荐将 <em>args</em> 作为字符串传递而非序列。</p>
|
||
<p>在 POSIX,当 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, shell 默认为 <code class="file docutils literal notranslate"><span class="pre">/bin/sh</span></code>。如果 <em>args</em> 是一个字符串,此字符串指定将通过 shell 执行的命令。这意味着字符串的格式必须和在命令提示符中所输入的完全相同。这包括,例如,引号和反斜杠转义包含空格的文件名。如果 <em>args</em> 是一个序列,第一项指定了命令,另外的项目将作为传递给 shell (而非命令) 的参数对待。也就是说, <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 等同于:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Popen</span><span class="p">([</span><span class="s1">'/bin/sh'</span><span class="p">,</span> <span class="s1">'-c'</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">...</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>在 Windows,使用 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>,环境变量 <span class="target" id="index-8"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">COMSPEC</span></code> 指定了默认 shell。在 Windows 你唯一需要指定 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 的情况是你想要执行内置在 shell 中的命令(例如 <strong class="command">dir</strong> 或者 <strong class="command">copy</strong>)。在运行一个批处理文件或者基于控制台的可执行文件时,不需要 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>在使用 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 之前, 请阅读 <a class="reference internal" href="#security-considerations">Security Considerations</a> 段落。</p>
|
||
</div>
|
||
<p><em>bufsize</em> 将在 <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 函数创建了 stdin/stdout/stderr 管道文件对象时作为对应的参数供应:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">0</span></code> 表示不使用缓冲区 (读取与写入是一个系统调用并且可以返回短内容)</p></li>
|
||
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">1</span></code> 表示行缓冲(只有 <code class="docutils literal notranslate"><span class="pre">universal_newlines=True</span></code> 时才有用,例如,在文本模式中)</p></li>
|
||
<li><p>任何其他正值表示使用一个约为对应大小的缓冲区</p></li>
|
||
<li><p>负的 <em>bufsize</em> (默认)表示使用系统默认的 io.DEFAULT_BUFFER_SIZE。</p></li>
|
||
</ul>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3.1 版更改: </span><em>bufsize</em> 现在默认为 -1 来启用缓冲,以符合大多数代码所期望的行为。在 Python 3.2.4 和 3.3.1 之前的版本中,它错误地将默认值设为了为 <code class="xref py py-const docutils literal notranslate"><span class="pre">0</span></code>,这是无缓冲的并且允许短读取。这是无意的,并且与大多数代码所期望的 Python 2 的行为不一致。</p>
|
||
</div>
|
||
<p><em>executable</em> 参数指定一个要执行的替换程序。这很少需要。当 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, <em>executable</em> 替换 <em>args</em> 指定运行的程序。但是,原始的 <em>args</em> 仍然被传递给程序。大多数程序将被 <em>args</em> 指定的程序作为命令名对待,这可以与实际运行的程序不同。在 POSIX, <em>args</em> 名作为实际调用程序中可执行文件的显示名称,例如 <strong class="program">ps</strong>。如果 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>,在 POSIX, <em>executable</em> 参数指定用于替换默认 shell <code class="file docutils literal notranslate"><span class="pre">/bin/sh</span></code> 的 shell。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>在POSIX 上 <em>executable</em> 形参可以接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在Windows 上 <em>executable</em> 形参可以接受一个字节串和 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>针对 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 改变的 Windows shell 搜索顺序。 当前目录和 <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> 会被替换为 <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>。 因此,在当前目录中投放一个命名为 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> 的恶意程序不会再起作用。</p>
|
||
</div>
|
||
<p><em>stdin</em>, <em>stdout</em> 和 <em>stderr</em> 分别指定被运行的程序的标准输入、输出和标准错误的文件句柄。合法的值有 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a> , <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> , 一个存在的文件描述符(一个正整数),一个存在的 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a> 以及 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a> 表示应创建一个新的对子进程的管道。 <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> 表示使用特殊的 <a class="reference internal" href="os.html#os.devnull" title="os.devnull"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.devnull</span></code></a> 文件。使用默认的 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则不进行成定向;子进程的文件流将继承自父进程。另外, <em>stderr</em> 可设为 <a class="reference internal" href="#subprocess.STDOUT" title="subprocess.STDOUT"><code class="xref py py-data docutils literal notranslate"><span class="pre">STDOUT</span></code></a>,表示应用程序的标准错误数据应和标准输出一同捕获。</p>
|
||
<p>如果 <em>preexec_fn</em> 被设为一个可调用对象,此对象将在子进程刚创建时被调用。(仅 POSIX)</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p><em>preexec_fn</em> 形参在应用程序中存在多线程时是不安全的。子进程在调用前可能死锁。如果你必须使用它,保持警惕!最小化你调用的库的数量。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果你需要修改子进程环境,使用 <em>env</em> 形参而非在 <em>preexec_fn</em> 中进行。 <em>start_new_session</em> 形参可以代替之前常用的 <em>preexec_fn</em> 来在子进程中调用 os.setsid()。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span><em>preexec_fn</em> 形参在子解释器中已不再受支持。 在子解释器中使用此形参将引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>。 这个新限制可能会影响部署在 mod_wsgi, uWSGI 和其他嵌入式环境中的应用。</p>
|
||
</div>
|
||
<p>如果 <em>close_fds</em> 为真,所有文件描述符除了 <code class="xref py py-const docutils literal notranslate"><span class="pre">0</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">1</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">2</span></code> 之外都会在子进程执行前关闭。而当 <em>close_fds</em> 为假时,文件描述符遵守它们继承的标志,如 <a class="reference internal" href="os.html#fd-inheritance"><span class="std std-ref">文件描述符的继承</span></a> 所述。</p>
|
||
<p>在 Windows,如果 <em>close_fds</em> 为真, 则子进程不会继承任何句柄,除非在 <code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.IpAttributeList</span></code> 的 <code class="docutils literal notranslate"><span class="pre">handle_list</span></code> 的键中显式传递,或者通过标准句柄重定向传递。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span><em>close_fds</em> 的默认值已经从 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 修改为上述值。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>在 Windows,当重定向标准句柄时 <em>close_fds</em> 的默认值从 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 变为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。现在重定向标准句柄时有可能设置 <em>close_fds</em> 为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。(标准句柄指三个 stdio 的句柄)</p>
|
||
</div>
|
||
<p><em>pass_fds</em> 是一个可选的在父子进程间保持打开的文件描述符序列。提供任何 <em>pass_fds</em> 将强制 <em>close_fds</em> 为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。(仅 POSIX)</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>加入了 <em>pass_fds</em> 形参。</p>
|
||
</div>
|
||
<p>如果 <em>cwd</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,此函数在执行子进程前会将当前工作目录改为 <em>cwd</em>。 <em>cwd</em> 可以是一个字符串、字节串或 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">路径类对象</span></a> 。 特别地,当可执行文件的路径为相对路径时,此函数会相对于*cwd* 来查找 <em>executable</em> (或 <em>args</em> 中的第一个条目)。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>在 POSIX 上 <em>cwd</em> 形参接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>在 Windows 上 <em>cwd</em> 形参接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在 Windows 上 <em>cwd</em> 形参接受一个字节串对象。</p>
|
||
</div>
|
||
<p> 如果 <em>restore_signals</em> 为 true(默认值),则 Python 设置为 SIG_IGN 的所有信号将在 exec 之前的子进程中恢复为 SIG_DFL。目前,这包括 SIGPIPE ,SIGXFZ 和 SIGXFSZ 信号。 (仅 POSIX)</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span><em>restore_signals</em> 被加入。</p>
|
||
</div>
|
||
<p>如果 <em>start_new_session</em> 为 true,则 setsid() 系统调用将在子进程执行之前被执行。(仅 POSIX)</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span><em>start_new_session</em> 被添加。</p>
|
||
</div>
|
||
<p>如果 <em>env</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则必须为一个为新进程定义了环境变量的字典;这些用于替换继承的当前进程环境的默认行为。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果指定, <em>env</em> 必须提供所有被子进程需求的变量。在 Windows,为了运行一个 <a class="reference external" href="https://en.wikipedia.org/wiki/Side-by-Side_Assembly">side-by-side assembly</a> ,指定的 <em>env</em> <strong>必须</strong> 包含一个有效的 <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">SystemRoot</span></code>。</p>
|
||
</div>
|
||
<p>如果 <em>encoding</em> 或 <em>errors</em> 被指定,或者 <em>text</em> 为 true,则文件对象 <em>stdin</em>, <em>stdout</em> 和 <em>stderr</em> 将会以指定的编码和 <em>errors</em> 以文本模式打开,如同 <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">常用参数</span></a> 所述。 <em>universal_newlines</em> 参数等同于 <em>text</em> 并且提供向后兼容性。默认情况下,文件对象都以二进制模式打开。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.6 新版功能: </span><em>encoding</em> 和 <em>errors</em> 被添加。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能: </span><em>text</em> 作为 <em>universal_newlines</em> 的一个更具可读性的别名被添加。</p>
|
||
</div>
|
||
<p>如果给出, <em>startupinfo</em> 将是一个将被传递给底层的 <code class="docutils literal notranslate"><span class="pre">CreateProcess</span></code> 函数的 <a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><code class="xref py py-class docutils literal notranslate"><span class="pre">STARTUPINFO</span></code></a> 对象。 <em>creationflags</em>,如果给出,可以是一个或多个以下标志之一:</p>
|
||
<blockquote>
|
||
<div><ul class="simple">
|
||
<li><p><a class="reference internal" href="#subprocess.CREATE_NEW_CONSOLE" title="subprocess.CREATE_NEW_CONSOLE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_NEW_CONSOLE</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.CREATE_NEW_PROCESS_GROUP" title="subprocess.CREATE_NEW_PROCESS_GROUP"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.ABOVE_NORMAL_PRIORITY_CLASS" title="subprocess.ABOVE_NORMAL_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">ABOVE_NORMAL_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.BELOW_NORMAL_PRIORITY_CLASS" title="subprocess.BELOW_NORMAL_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">BELOW_NORMAL_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.HIGH_PRIORITY_CLASS" title="subprocess.HIGH_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">HIGH_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.IDLE_PRIORITY_CLASS" title="subprocess.IDLE_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">IDLE_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.NORMAL_PRIORITY_CLASS" title="subprocess.NORMAL_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">NORMAL_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.REALTIME_PRIORITY_CLASS" title="subprocess.REALTIME_PRIORITY_CLASS"><code class="xref py py-data docutils literal notranslate"><span class="pre">REALTIME_PRIORITY_CLASS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.CREATE_NO_WINDOW" title="subprocess.CREATE_NO_WINDOW"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_NO_WINDOW</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.DETACHED_PROCESS" title="subprocess.DETACHED_PROCESS"><code class="xref py py-data docutils literal notranslate"><span class="pre">DETACHED_PROCESS</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.CREATE_DEFAULT_ERROR_MODE" title="subprocess.CREATE_DEFAULT_ERROR_MODE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_DEFAULT_ERROR_MODE</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="#subprocess.CREATE_BREAKAWAY_FROM_JOB" title="subprocess.CREATE_BREAKAWAY_FROM_JOB"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_BREAKAWAY_FROM_JOB</span></code></a></p></li>
|
||
</ul>
|
||
</div></blockquote>
|
||
<p>Popen 对象支持通过 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句作为上下文管理器,在退出时关闭文件描述符并等待进程:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">"ifconfig"</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span> <span class="k">as</span> <span class="n">proc</span><span class="p">:</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">proc</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
|
||
</pre></div>
|
||
</div>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">subprocess.Popen</span></code>,附带参数 <code class="docutils literal notranslate"><span class="pre">executable</span></code>, <code class="docutils literal notranslate"><span class="pre">args</span></code>, <code class="docutils literal notranslate"><span class="pre">cwd</span></code>, <code class="docutils literal notranslate"><span class="pre">env</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了上下文管理器支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>现在,如果 Popen 析构时子进程仍然在运行,则析构器会发送一个 <a class="reference internal" href="exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> 警告。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在某些情况下 Popen 可以使用 <a class="reference internal" href="os.html#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.posix_spawn()</span></code></a> 以获得更好的性能。在适用于 Linux 的 Windows 子系统和 QEMU 用户模拟器上,使用 <a class="reference internal" href="os.html#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.posix_spawn()</span></code></a> 的 Popen 构造器不再会因找不到程序等错误而引发异常,而是上下级进程失败并返回一个非零的 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="exceptions">
|
||
<h3>异常<a class="headerlink" href="#exceptions" title="永久链接至标题">¶</a></h3>
|
||
<p>在子进程中抛出的异常,在新的进程开始执行前,将会被再次在父进程中抛出。</p>
|
||
<p>最常见的被抛出异常是 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。例如,当尝试执行一个不存在的文件时就会发生。应用程序需要为 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 异常做好准备。</p>
|
||
<p>如果 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 调用时有无效的参数,则一个 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> 将被抛出。</p>
|
||
<p><a class="reference internal" href="#subprocess.check_call" title="subprocess.check_call"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_call()</span></code></a> 与 <a class="reference internal" href="#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> 在调用的进程返回非零退出码时将抛出 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a>。</p>
|
||
<p>所有接受 <em>timeout</em> 形参的函数与方法,例如 <a class="reference internal" href="#subprocess.call" title="subprocess.call"><code class="xref py py-func docutils literal notranslate"><span class="pre">call()</span></code></a> 和 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a> 将会在进程退出前超时到期时抛出 <a class="reference internal" href="#subprocess.TimeoutExpired" title="subprocess.TimeoutExpired"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutExpired</span></code></a>。</p>
|
||
<p>此模块中定义的异常都继承自 <a class="reference internal" href="#subprocess.SubprocessError" title="subprocess.SubprocessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SubprocessError</span></code></a>。</p>
|
||
<blockquote>
|
||
<div><div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能: </span>基类 <a class="reference internal" href="#subprocess.SubprocessError" title="subprocess.SubprocessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SubprocessError</span></code></a> 被添加。</p>
|
||
</div>
|
||
</div></blockquote>
|
||
</section>
|
||
</section>
|
||
<section id="security-considerations">
|
||
<h2>安全考量<a class="headerlink" href="#security-considerations" title="永久链接至标题">¶</a></h2>
|
||
<p>Unlike some other popen functions, this library will not
|
||
implicitly choose to call a system shell. This means that all characters,
|
||
including shell metacharacters, can safely be passed to child processes.
|
||
If the shell is invoked explicitly, via <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, it is the application's
|
||
responsibility to ensure that all whitespace and metacharacters are
|
||
quoted appropriately to avoid
|
||
<a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>
|
||
vulnerabilities.</p>
|
||
<p>当使用 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, <a class="reference internal" href="shlex.html#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">shlex.quote()</span></code></a> 函数可以作为在将被用于构造 shell 指令的字符串中转义空白字符以及 shell 元字符的方案。</p>
|
||
<p>在 Windows 上,批处理文件 (<code class="file docutils literal notranslate"><span class="pre">*.bat</span></code> 或 <code class="file docutils literal notranslate"><span class="pre">*.cmd</span></code>) 可以在系统 shell 中通过操作系统调用来启动而忽略传给该库的参数。 这可能导致根据 shell 规则来解析参数,而没有任何 Python 添加的转义。 如果你想要附带来自不受信任源的参数启动批处理文件,请考虑传入 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 以允许 Python 转义特殊字符。 请参阅 <a class="reference external" href="https://github.com/python/cpython/issues/114539">gh-114539</a> 了解相关讨论。</p>
|
||
</section>
|
||
<section id="popen-objects">
|
||
<h2>Popen 对象<a class="headerlink" href="#popen-objects" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 类的实例拥有以下方法:</p>
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.poll">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">poll</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.poll" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>检查子进程是否已被终止。设置并返回 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性。否则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.wait">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">wait</code><span class="sig-paren">(</span><em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.wait" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等待子进程被终止。设置并返回 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性。</p>
|
||
<p>如果进程在 <em>timeout</em> 秒后未中断,抛出一个 <a class="reference internal" href="#subprocess.TimeoutExpired" title="subprocess.TimeoutExpired"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutExpired</span></code></a> 异常,可以安全地捕获此异常并重新等待。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>当 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 或者 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> 并且子进程产生了足以阻塞 OS 管道缓冲区接收更多数据的输出到管道时,将会发生死锁。当使用管道时用 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.communicate()</span></code></a> 来规避它。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>此函数使用了一个 busy loop (非阻塞调用以及短睡眠) 实现。使用 <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> 模块进行异步等待: 参阅 <a class="reference internal" href="asyncio-subprocess.html#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.create_subprocess_exec</span></code></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>timeout</em> 被添加</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.communicate">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">communicate</code><span class="sig-paren">(</span><em class="sig-param">input=None</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.communicate" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>与进程交互:将数据发送到 stdin。 从 stdout 和 stderr 读取数据,直到抵达文件结尾。 等待进程终止并设置 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性。 可选的 <em>input</em> 参数应为要发送到下级进程的数据,或者如果没有要发送到下级进程的数据则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 如果流是以文本模式打开的,则 <em>input</em> 必须为字符串。 在其他情况下,它必须为字节串。</p>
|
||
<p><a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> 返回一个 <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code> 元组。如果文件以文本模式打开则为字符串;否则字节。</p>
|
||
<p>注意如果你想要向进程的 stdin 传输数据,你需要通过 <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code> 创建此 Popen 对象。类似的,要从结果元组获取任何非 <code class="docutils literal notranslate"><span class="pre">None</span></code> 值,你同样需要设置 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 或者 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code>。</p>
|
||
<p>如果进程在 <em>timeout</em> 秒后未终止,一个 <a class="reference internal" href="#subprocess.TimeoutExpired" title="subprocess.TimeoutExpired"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutExpired</span></code></a> 异常将被抛出。捕获此异常并重新等待将不会丢失任何输出。</p>
|
||
<p>如果超时到期,子进程不会被杀死,所以为了正确清理一个行为良好的应用程序应该杀死子进程并完成通讯。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">proc</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="n">outs</span><span class="p">,</span> <span class="n">errs</span> <span class="o">=</span> <span class="n">proc</span><span class="o">.</span><span class="n">communicate</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
|
||
<span class="k">except</span> <span class="n">TimeoutExpired</span><span class="p">:</span>
|
||
<span class="n">proc</span><span class="o">.</span><span class="n">kill</span><span class="p">()</span>
|
||
<span class="n">outs</span><span class="p">,</span> <span class="n">errs</span> <span class="o">=</span> <span class="n">proc</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>内存里数据读取是缓冲的,所以如果数据尺寸过大或无限,不要使用此方法。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>timeout</em> 被添加</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.send_signal">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">send_signal</code><span class="sig-paren">(</span><em class="sig-param">signal</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.send_signal" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将信号 <em>signal</em> 发送给子进程。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>在 Windows, SIGTERM 是一个 <a class="reference internal" href="#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> 的别名。 CTRL_C_EVENT 和 CTRL_BREAK_EVENT 可以被发送给以包含 <code class="docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS</span></code> 的 <em>creationflags</em> 形参启动的进程。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.terminate">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">terminate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.terminate" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>停止子进程。 在 POSIX 操作系统上,此方法会发送 SIGTERM 给子进程。 在 Windows 上则会调用 Win32 API 函数 <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> 来停止子进程。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="subprocess.Popen.kill">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">kill</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.kill" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>杀死子进程。 在 POSIX 操作系统上,此函数会发送 SIGKILL 给子进程。 在 Windows 上 <a class="reference internal" href="#subprocess.Popen.kill" title="subprocess.Popen.kill"><code class="xref py py-meth docutils literal notranslate"><span class="pre">kill()</span></code></a> 则是 <a class="reference internal" href="#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> 的别名。</p>
|
||
</dd></dl>
|
||
|
||
<p>以下属性也是可用的:</p>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.args">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">args</code><a class="headerlink" href="#subprocess.Popen.args" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><em>args</em> 参数传递给 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> -- 一个程序参数的序列或者一个简单字符串。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.stdin">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">stdin</code><a class="headerlink" href="#subprocess.Popen.stdin" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>stdin</em> 参数为 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>,此属性是一个类似 <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 返回的可写的流对象。如果 <em>encoding</em> 或 <em>errors</em> 参数被指定或者 <em>universal_newlines</em> 参数为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则此流是一个文本流,否则是字节流。如果 <em>stdin</em> 参数非 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>, 此属性为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.stdout">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">stdout</code><a class="headerlink" href="#subprocess.Popen.stdout" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>stdout</em> 参数是 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>,此属性是一个类似 <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 返回的可读流。从流中读取子进程提供的输出。如果 <em>encoding</em> 或 <em>errors</em> 参数被指定或者 <em>universal_newlines</em> 参数为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,此流为文本流,否则为字节流。如果 <em>stdout</em> 参数非 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>,此属性为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.stderr">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">stderr</code><a class="headerlink" href="#subprocess.Popen.stderr" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>stderr</em> 参数是 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>,此属性是一个类似 <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 返回的可读流。从流中读取子进程提供的输出。如果 <em>encoding</em> 或 <em>errors</em> 参数被指定或者 <em>universal_newlines</em> 参数为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,此流为文本流,否则为字节流。如果 <em>stderr</em> 参数非 <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">PIPE</span></code></a>,此属性为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>使用 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> 而非 <a class="reference internal" href="#subprocess.Popen.stdin" title="subprocess.Popen.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">.stdin.write</span></code></a>, <a class="reference internal" href="#subprocess.Popen.stdout" title="subprocess.Popen.stdout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">.stdout.read</span></code></a> 或者 <a class="reference internal" href="#subprocess.Popen.stderr" title="subprocess.Popen.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">.stderr.read</span></code></a> 来避免由于任意其他 OS 管道缓冲区被子进程填满阻塞而导致的死锁。</p>
|
||
</div>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.pid">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">pid</code><a class="headerlink" href="#subprocess.Popen.pid" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>子进程的进程号。</p>
|
||
<p>注意如果你设置了 <em>shell</em> 参数为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则这是生成的子 shell 的进程号。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.Popen.returncode">
|
||
<code class="sig-prename descclassname">Popen.</code><code class="sig-name descname">returncode</code><a class="headerlink" href="#subprocess.Popen.returncode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>此进程的退出码,由 <a class="reference internal" href="#subprocess.Popen.poll" title="subprocess.Popen.poll"><code class="xref py py-meth docutils literal notranslate"><span class="pre">poll()</span></code></a> 和 <a class="reference internal" href="#subprocess.Popen.wait" title="subprocess.Popen.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> 设置(以及直接由 <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> 设置)。一个 <code class="docutils literal notranslate"><span class="pre">None</span></code> 值 表示此进程仍未结束。</p>
|
||
<p>一个负值 <code class="docutils literal notranslate"><span class="pre">-N</span></code> 表示子进程被信号 <code class="docutils literal notranslate"><span class="pre">N</span></code> 中断 (仅 POSIX).</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="windows-popen-helpers">
|
||
<h2>Windows Popen 助手<a class="headerlink" href="#windows-popen-helpers" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><code class="xref py py-class docutils literal notranslate"><span class="pre">STARTUPINFO</span></code></a> 类和以下常数仅在 Windows 有效。</p>
|
||
<dl class="class">
|
||
<dt id="subprocess.STARTUPINFO">
|
||
<em class="property">class </em><code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STARTUPINFO</code><span class="sig-paren">(</span><em class="sig-param">*</em>, <em class="sig-param">dwFlags=0</em>, <em class="sig-param">hStdInput=None</em>, <em class="sig-param">hStdOutput=None</em>, <em class="sig-param">hStdError=None</em>, <em class="sig-param">wShowWindow=0</em>, <em class="sig-param">lpAttributeList=None</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.STARTUPINFO" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 创建时部分支持 Windows 的 <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms686331(v=vs.85).aspx">STARTUPINFO</a> 结构。接下来的属性仅能通过关键词参数设置。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>仅关键词参数支持被加入。</p>
|
||
</div>
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.dwFlags">
|
||
<code class="sig-name descname">dwFlags</code><a class="headerlink" href="#subprocess.STARTUPINFO.dwFlags" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>一个位字段,用于确定进程在创建窗口时是否使用某些 <a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><code class="xref py py-class docutils literal notranslate"><span class="pre">STARTUPINFO</span></code></a> 属性。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">si</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTUPINFO</span><span class="p">()</span>
|
||
<span class="n">si</span><span class="o">.</span><span class="n">dwFlags</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTF_USESTDHANDLES</span> <span class="o">|</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTF_USESHOWWINDOW</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.hStdInput">
|
||
<code class="sig-name descname">hStdInput</code><a class="headerlink" href="#subprocess.STARTUPINFO.hStdInput" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dwFlags</span></code></a> 被指定为 <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><code class="xref py py-data docutils literal notranslate"><span class="pre">STARTF_USESTDHANDLES</span></code></a>,则此属性是进程的标准输入句柄,如果 <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><code class="xref py py-data docutils literal notranslate"><span class="pre">STARTF_USESTDHANDLES</span></code></a> 未指定,则默认的标准输入是键盘缓冲区。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.hStdOutput">
|
||
<code class="sig-name descname">hStdOutput</code><a class="headerlink" href="#subprocess.STARTUPINFO.hStdOutput" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dwFlags</span></code></a> 被指定为 <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><code class="xref py py-data docutils literal notranslate"><span class="pre">STARTF_USESTDHANDLES</span></code></a>,则此属性是进程的标准输出句柄。除此之外,此此属性将被忽略并且默认标准输出是控制台窗口缓冲区。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.hStdError">
|
||
<code class="sig-name descname">hStdError</code><a class="headerlink" href="#subprocess.STARTUPINFO.hStdError" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dwFlags</span></code></a> 被指定为 <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><code class="xref py py-data docutils literal notranslate"><span class="pre">STARTF_USESTDHANDLES</span></code></a>,则此属性是进程的标准错误句柄。除此之外,此属性将被忽略并且默认标准错误为控制台窗口的缓冲区。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.wShowWindow">
|
||
<code class="sig-name descname">wShowWindow</code><a class="headerlink" href="#subprocess.STARTUPINFO.wShowWindow" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dwFlags</span></code></a> 指定了 <a class="reference internal" href="#subprocess.STARTF_USESHOWWINDOW" title="subprocess.STARTF_USESHOWWINDOW"><code class="xref py py-data docutils literal notranslate"><span class="pre">STARTF_USESHOWWINDOW</span></code></a>,此属性可为能被指定为 函数 <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx">ShowWindow</a> 的nCmdShow 的形参的任意值,除了 <code class="docutils literal notranslate"><span class="pre">SW_SHOWDEFAULT</span></code>。如此之外,此属性被忽略。</p>
|
||
<p><a class="reference internal" href="#subprocess.SW_HIDE" title="subprocess.SW_HIDE"><code class="xref py py-data docutils literal notranslate"><span class="pre">SW_HIDE</span></code></a> 被提供给此属性。它在 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 由 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 调用时使用。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="subprocess.STARTUPINFO.lpAttributeList">
|
||
<code class="sig-name descname">lpAttributeList</code><a class="headerlink" href="#subprocess.STARTUPINFO.lpAttributeList" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><code class="docutils literal notranslate"><span class="pre">STARTUPINFOEX</span></code> 给出的用于进程创建的额外属性字典,参阅 <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms686880(v=vs.85).aspx">UpdateProcThreadAttribute</a>。</p>
|
||
<p>支持的属性:</p>
|
||
<dl>
|
||
<dt><strong>handle_list</strong></dt><dd><p>将被继承的句柄的序列。如果非空, <em>close_fds</em> 必须为 true。</p>
|
||
<p>当传递给 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 构造函数时,这些句柄必须暂时地能被 <a class="reference internal" href="os.html#os.set_handle_inheritable" title="os.set_handle_inheritable"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.set_handle_inheritable()</span></code></a> 继承,否则 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSError</span></code></a> 将以 Windows error <code class="docutils literal notranslate"><span class="pre">ERROR_INVALID_PARAMETER</span></code> (87) 抛出。</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>在多线程进程中,请谨慎使用,以便在将此功能与对继承所有句柄的其他进程创建函数——例如 <a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a> 的并发调用——相结合时,避免泄漏标记为可继承的句柄。这也应用于临时性创建可继承句柄的标准句柄重定向。</p>
|
||
</div>
|
||
</dd>
|
||
</dl>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
<section id="windows-constants">
|
||
<h3>Windows 常数<a class="headerlink" href="#windows-constants" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块曝出以下常数。</p>
|
||
<dl class="data">
|
||
<dt id="subprocess.STD_INPUT_HANDLE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STD_INPUT_HANDLE</code><a class="headerlink" href="#subprocess.STD_INPUT_HANDLE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>标准输入设备,这是控制台输入缓冲区 <code class="docutils literal notranslate"><span class="pre">CONIN$</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.STD_OUTPUT_HANDLE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STD_OUTPUT_HANDLE</code><a class="headerlink" href="#subprocess.STD_OUTPUT_HANDLE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>标准输出设备。最初,这是活动控制台屏幕缓冲区 <code class="docutils literal notranslate"><span class="pre">CONOUT$</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.STD_ERROR_HANDLE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STD_ERROR_HANDLE</code><a class="headerlink" href="#subprocess.STD_ERROR_HANDLE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>标准错误设备。最初,这是活动控制台屏幕缓冲区 <code class="docutils literal notranslate"><span class="pre">CONOUT$</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.SW_HIDE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">SW_HIDE</code><a class="headerlink" href="#subprocess.SW_HIDE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>隐藏窗口。另一个窗口将被激活。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.STARTF_USESTDHANDLES">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STARTF_USESTDHANDLES</code><a class="headerlink" href="#subprocess.STARTF_USESTDHANDLES" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明 <a class="reference internal" href="#subprocess.STARTUPINFO.hStdInput" title="subprocess.STARTUPINFO.hStdInput"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.hStdInput</span></code></a>, <a class="reference internal" href="#subprocess.STARTUPINFO.hStdOutput" title="subprocess.STARTUPINFO.hStdOutput"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.hStdOutput</span></code></a> 和 <a class="reference internal" href="#subprocess.STARTUPINFO.hStdError" title="subprocess.STARTUPINFO.hStdError"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.hStdError</span></code></a> 属性包含额外的信息。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.STARTF_USESHOWWINDOW">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">STARTF_USESHOWWINDOW</code><a class="headerlink" href="#subprocess.STARTF_USESHOWWINDOW" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明 <a class="reference internal" href="#subprocess.STARTUPINFO.wShowWindow" title="subprocess.STARTUPINFO.wShowWindow"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.wShowWindow</span></code></a> 属性包含额外的信息。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.CREATE_NEW_CONSOLE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CREATE_NEW_CONSOLE</code><a class="headerlink" href="#subprocess.CREATE_NEW_CONSOLE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>新的进程将有新的控制台,而不是继承父进程的(默认)控制台。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.CREATE_NEW_PROCESS_GROUP">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CREATE_NEW_PROCESS_GROUP</code><a class="headerlink" href="#subprocess.CREATE_NEW_PROCESS_GROUP" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明将创建一个新的进程组的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。 这个旗标对于在子进程上使用 <a class="reference internal" href="os.html#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.kill()</span></code></a> 来说是必须的。</p>
|
||
<p>如果指定了 <a class="reference internal" href="#subprocess.CREATE_NEW_CONSOLE" title="subprocess.CREATE_NEW_CONSOLE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CREATE_NEW_CONSOLE</span></code></a> 则这个旗标会被忽略。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.ABOVE_NORMAL_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">ABOVE_NORMAL_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.ABOVE_NORMAL_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有高于平均的优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.BELOW_NORMAL_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">BELOW_NORMAL_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.BELOW_NORMAL_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有低于平均的优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.HIGH_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">HIGH_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.HIGH_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有高优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.IDLE_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">IDLE_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.IDLE_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有空闲(最低)优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.NORMAL_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">NORMAL_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.NORMAL_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有正常(默认)优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.REALTIME_PRIORITY_CLASS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">REALTIME_PRIORITY_CLASS</code><a class="headerlink" href="#subprocess.REALTIME_PRIORITY_CLASS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于指明一个新进程将具有实时优先级的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。 你应当几乎永远不使用 REALTIME_PRIORITY_CLASS,因为这会中断管理鼠标输入、键盘输入以及后台磁盘刷新的系统线程。 这个类只适用于直接与硬件“对话”,或者执行短暂任务具有受限中断的应用。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.CREATE_NO_WINDOW">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CREATE_NO_WINDOW</code><a class="headerlink" href="#subprocess.CREATE_NO_WINDOW" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明一个新进程将不会创建窗口的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.DETACHED_PROCESS">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">DETACHED_PROCESS</code><a class="headerlink" href="#subprocess.DETACHED_PROCESS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明一个新进程将不会继承其父控制台的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。 这个值不能与 CREATE_NEW_CONSOLE 一同使用。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.CREATE_DEFAULT_ERROR_MODE">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CREATE_DEFAULT_ERROR_MODE</code><a class="headerlink" href="#subprocess.CREATE_DEFAULT_ERROR_MODE" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明一个新进程不会继承调用方进程的错误模式的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。 新进程会转为采用默认的错误模式。 这个特性特别适用于运行时禁用硬错误的多线程 shell 应用。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="subprocess.CREATE_BREAKAWAY_FROM_JOB">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">CREATE_BREAKAWAY_FROM_JOB</code><a class="headerlink" href="#subprocess.CREATE_BREAKAWAY_FROM_JOB" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明一个新进程不会关联到任务的 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> <code class="docutils literal notranslate"><span class="pre">creationflags</span></code> 形参。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
</section>
|
||
<section id="older-high-level-api">
|
||
<span id="call-function-trio"></span><h2>较旧的高阶 API<a class="headerlink" href="#older-high-level-api" title="永久链接至标题">¶</a></h2>
|
||
<p>在 Python 3.5 之前,这三个函数组成了 subprocess 的高阶 API。 现在你可以在许多情况下使用 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a>,但有大量现在代码仍会调用这些函数。</p>
|
||
<dl class="function">
|
||
<dt id="subprocess.call">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">call</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">*</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">shell=False</em>, <em class="sig-param">cwd=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">**other_popen_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.call" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>运行由 <em>args</em> 所描述的命令。 等待命令完成,然后返回 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性。</p>
|
||
<p>需要捕获 stdout 或 stderr 的代码应当改用 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">run</span><span class="p">(</span><span class="o">...</span><span class="p">)</span><span class="o">.</span><span class="n">returncode</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>要屏蔽 stdout 或 stderr,可提供 <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> 这个值。</p>
|
||
<p>上面显示的参数只是常见的一些。 完整的函数签名与 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 构造器的相同 —— 此函数会将所提供的 <em>timeout</em> 之外的全部参数直接传递给目标接口。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>请不要在此函数中使用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 或 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code>。 如果子进程向管道生成了足以填满 OS 管理缓冲区的输出而管道还未被读取时它将会阻塞。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>timeout</em> 被添加</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>针对 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 改变的 Windows shell 搜索顺序。 当前目录和 <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> 会被替换为 <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>。 因此,在当前目录中投放一个命名为 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> 的恶意程序不会再起作用。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="subprocess.check_call">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">check_call</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">*</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">shell=False</em>, <em class="sig-param">cwd=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">**other_popen_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.check_call" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>附带参数运行命令。 等待命令完成。 如果返回码为零则正常返回,否则引发 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a>。 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a> 对象将在 <a class="reference internal" href="#subprocess.CalledProcessError.returncode" title="subprocess.CalledProcessError.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性中保存返回码。</p>
|
||
<p>需要捕获 stdout 或 stderr 的代码应当改用 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">run</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>要屏蔽 stdout 或 stderr,可提供 <a class="reference internal" href="#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> 这个值。</p>
|
||
<p>上面显示的参数只是常见的一些。 完整的函数签名与 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 构造器的相同 —— 此函数会将所提供的 <em>timeout</em> 之外的全部参数直接传递给目标接口。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>请不要在此函数中使用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 或 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code>。 如果子进程向管道生成了足以填满 OS 管理缓冲区的输出而管道还未被读取时它将会阻塞。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>timeout</em> 被添加</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>针对 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 改变的 Windows shell 搜索顺序。 当前目录和 <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> 会被替换为 <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>。 因此,在当前目录中投放一个命名为 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> 的恶意程序不会再起作用。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="subprocess.check_output">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">check_output</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">*</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">shell=False</em>, <em class="sig-param">cwd=None</em>, <em class="sig-param">encoding=None</em>, <em class="sig-param">errors=None</em>, <em class="sig-param">universal_newlines=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">text=None</em>, <em class="sig-param">**other_popen_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.check_output" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>附带参数运行命令并返回其输出。</p>
|
||
<p>如果返回码非零则会引发 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a>。 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a> 对象将在 <a class="reference internal" href="#subprocess.CalledProcessError.returncode" title="subprocess.CalledProcessError.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性中保存返回码并在 <a class="reference internal" href="#subprocess.CalledProcessError.output" title="subprocess.CalledProcessError.output"><code class="xref py py-attr docutils literal notranslate"><span class="pre">output</span></code></a> 属性中保存所有输出。</p>
|
||
<p>这相当于:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">run</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span><span class="o">.</span><span class="n">stdout</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>上面显示的参数只是常见的一些。 完整的函数签名与 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 的大致相同 —— 大部分参数会通过该接口直接传递。 存在一个与 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 行为不同的 API 差异:传递 <code class="docutils literal notranslate"><span class="pre">input=None</span></code> 的行为将与 <code class="docutils literal notranslate"><span class="pre">input=b''</span></code> (或 <code class="docutils literal notranslate"><span class="pre">input=''</span></code>,具体取决于其他参数) 一样而不是使用父对象的标准输入文件处理。</p>
|
||
<p>默认情况下,此函数将把数据返回为已编码的字节串。 输出数据的实际编码格式将取决于发起调用的命令,因此解码为文本的操作往往需要在应用程序层级上进行处理。</p>
|
||
<p>此行为可以通过设置 <em>text</em>, <em>encoding</em>, <em>errors</em> 或将 <em>universal_newlines</em> 设为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 来重载,具体描述见 <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">常用参数</span></a> 和 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a>。</p>
|
||
<p>要在结果中同时捕获标准错误,请使用 <code class="docutils literal notranslate"><span class="pre">stderr=subprocess.STDOUT</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">(</span>
|
||
<span class="gp">... </span> <span class="s2">"ls non_existent_file; exit 0"</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">STDOUT</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="go">'ls: non_existent_file: No such file or directory\n'</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.1 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>timeout</em> 被添加</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>增加了对 <em>input</em> 关键字参数的支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>增加了 <em>encoding</em> 和 <em>errors</em>。 详情参见 <a class="reference internal" href="#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a>。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能: </span><em>text</em> 作为 <em>universal_newlines</em> 的一个更具可读性的别名被添加。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>针对 <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> 改变的 Windows shell 搜索顺序。 当前目录和 <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> 会被替换为 <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>。 因此,在当前目录中投放一个命名为 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> 的恶意程序不会再起作用。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="replacing-older-functions-with-the-subprocess-module">
|
||
<span id="subprocess-replacements"></span><h2>使用 <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块替换旧函数<a class="headerlink" href="#replacing-older-functions-with-the-subprocess-module" title="永久链接至标题">¶</a></h2>
|
||
<p>在这一节中,"a 改为 b" 意味着 b 可以被用作 a 的替代。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>在这一节中的所有 "a" 函数会在找不到被执行的程序时(差不多)静默地失败;"b" 替代函数则会改为引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。</p>
|
||
<p>此外,在使用 <a class="reference internal" href="#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> 时如果替代函数所请求的操作产生了非零返回值则将失败并引发 <a class="reference internal" href="#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a>。 操作的输出仍能以所引发异常的 <a class="reference internal" href="#subprocess.CalledProcessError.output" title="subprocess.CalledProcessError.output"><code class="xref py py-attr docutils literal notranslate"><span class="pre">output</span></code></a> 属性的方式被访问。</p>
|
||
</div>
|
||
<p>在下列例子中,我们假定相关的函数都已从 <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> 模块中导入了。</p>
|
||
<section id="replacing-bin-sh-shell-command-substitution">
|
||
<h3>替代 <strong class="program">/bin/sh</strong> shell 命令替换<a class="headerlink" href="#replacing-bin-sh-shell-command-substitution" title="永久链接至标题">¶</a></h3>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nv">output</span><span class="o">=</span><span class="k">$(</span>mycmd<span class="w"> </span>myarg<span class="k">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>改为:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">output</span> <span class="o">=</span> <span class="n">check_output</span><span class="p">([</span><span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="replacing-shell-pipeline">
|
||
<h3>替代 shell 管道<a class="headerlink" href="#replacing-shell-pipeline" title="永久链接至标题">¶</a></h3>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nv">output</span><span class="o">=</span><span class="k">$(</span>dmesg<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>hda<span class="k">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>改为:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">p1</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">"dmesg"</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
|
||
<span class="n">p2</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">"grep"</span><span class="p">,</span> <span class="s2">"hda"</span><span class="p">],</span> <span class="n">stdin</span><span class="o">=</span><span class="n">p1</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
|
||
<span class="n">p1</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="c1"># Allow p1 to receive a SIGPIPE if p2 exits.</span>
|
||
<span class="n">output</span> <span class="o">=</span> <span class="n">p2</span><span class="o">.</span><span class="n">communicate</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>启动 p2 之后再执行 <code class="docutils literal notranslate"><span class="pre">p1.stdout.close()</span></code> 调用很重要,这是为了让 p1 能在 p2 先于 p1 退出时接收到 SIGPIPE。</p>
|
||
<p>另外,对于受信任的输入,shell 本身的管道支持仍然可被直接使用:</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nv">output</span><span class="o">=</span><span class="k">$(</span>dmesg<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>hda<span class="k">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>改为:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">output</span><span class="o">=</span><span class="n">check_output</span><span class="p">(</span><span class="s2">"dmesg | grep hda"</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="replacing-os-system">
|
||
<h3>替代 <a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a><a class="headerlink" href="#replacing-os-system" title="永久链接至标题">¶</a></h3>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sts</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s2">"mycmd"</span> <span class="o">+</span> <span class="s2">" myarg"</span><span class="p">)</span>
|
||
<span class="c1"># becomes</span>
|
||
<span class="n">sts</span> <span class="o">=</span> <span class="n">call</span><span class="p">(</span><span class="s2">"mycmd"</span> <span class="o">+</span> <span class="s2">" myarg"</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>注释:</p>
|
||
<ul class="simple">
|
||
<li><p>通过 shell 来调用程序通常是不必要的。</p></li>
|
||
</ul>
|
||
<p>一个更现实的例子如下所示:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
|
||
<span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">(</span><span class="s2">"mycmd"</span> <span class="o">+</span> <span class="s2">" myarg"</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="k">if</span> <span class="n">retcode</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Child was terminated by signal"</span><span class="p">,</span> <span class="o">-</span><span class="n">retcode</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Child returned"</span><span class="p">,</span> <span class="n">retcode</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Execution failed:"</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="replacing-the-os-spawn-family">
|
||
<h3>替代 <a class="reference internal" href="os.html#os.spawnl" title="os.spawnl"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawn</span></code></a> 函数族<a class="headerlink" href="#replacing-the-os-spawn-family" title="永久链接至标题">¶</a></h3>
|
||
<p>P_NOWAIT 示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">pid</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">])</span><span class="o">.</span><span class="n">pid</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>P_WAIT 示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">retcode</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_WAIT</span><span class="p">,</span> <span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">([</span><span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Vector 示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">spawnvp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">Popen</span><span class="p">([</span><span class="n">path</span><span class="p">]</span> <span class="o">+</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Environment 示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">spawnlpe</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">Popen</span><span class="p">([</span><span class="s2">"/bin/mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">],</span> <span class="n">env</span><span class="o">=</span><span class="p">{</span><span class="s2">"PATH"</span><span class="p">:</span> <span class="s2">"/usr/bin"</span><span class="p">})</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="replacing-os-popen-os-popen2-os-popen3">
|
||
<h3>替代 <a class="reference internal" href="os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen2()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen3()</span></code><a class="headerlink" href="#replacing-os-popen-os-popen2-os-popen3" title="永久链接至标题">¶</a></h3>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen2</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
|
||
<span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span>
|
||
<span class="n">child_stdout</span><span class="p">,</span>
|
||
<span class="n">child_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen3</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
|
||
<span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span>
|
||
<span class="n">child_stdout</span><span class="p">,</span>
|
||
<span class="n">child_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout_and_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen4</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
|
||
<span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">STDOUT</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout_and_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>返回码以如下方式处理转写:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pipe</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span>
|
||
<span class="o">...</span>
|
||
<span class="n">rc</span> <span class="o">=</span> <span class="n">pipe</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||
<span class="k">if</span> <span class="n">rc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">rc</span> <span class="o">>></span> <span class="mi">8</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"There were some errors"</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">process</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
|
||
<span class="o">...</span>
|
||
<span class="n">process</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||
<span class="k">if</span> <span class="n">process</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"There were some errors"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="replacing-functions-from-the-popen2-module">
|
||
<h3>来自 <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> 模块的替代函数<a class="headerlink" href="#replacing-functions-from-the-popen2-module" title="永久链接至标题">¶</a></h3>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果 popen2 函数的 cmd 参数是一个字符串,命令会通过 /bin/sh 来执行。 如果是一个列表,命令会被直接执行。</p>
|
||
</div>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="n">popen2</span><span class="o">.</span><span class="n">popen2</span><span class="p">(</span><span class="s2">"somestring"</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="s2">"somestring"</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
|
||
<span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="n">popen2</span><span class="o">.</span><span class="n">popen2</span><span class="p">([</span><span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">],</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
|
||
<span class="o">==></span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">"mycmd"</span><span class="p">,</span> <span class="s2">"myarg"</span><span class="p">],</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
|
||
<span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">popen2.Popen3</span></code> 和 <code class="xref py py-class docutils literal notranslate"><span class="pre">popen2.Popen4</span></code> 基本上类似于 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a>,不同之处在于:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 如果执行失败会引发一个异常。</p></li>
|
||
<li><p><em>capturestderr</em> 参数被替换为 <em>stderr</em> 参数。</p></li>
|
||
<li><p>必须指定 <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code> 和 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code>。</p></li>
|
||
<li><p>popen2 默认会关闭所有文件描述符,但对于 <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> 你必须指明 <code class="docutils literal notranslate"><span class="pre">close_fds=True</span></code> 以才能在所有平台或较旧的 Python 版本中确保此行为。</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="legacy-shell-invocation-functions">
|
||
<h2>旧式的 Shell 发起函数<a class="headerlink" href="#legacy-shell-invocation-functions" title="永久链接至标题">¶</a></h2>
|
||
<p>此模块还提供了以下来自 2.x <code class="docutils literal notranslate"><span class="pre">commands</span></code> 模块的旧版函数。 这些操作会隐式地发起调用系统 shell 并且上文所描述的有关安全与异常处理一致性保证都不适用于这些函数。</p>
|
||
<dl class="function">
|
||
<dt id="subprocess.getstatusoutput">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">getstatusoutput</code><span class="sig-paren">(</span><em class="sig-param">cmd</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.getstatusoutput" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回在 shell 中执行 <em>cmd</em> 产生的 <code class="docutils literal notranslate"><span class="pre">(exitcode,</span> <span class="pre">output)</span></code>。</p>
|
||
<p>在 shell 中以 <code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.check_output()</span></code> 执行字符串 <em>cmd</em> 并返回一个 2 元组 <code class="docutils literal notranslate"><span class="pre">(exitcode,</span> <span class="pre">output)</span></code>。 会使用当前区域设置的编码格式;请参阅 <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">常用参数</span></a> 中的说明来了解详情。</p>
|
||
<p>末尾的一个换行符会从输出中被去除。 命令的退出码可被解读为子进程的返回码。 例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">'ls /bin/ls'</span><span class="p">)</span>
|
||
<span class="go">(0, '/bin/ls')</span>
|
||
<span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">'cat /bin/junk'</span><span class="p">)</span>
|
||
<span class="go">(1, 'cat: /bin/junk: No such file or directory')</span>
|
||
<span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">'/bin/junk'</span><span class="p">)</span>
|
||
<span class="go">(127, 'sh: /bin/junk: not found')</span>
|
||
<span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">'/bin/kill $$'</span><span class="p">)</span>
|
||
<span class="go">(-15, '')</span>
|
||
</pre></div>
|
||
</div>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: POSIX 和 Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3.4 版更改: </span>添加了 Windows 支持。</p>
|
||
<p>此函数现在返回 (exitcode, output) 而不是像 Python 3.3.3 及更早的版本那样返回 (status, output)。 exitcode 的值与 <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 相同。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="subprocess.getoutput">
|
||
<code class="sig-prename descclassname">subprocess.</code><code class="sig-name descname">getoutput</code><span class="sig-paren">(</span><em class="sig-param">cmd</em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.getoutput" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回在 shell 中执行 <em>cmd</em> 产生的输出(stdout 和 stderr)。</p>
|
||
<p>类似于 <a class="reference internal" href="#subprocess.getstatusoutput" title="subprocess.getstatusoutput"><code class="xref py py-func docutils literal notranslate"><span class="pre">getstatusoutput()</span></code></a>,但退出码会被忽略并且返回值为包含命令输出的字符串。 例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getoutput</span><span class="p">(</span><span class="s1">'ls /bin/ls'</span><span class="p">)</span>
|
||
<span class="go">'/bin/ls'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: POSIX 和 Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3.4 版更改: </span>添加了 Windows 支持</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="notes">
|
||
<h2>备注<a class="headerlink" href="#notes" title="永久链接至标题">¶</a></h2>
|
||
<section id="converting-an-argument-sequence-to-a-string-on-windows">
|
||
<span id="converting-argument-sequence"></span><h3>在 Windows 上将参数列表转换为一个字符串<a class="headerlink" href="#converting-an-argument-sequence-to-a-string-on-windows" title="永久链接至标题">¶</a></h3>
|
||
<p>在 Windows 上,<em>args</em> 序列会被转换为可使用以下规则来解析的字符串(对应于 MS C 运行时所使用的规则):</p>
|
||
<ol class="arabic simple">
|
||
<li><p>参数以空白符分隔,即空格符或制表符。</p></li>
|
||
<li><p>用双引号标示的字符串会被解读为单个参数,而不再考虑其中的空白符。 一个参数可以嵌套用引号标示的字符串。</p></li>
|
||
<li><p>带有一个反斜杠前缀的双引号会被解读为双引号字面值。</p></li>
|
||
<li><p>反斜杠会按字面值解读,除非它是作为双引号的前缀。</p></li>
|
||
<li><p>如果反斜杠被作为双引号的前缀,则每个反斜杠对会被解读为一个反斜杠字面值。 如果反斜杠数量为奇数,则最后一个反斜杠会如规则 3 所描述的那样转义下一个双引号。</p></li>
|
||
</ol>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt><a class="reference internal" href="shlex.html#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a></dt><dd><p>此模块提供了用于解析和转义命令行的函数。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> --- 子进程管理</a><ul>
|
||
<li><a class="reference internal" href="#using-the-subprocess-module">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> 模块</a><ul>
|
||
<li><a class="reference internal" href="#frequently-used-arguments">常用参数</a></li>
|
||
<li><a class="reference internal" href="#popen-constructor">Popen 构造函数</a></li>
|
||
<li><a class="reference internal" href="#exceptions">异常</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#security-considerations">安全考量</a></li>
|
||
<li><a class="reference internal" href="#popen-objects">Popen 对象</a></li>
|
||
<li><a class="reference internal" href="#windows-popen-helpers">Windows Popen 助手</a><ul>
|
||
<li><a class="reference internal" href="#windows-constants">Windows 常数</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#older-high-level-api">较旧的高阶 API</a></li>
|
||
<li><a class="reference internal" href="#replacing-older-functions-with-the-subprocess-module">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> 模块替换旧函数</a><ul>
|
||
<li><a class="reference internal" href="#replacing-bin-sh-shell-command-substitution">替代 <strong class="program">/bin/sh</strong> shell 命令替换</a></li>
|
||
<li><a class="reference internal" href="#replacing-shell-pipeline">替代 shell 管道</a></li>
|
||
<li><a class="reference internal" href="#replacing-os-system">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a></li>
|
||
<li><a class="reference internal" href="#replacing-the-os-spawn-family">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawn</span></code> 函数族</a></li>
|
||
<li><a class="reference internal" href="#replacing-os-popen-os-popen2-os-popen3">替代 <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen2()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen3()</span></code></a></li>
|
||
<li><a class="reference internal" href="#replacing-functions-from-the-popen2-module">来自 <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> 模块的替代函数</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#legacy-shell-invocation-functions">旧式的 Shell 发起函数</a></li>
|
||
<li><a class="reference internal" href="#notes">备注</a><ul>
|
||
<li><a class="reference internal" href="#converting-an-argument-sequence-to-a-string-on-windows">在 Windows 上将参数列表转换为一个字符串</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="concurrent.futures.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> --- 启动并行任务</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="sched.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> --- 事件调度器</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/subprocess.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="sched.html" title="sched --- 事件调度器"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="concurrent.futures.html" title="concurrent.futures --- 启动并行任务"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="concurrency.html" >并发执行</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |