gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2010 lines
209KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="subprocess — Subprocess management" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/subprocess.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>subprocess — Subprocess management &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="sched — Event scheduler" href="sched.html" />
  33. <link rel="prev" title="concurrent.futures — Launching parallel tasks" href="concurrent.futures.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/subprocess.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a><ul>
  86. <li><a class="reference internal" href="#using-the-subprocess-module">Using the <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> Module</a><ul>
  87. <li><a class="reference internal" href="#frequently-used-arguments">Frequently Used Arguments</a></li>
  88. <li><a class="reference internal" href="#popen-constructor">Popen Constructor</a></li>
  89. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#security-considerations">Security Considerations</a></li>
  93. <li><a class="reference internal" href="#popen-objects">Popen Objects</a></li>
  94. <li><a class="reference internal" href="#windows-popen-helpers">Windows Popen Helpers</a><ul>
  95. <li><a class="reference internal" href="#windows-constants">Windows Constants</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#older-high-level-api">Older high-level API</a></li>
  99. <li><a class="reference internal" href="#replacing-older-functions-with-the-subprocess-module">Replacing Older Functions with the <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> Module</a><ul>
  100. <li><a class="reference internal" href="#replacing-bin-sh-shell-command-substitution">Replacing <strong class="program">/bin/sh</strong> shell command substitution</a></li>
  101. <li><a class="reference internal" href="#replacing-shell-pipeline">Replacing shell pipeline</a></li>
  102. <li><a class="reference internal" href="#replacing-os-system">Replacing <code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a></li>
  103. <li><a class="reference internal" href="#replacing-the-os-spawn-family">Replacing the <code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawn</span></code> family</a></li>
  104. <li><a class="reference internal" href="#replacing-os-popen-os-popen2-os-popen3">Replacing <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>
  105. <li><a class="reference internal" href="#replacing-functions-from-the-popen2-module">Replacing functions from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> module</a></li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#legacy-shell-invocation-functions">Legacy Shell Invocation Functions</a></li>
  109. <li><a class="reference internal" href="#notes">Notes</a><ul>
  110. <li><a class="reference internal" href="#converting-an-argument-sequence-to-a-string-on-windows">Converting an argument sequence to a string on Windows</a></li>
  111. <li><a class="reference internal" href="#disabling-use-of-vfork-or-posix-spawn">Disabling use of <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> or <code class="docutils literal notranslate"><span class="pre">posix_spawn()</span></code></a></li>
  112. </ul>
  113. </li>
  114. </ul>
  115. </li>
  116. </ul>
  117. </div>
  118. <div>
  119. <h4>Previous topic</h4>
  120. <p class="topless"><a href="concurrent.futures.html"
  121. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a></p>
  122. </div>
  123. <div>
  124. <h4>Next topic</h4>
  125. <p class="topless"><a href="sched.html"
  126. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> — Event scheduler</a></p>
  127. </div>
  128. <div role="note" aria-label="source link">
  129. <h3>This Page</h3>
  130. <ul class="this-page-menu">
  131. <li><a href="../bugs.html">Report a Bug</a></li>
  132. <li>
  133. <a href="https://github.com/python/cpython/blob/main/Doc/library/subprocess.rst"
  134. rel="nofollow">Show Source
  135. </a>
  136. </li>
  137. </ul>
  138. </div>
  139. </nav>
  140. </div>
  141. </div>
  142. <div class="related" role="navigation" aria-label="related navigation">
  143. <h3>Navigation</h3>
  144. <ul>
  145. <li class="right" style="margin-right: 10px">
  146. <a href="../genindex.html" title="General Index"
  147. accesskey="I">index</a></li>
  148. <li class="right" >
  149. <a href="../py-modindex.html" title="Python Module Index"
  150. >modules</a> |</li>
  151. <li class="right" >
  152. <a href="sched.html" title="sched — Event scheduler"
  153. accesskey="N">next</a> |</li>
  154. <li class="right" >
  155. <a href="concurrent.futures.html" title="concurrent.futures — Launching parallel tasks"
  156. accesskey="P">previous</a> |</li>
  157. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  158. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  159. <li class="switchers">
  160. <div class="language_switcher_placeholder"></div>
  161. <div class="version_switcher_placeholder"></div>
  162. </li>
  163. <li>
  164. </li>
  165. <li id="cpython-language-and-version">
  166. <a href="../index.html">3.12.3 Documentation</a> &#187;
  167. </li>
  168. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  169. <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> &#187;</li>
  170. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a></li>
  171. <li class="right">
  172. <div class="inline-search" role="search">
  173. <form class="inline-search" action="../search.html" method="get">
  174. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  175. <input type="submit" value="Go" />
  176. </form>
  177. </div>
  178. |
  179. </li>
  180. <li class="right">
  181. <label class="theme-selector-label">
  182. Theme
  183. <select class="theme-selector" oninput="activateTheme(this.value)">
  184. <option value="auto" selected>Auto</option>
  185. <option value="light">Light</option>
  186. <option value="dark">Dark</option>
  187. </select>
  188. </label> |</li>
  189. </ul>
  190. </div>
  191. <div class="document">
  192. <div class="documentwrapper">
  193. <div class="bodywrapper">
  194. <div class="body" role="main">
  195. <section id="module-subprocess">
  196. <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> — Subprocess management<a class="headerlink" href="#module-subprocess" title="Link to this heading">¶</a></h1>
  197. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/subprocess.py">Lib/subprocess.py</a></p>
  198. <hr class="docutils" />
  199. <p>The <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> module allows you to spawn new processes, connect to their
  200. input/output/error pipes, and obtain their return codes. This module intends to
  201. replace several older modules and functions:</p>
  202. <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>
  203. <span class="n">os</span><span class="o">.</span><span class="n">spawn</span><span class="o">*</span>
  204. </pre></div>
  205. </div>
  206. <p>Information about how the <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> module can be used to replace these
  207. modules and functions can be found in the following sections.</p>
  208. <div class="admonition seealso">
  209. <p class="admonition-title">See also</p>
  210. <p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0324/"><strong>PEP 324</strong></a> – PEP proposing the subprocess module</p>
  211. </div>
  212. <div class="availability docutils container">
  213. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  214. <p>This module does not work or is not available on WebAssembly platforms
  215. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  216. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  217. </div>
  218. <section id="using-the-subprocess-module">
  219. <h2>Using the <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> Module<a class="headerlink" href="#using-the-subprocess-module" title="Link to this heading">¶</a></h2>
  220. <p>The recommended approach to invoking subprocesses is to use the <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>
  221. function for all use cases it can handle. For more advanced use cases, the
  222. underlying <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> interface can be used directly.</p>
  223. <dl class="py function">
  224. <dt class="sig sig-object py" id="subprocess.run">
  225. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">input</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capture_output</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shell</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">universal_newlines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">other_popen_kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.run" title="Link to this definition">¶</a></dt>
  226. <dd><p>Run the command described by <em>args</em>. Wait for command to complete, then
  227. return a <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> instance.</p>
  228. <p>The arguments shown above are merely the most common ones, described below
  229. in <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">Frequently Used Arguments</span></a> (hence the use of keyword-only notation
  230. in the abbreviated signature). The full function signature is largely the
  231. same as that of the <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> constructor - most of the arguments to
  232. this function are passed through to that interface. (<em>timeout</em>, <em>input</em>,
  233. <em>check</em>, and <em>capture_output</em> are not.)</p>
  234. <p>If <em>capture_output</em> is true, stdout and stderr will be captured.
  235. When used, the internal <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> object is automatically created with
  236. <em>stdout</em> and <em>stdin</em> both set to <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>.
  237. The <em>stdout</em> and <em>stderr</em> arguments may not be supplied at the same time as <em>capture_output</em>.
  238. If you wish to capture and combine both streams into one,
  239. set <em>stdout</em> to <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>
  240. and <em>stderr</em> to <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>,
  241. instead of using <em>capture_output</em>.</p>
  242. <p>A <em>timeout</em> may be specified in seconds, it is internally passed on to
  243. <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>. If the timeout expires, the child process will be
  244. killed and waited for. The <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> exception will be
  245. re-raised after the child process has terminated. The initial process
  246. creation itself cannot be interrupted on many platform APIs so you are not
  247. guaranteed to see a timeout exception until at least after however long
  248. process creation takes.</p>
  249. <p>The <em>input</em> argument is passed to <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> and thus to the
  250. subprocess’s stdin. If used it must be a byte sequence, or a string if
  251. <em>encoding</em> or <em>errors</em> is specified or <em>text</em> is true. When
  252. used, the internal <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> object is automatically created with
  253. <em>stdin</em> set to <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>,
  254. and the <em>stdin</em> argument may not be used as well.</p>
  255. <p>If <em>check</em> is true, and the process exits with a non-zero exit code, a
  256. <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> exception will be raised. Attributes of that
  257. exception hold the arguments, the exit code, and stdout and stderr if they
  258. were captured.</p>
  259. <p>If <em>encoding</em> or <em>errors</em> are specified, or <em>text</em> is true,
  260. file objects for stdin, stdout and stderr are opened in text mode using the
  261. specified <em>encoding</em> and <em>errors</em> or the <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> default.
  262. The <em>universal_newlines</em> argument is equivalent to <em>text</em> and is provided
  263. for backwards compatibility. By default, file objects are opened in binary mode.</p>
  264. <p>If <em>env</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be a mapping that defines the environment
  265. variables for the new process; these are used instead of the default
  266. behavior of inheriting the current process’ environment. It is passed
  267. directly to <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>. This mapping can be str to str on any platform
  268. or bytes to bytes on POSIX platforms much like <a class="reference internal" href="os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> or
  269. <a class="reference internal" href="os.html#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a>.</p>
  270. <p>Examples:</p>
  271. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">&quot;ls&quot;</span><span class="p">,</span> <span class="s2">&quot;-l&quot;</span><span class="p">])</span> <span class="c1"># doesn&#39;t capture output</span>
  272. <span class="go">CompletedProcess(args=[&#39;ls&#39;, &#39;-l&#39;], returncode=0)</span>
  273. <span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s2">&quot;exit 1&quot;</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>
  274. <span class="gt">Traceback (most recent call last):</span>
  275. <span class="w"> </span><span class="c">...</span>
  276. <span class="gr">subprocess.CalledProcessError</span>: <span class="n">Command &#39;exit 1&#39; returned non-zero exit status 1</span>
  277. <span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">&quot;ls&quot;</span><span class="p">,</span> <span class="s2">&quot;-l&quot;</span><span class="p">,</span> <span class="s2">&quot;/dev/null&quot;</span><span class="p">],</span> <span class="n">capture_output</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  278. <span class="go">CompletedProcess(args=[&#39;ls&#39;, &#39;-l&#39;, &#39;/dev/null&#39;], returncode=0,</span>
  279. <span class="go">stdout=b&#39;crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n&#39;, stderr=b&#39;&#39;)</span>
  280. </pre></div>
  281. </div>
  282. <div class="versionadded">
  283. <p><span class="versionmodified added">New in version 3.5.</span></p>
  284. </div>
  285. <div class="versionchanged">
  286. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>encoding</em> and <em>errors</em> parameters</p>
  287. </div>
  288. <div class="versionchanged">
  289. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>text</em> parameter, as a more understandable alias of <em>universal_newlines</em>.
  290. Added the <em>capture_output</em> parameter.</p>
  291. </div>
  292. <div class="versionchanged">
  293. <p><span class="versionmodified changed">Changed in version 3.12: </span>Changed Windows shell search order for <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>. The current
  294. directory and <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> are replaced with <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> and
  295. <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>. As a result, dropping a
  296. malicious program named <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> into a current directory no
  297. longer works.</p>
  298. </div>
  299. </dd></dl>
  300. <dl class="py class">
  301. <dt class="sig sig-object py" id="subprocess.CompletedProcess">
  302. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CompletedProcess</span></span><a class="headerlink" href="#subprocess.CompletedProcess" title="Link to this definition">¶</a></dt>
  303. <dd><p>The return value from <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>, representing a process that has finished.</p>
  304. <dl class="py attribute">
  305. <dt class="sig sig-object py" id="subprocess.CompletedProcess.args">
  306. <span class="sig-name descname"><span class="pre">args</span></span><a class="headerlink" href="#subprocess.CompletedProcess.args" title="Link to this definition">¶</a></dt>
  307. <dd><p>The arguments used to launch the process. This may be a list or a string.</p>
  308. </dd></dl>
  309. <dl class="py attribute">
  310. <dt class="sig sig-object py" id="subprocess.CompletedProcess.returncode">
  311. <span class="sig-name descname"><span class="pre">returncode</span></span><a class="headerlink" href="#subprocess.CompletedProcess.returncode" title="Link to this definition">¶</a></dt>
  312. <dd><p>Exit status of the child process. Typically, an exit status of 0 indicates
  313. that it ran successfully.</p>
  314. <p>A negative value <code class="docutils literal notranslate"><span class="pre">-N</span></code> indicates that the child was terminated by signal
  315. <code class="docutils literal notranslate"><span class="pre">N</span></code> (POSIX only).</p>
  316. </dd></dl>
  317. <dl class="py attribute">
  318. <dt class="sig sig-object py" id="subprocess.CompletedProcess.stdout">
  319. <span class="sig-name descname"><span class="pre">stdout</span></span><a class="headerlink" href="#subprocess.CompletedProcess.stdout" title="Link to this definition">¶</a></dt>
  320. <dd><p>Captured stdout from the child process. A bytes sequence, or a string if
  321. <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> was called with an encoding, errors, or text=True.
  322. <code class="docutils literal notranslate"><span class="pre">None</span></code> if stdout was not captured.</p>
  323. <p>If you ran the process with <code class="docutils literal notranslate"><span class="pre">stderr=subprocess.STDOUT</span></code>, stdout and
  324. stderr will be combined in this attribute, and <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> will be
  325. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  326. </dd></dl>
  327. <dl class="py attribute">
  328. <dt class="sig sig-object py" id="subprocess.CompletedProcess.stderr">
  329. <span class="sig-name descname"><span class="pre">stderr</span></span><a class="headerlink" href="#subprocess.CompletedProcess.stderr" title="Link to this definition">¶</a></dt>
  330. <dd><p>Captured stderr from the child process. A bytes sequence, or a string if
  331. <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> was called with an encoding, errors, or text=True.
  332. <code class="docutils literal notranslate"><span class="pre">None</span></code> if stderr was not captured.</p>
  333. </dd></dl>
  334. <dl class="py method">
  335. <dt class="sig sig-object py" id="subprocess.CompletedProcess.check_returncode">
  336. <span class="sig-name descname"><span class="pre">check_returncode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.CompletedProcess.check_returncode" title="Link to this definition">¶</a></dt>
  337. <dd><p>If <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> is non-zero, raise 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>
  338. </dd></dl>
  339. <div class="versionadded">
  340. <p><span class="versionmodified added">New in version 3.5.</span></p>
  341. </div>
  342. </dd></dl>
  343. <dl class="py data">
  344. <dt class="sig sig-object py" id="subprocess.DEVNULL">
  345. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">DEVNULL</span></span><a class="headerlink" href="#subprocess.DEVNULL" title="Link to this definition">¶</a></dt>
  346. <dd><p>Special value that can be used as the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> argument
  347. to <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> and indicates that the special file <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>
  348. will be used.</p>
  349. <div class="versionadded">
  350. <p><span class="versionmodified added">New in version 3.3.</span></p>
  351. </div>
  352. </dd></dl>
  353. <dl class="py data">
  354. <dt class="sig sig-object py" id="subprocess.PIPE">
  355. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">PIPE</span></span><a class="headerlink" href="#subprocess.PIPE" title="Link to this definition">¶</a></dt>
  356. <dd><p>Special value that can be used as the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> argument
  357. to <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> and indicates that a pipe to the standard stream should be
  358. opened. Most useful with <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>
  359. </dd></dl>
  360. <dl class="py data">
  361. <dt class="sig sig-object py" id="subprocess.STDOUT">
  362. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STDOUT</span></span><a class="headerlink" href="#subprocess.STDOUT" title="Link to this definition">¶</a></dt>
  363. <dd><p>Special value that can be used as the <em>stderr</em> argument to <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> and
  364. indicates that standard error should go into the same handle as standard
  365. output.</p>
  366. </dd></dl>
  367. <dl class="py exception">
  368. <dt class="sig sig-object py" id="subprocess.SubprocessError">
  369. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">SubprocessError</span></span><a class="headerlink" href="#subprocess.SubprocessError" title="Link to this definition">¶</a></dt>
  370. <dd><p>Base class for all other exceptions from this module.</p>
  371. <div class="versionadded">
  372. <p><span class="versionmodified added">New in version 3.3.</span></p>
  373. </div>
  374. </dd></dl>
  375. <dl class="py exception">
  376. <dt class="sig sig-object py" id="subprocess.TimeoutExpired">
  377. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">TimeoutExpired</span></span><a class="headerlink" href="#subprocess.TimeoutExpired" title="Link to this definition">¶</a></dt>
  378. <dd><p>Subclass of <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>, raised when a timeout expires
  379. while waiting for a child process.</p>
  380. <dl class="py attribute">
  381. <dt class="sig sig-object py" id="subprocess.TimeoutExpired.cmd">
  382. <span class="sig-name descname"><span class="pre">cmd</span></span><a class="headerlink" href="#subprocess.TimeoutExpired.cmd" title="Link to this definition">¶</a></dt>
  383. <dd><p>Command that was used to spawn the child process.</p>
  384. </dd></dl>
  385. <dl class="py attribute">
  386. <dt class="sig sig-object py" id="subprocess.TimeoutExpired.timeout">
  387. <span class="sig-name descname"><span class="pre">timeout</span></span><a class="headerlink" href="#subprocess.TimeoutExpired.timeout" title="Link to this definition">¶</a></dt>
  388. <dd><p>Timeout in seconds.</p>
  389. </dd></dl>
  390. <dl class="py attribute">
  391. <dt class="sig sig-object py" id="subprocess.TimeoutExpired.output">
  392. <span class="sig-name descname"><span class="pre">output</span></span><a class="headerlink" href="#subprocess.TimeoutExpired.output" title="Link to this definition">¶</a></dt>
  393. <dd><p>Output of the child process if it was captured by <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> or
  394. <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>. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is always
  395. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> when any output was captured regardless of the
  396. <code class="docutils literal notranslate"><span class="pre">text=True</span></code> setting. It may remain <code class="docutils literal notranslate"><span class="pre">None</span></code> instead of <code class="docutils literal notranslate"><span class="pre">b''</span></code>
  397. when no output was observed.</p>
  398. </dd></dl>
  399. <dl class="py attribute">
  400. <dt class="sig sig-object py" id="subprocess.TimeoutExpired.stdout">
  401. <span class="sig-name descname"><span class="pre">stdout</span></span><a class="headerlink" href="#subprocess.TimeoutExpired.stdout" title="Link to this definition">¶</a></dt>
  402. <dd><p>Alias for output, for symmetry with <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>
  403. </dd></dl>
  404. <dl class="py attribute">
  405. <dt class="sig sig-object py" id="subprocess.TimeoutExpired.stderr">
  406. <span class="sig-name descname"><span class="pre">stderr</span></span><a class="headerlink" href="#subprocess.TimeoutExpired.stderr" title="Link to this definition">¶</a></dt>
  407. <dd><p>Stderr output of the child process if it was captured by <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>.
  408. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is always <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> when stderr output
  409. was captured regardless of the <code class="docutils literal notranslate"><span class="pre">text=True</span></code> setting. It may remain
  410. <code class="docutils literal notranslate"><span class="pre">None</span></code> instead of <code class="docutils literal notranslate"><span class="pre">b''</span></code> when no stderr output was observed.</p>
  411. </dd></dl>
  412. <div class="versionadded">
  413. <p><span class="versionmodified added">New in version 3.3.</span></p>
  414. </div>
  415. <div class="versionchanged">
  416. <p><span class="versionmodified changed">Changed in version 3.5: </span><em>stdout</em> and <em>stderr</em> attributes added</p>
  417. </div>
  418. </dd></dl>
  419. <dl class="py exception">
  420. <dt class="sig sig-object py" id="subprocess.CalledProcessError">
  421. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CalledProcessError</span></span><a class="headerlink" href="#subprocess.CalledProcessError" title="Link to this definition">¶</a></dt>
  422. <dd><p>Subclass of <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>, raised when a process run by
  423. <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>, or <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> (with <code class="docutils literal notranslate"><span class="pre">check=True</span></code>)
  424. returns a non-zero exit status.</p>
  425. <dl class="py attribute">
  426. <dt class="sig sig-object py" id="subprocess.CalledProcessError.returncode">
  427. <span class="sig-name descname"><span class="pre">returncode</span></span><a class="headerlink" href="#subprocess.CalledProcessError.returncode" title="Link to this definition">¶</a></dt>
  428. <dd><p>Exit status of the child process. If the process exited due to a
  429. signal, this will be the negative signal number.</p>
  430. </dd></dl>
  431. <dl class="py attribute">
  432. <dt class="sig sig-object py" id="subprocess.CalledProcessError.cmd">
  433. <span class="sig-name descname"><span class="pre">cmd</span></span><a class="headerlink" href="#subprocess.CalledProcessError.cmd" title="Link to this definition">¶</a></dt>
  434. <dd><p>Command that was used to spawn the child process.</p>
  435. </dd></dl>
  436. <dl class="py attribute">
  437. <dt class="sig sig-object py" id="subprocess.CalledProcessError.output">
  438. <span class="sig-name descname"><span class="pre">output</span></span><a class="headerlink" href="#subprocess.CalledProcessError.output" title="Link to this definition">¶</a></dt>
  439. <dd><p>Output of the child process if it was captured by <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> or
  440. <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>. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  441. </dd></dl>
  442. <dl class="py attribute">
  443. <dt class="sig sig-object py" id="subprocess.CalledProcessError.stdout">
  444. <span class="sig-name descname"><span class="pre">stdout</span></span><a class="headerlink" href="#subprocess.CalledProcessError.stdout" title="Link to this definition">¶</a></dt>
  445. <dd><p>Alias for output, for symmetry with <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>
  446. </dd></dl>
  447. <dl class="py attribute">
  448. <dt class="sig sig-object py" id="subprocess.CalledProcessError.stderr">
  449. <span class="sig-name descname"><span class="pre">stderr</span></span><a class="headerlink" href="#subprocess.CalledProcessError.stderr" title="Link to this definition">¶</a></dt>
  450. <dd><p>Stderr output of the child process if it was captured by <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>.
  451. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  452. </dd></dl>
  453. <div class="versionchanged">
  454. <p><span class="versionmodified changed">Changed in version 3.5: </span><em>stdout</em> and <em>stderr</em> attributes added</p>
  455. </div>
  456. </dd></dl>
  457. <section id="frequently-used-arguments">
  458. <span id="id1"></span><h3>Frequently Used Arguments<a class="headerlink" href="#frequently-used-arguments" title="Link to this heading">¶</a></h3>
  459. <p>To support a wide variety of use cases, the <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> constructor (and
  460. the convenience functions) accept a large number of optional arguments. For
  461. most typical use cases, many of these arguments can be safely left at their
  462. default values. The arguments that are most commonly needed are:</p>
  463. <blockquote>
  464. <div><p><em>args</em> is required for all calls and should be a string, or a sequence of
  465. program arguments. Providing a sequence of arguments is generally
  466. preferred, as it allows the module to take care of any required escaping
  467. and quoting of arguments (e.g. to permit spaces in file names). If passing
  468. a single string, either <em>shell</em> must be <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> (see below) or else
  469. the string must simply name the program to be executed without specifying
  470. any arguments.</p>
  471. <p><em>stdin</em>, <em>stdout</em> and <em>stderr</em> specify the executed program’s standard input,
  472. standard output and standard error file handles, respectively. Valid values
  473. are <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>, an existing file descriptor (a
  474. positive integer), and an existing <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> with a valid file
  475. descriptor. With the default settings of <code class="docutils literal notranslate"><span class="pre">None</span></code>, no redirection will
  476. occur. <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> indicates that a new pipe to the child should be
  477. created. <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> indicates that the special file <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>
  478. will be used. Additionally, <em>stderr</em> can be <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>, which indicates
  479. that the stderr data from the child process should be captured into the same
  480. file handle as for <em>stdout</em>.</p>
  481. <p id="index-1">If <em>encoding</em> or <em>errors</em> are specified, or <em>text</em> (also known as
  482. <em>universal_newlines</em>) is true,
  483. the file objects <em>stdin</em>, <em>stdout</em> and <em>stderr</em> will be opened in text
  484. mode using the <em>encoding</em> and <em>errors</em> specified in the call or the
  485. defaults for <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>
  486. <p>For <em>stdin</em>, line ending characters <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> in the input will be converted
  487. to the default line separator <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>. For <em>stdout</em> and <em>stderr</em>,
  488. all line endings in the output will be converted to <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>. For more
  489. information see the documentation of the <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> class
  490. when the <em>newline</em> argument to its constructor is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  491. <p>If text mode is not used, <em>stdin</em>, <em>stdout</em> and <em>stderr</em> will be opened as
  492. binary streams. No encoding or line ending conversion is performed.</p>
  493. <div class="versionchanged">
  494. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <em>encoding</em> and <em>errors</em> parameters.</p>
  495. </div>
  496. <div class="versionchanged">
  497. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>text</em> parameter as an alias for <em>universal_newlines</em>.</p>
  498. </div>
  499. <div class="admonition note">
  500. <p class="admonition-title">Note</p>
  501. <p>The newlines attribute of the file objects <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>,
  502. <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> and <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> are not updated by
  503. the <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> method.</p>
  504. </div>
  505. <p>If <em>shell</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the specified command will be executed through
  506. the shell. This can be useful if you are using Python primarily for the
  507. enhanced control flow it offers over most system shells and still want
  508. convenient access to other shell features such as shell pipes, filename
  509. wildcards, environment variable expansion, and expansion of <code class="docutils literal notranslate"><span class="pre">~</span></code> to a
  510. user’s home directory. However, note that Python itself offers
  511. implementations of many shell-like features (in particular, <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>,
  512. <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>,
  513. <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>, and <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>
  514. <div class="versionchanged">
  515. <p><span class="versionmodified changed">Changed in version 3.3: </span>When <em>universal_newlines</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the class uses the encoding
  516. <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>
  517. instead of <code class="docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code>. See the
  518. <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> class for more information on this change.</p>
  519. </div>
  520. <div class="admonition note">
  521. <p class="admonition-title">Note</p>
  522. <p>Read the <a class="reference internal" href="#security-considerations">Security Considerations</a> section before using <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>.</p>
  523. </div>
  524. </div></blockquote>
  525. <p>These options, along with all of the other options, are described in more
  526. detail in the <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> constructor documentation.</p>
  527. </section>
  528. <section id="popen-constructor">
  529. <h3>Popen Constructor<a class="headerlink" href="#popen-constructor" title="Link to this heading">¶</a></h3>
  530. <p>The underlying process creation and management in this module is handled by
  531. the <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> class. It offers a lot of flexibility so that developers
  532. are able to handle the less common cases not covered by the convenience
  533. functions.</p>
  534. <dl class="py class">
  535. <dt class="sig sig-object py" id="subprocess.Popen">
  536. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">Popen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bufsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">executable</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">preexec_fn</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">close_fds</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shell</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">universal_newlines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">startupinfo</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">creationflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">restore_signals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">start_new_session</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pass_fds</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">group</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extra_groups</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">user</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">umask</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pipesize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">process_group</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen" title="Link to this definition">¶</a></dt>
  537. <dd><p>Execute a child program in a new process. On POSIX, the class uses
  538. <a class="reference internal" href="os.html#os.execvpe" title="os.execvpe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.execvpe()</span></code></a>-like behavior to execute the child program. On Windows,
  539. the class uses the Windows <code class="docutils literal notranslate"><span class="pre">CreateProcess()</span></code> function. The arguments to
  540. <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> are as follows.</p>
  541. <p><em>args</em> should be a sequence of program arguments or else a single string
  542. or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.
  543. By default, the program to execute is the first item in <em>args</em> if <em>args</em> is
  544. a sequence. If <em>args</em> is a string, the interpretation is
  545. platform-dependent and described below. See the <em>shell</em> and <em>executable</em>
  546. arguments for additional differences from the default behavior. Unless
  547. otherwise stated, it is recommended to pass <em>args</em> as a sequence.</p>
  548. <div class="admonition warning">
  549. <p class="admonition-title">Warning</p>
  550. <p>For maximum reliability, use a fully qualified path for the executable.
  551. To search for an unqualified name on <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>, use
  552. <a class="reference internal" href="shutil.html#shutil.which" title="shutil.which"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutil.which()</span></code></a>. On all platforms, passing <a class="reference internal" href="sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a>
  553. is the recommended way to launch the current Python interpreter again,
  554. and use the <code class="docutils literal notranslate"><span class="pre">-m</span></code> command-line format to launch an installed module.</p>
  555. <p>Resolving the path of <em>executable</em> (or the first item of <em>args</em>) is
  556. platform dependent. For POSIX, see <a class="reference internal" href="os.html#os.execvpe" title="os.execvpe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.execvpe()</span></code></a>, and note that
  557. when resolving or searching for the executable path, <em>cwd</em> overrides the
  558. current working directory and <em>env</em> can override the <code class="docutils literal notranslate"><span class="pre">PATH</span></code>
  559. environment variable. For Windows, see the documentation of the
  560. <code class="docutils literal notranslate"><span class="pre">lpApplicationName</span></code> and <code class="docutils literal notranslate"><span class="pre">lpCommandLine</span></code> parameters of WinAPI
  561. <code class="docutils literal notranslate"><span class="pre">CreateProcess</span></code>, and note that when resolving or searching for the
  562. executable path with <code class="docutils literal notranslate"><span class="pre">shell=False</span></code>, <em>cwd</em> does not override the
  563. current working directory and <em>env</em> cannot override the <code class="docutils literal notranslate"><span class="pre">PATH</span></code>
  564. environment variable. Using a full path avoids all of these variations.</p>
  565. </div>
  566. <p>An example of passing some arguments to an external program
  567. as a sequence is:</p>
  568. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;/usr/bin/git&quot;</span><span class="p">,</span> <span class="s2">&quot;commit&quot;</span><span class="p">,</span> <span class="s2">&quot;-m&quot;</span><span class="p">,</span> <span class="s2">&quot;Fixes a bug.&quot;</span><span class="p">])</span>
  569. </pre></div>
  570. </div>
  571. <p>On POSIX, if <em>args</em> is a string, the string is interpreted as the name or
  572. path of the program to execute. However, this can only be done if not
  573. passing arguments to the program.</p>
  574. <div class="admonition note">
  575. <p class="admonition-title">Note</p>
  576. <p>It may not be obvious how to break a shell command into a sequence of arguments,
  577. especially in complex cases. <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> can illustrate how to
  578. determine the correct tokenization for <em>args</em>:</p>
  579. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shlex</span><span class="o">,</span> <span class="nn">subprocess</span>
  580. <span class="gp">&gt;&gt;&gt; </span><span class="n">command_line</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
  581. <span class="go">/bin/vikings -input eggs.txt -output &quot;spam spam.txt&quot; -cmd &quot;echo &#39;$MONEY&#39;&quot;</span>
  582. <span class="gp">&gt;&gt;&gt; </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>
  583. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  584. <span class="go">[&#39;/bin/vikings&#39;, &#39;-input&#39;, &#39;eggs.txt&#39;, &#39;-output&#39;, &#39;spam spam.txt&#39;, &#39;-cmd&#39;, &quot;echo &#39;$MONEY&#39;&quot;]</span>
  585. <span class="gp">&gt;&gt;&gt; </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>
  586. </pre></div>
  587. </div>
  588. <p>Note in particular that options (such as <em>-input</em>) and arguments (such
  589. as <em>eggs.txt</em>) that are separated by whitespace in the shell go in separate
  590. list elements, while arguments that need quoting or backslash escaping when
  591. used in the shell (such as filenames containing spaces or the <em>echo</em> command
  592. shown above) are single list elements.</p>
  593. </div>
  594. <p>On Windows, if <em>args</em> is a sequence, it will be converted to a string in a
  595. manner described in <a class="reference internal" href="#converting-argument-sequence"><span class="std std-ref">Converting an argument sequence to a string on Windows</span></a>. This is because
  596. the underlying <code class="docutils literal notranslate"><span class="pre">CreateProcess()</span></code> operates on strings.</p>
  597. <div class="versionchanged">
  598. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>args</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> if <em>shell</em> is
  599. <code class="docutils literal notranslate"><span class="pre">False</span></code> and a sequence containing path-like objects on POSIX.</p>
  600. </div>
  601. <div class="versionchanged">
  602. <p><span class="versionmodified changed">Changed in version 3.8: </span><em>args</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> if <em>shell</em> is
  603. <code class="docutils literal notranslate"><span class="pre">False</span></code> and a sequence containing bytes and path-like objects
  604. on Windows.</p>
  605. </div>
  606. <p>The <em>shell</em> argument (which defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>) specifies whether to use
  607. the shell as the program to execute. If <em>shell</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, it is
  608. recommended to pass <em>args</em> as a string rather than as a sequence.</p>
  609. <p>On POSIX with <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, the shell defaults to <code class="file docutils literal notranslate"><span class="pre">/bin/sh</span></code>. If
  610. <em>args</em> is a string, the string specifies the command
  611. to execute through the shell. This means that the string must be
  612. formatted exactly as it would be when typed at the shell prompt. This
  613. includes, for example, quoting or backslash escaping filenames with spaces in
  614. them. If <em>args</em> is a sequence, the first item specifies the command string, and
  615. any additional items will be treated as additional arguments to the shell
  616. itself. That is to say, <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> does the equivalent of:</p>
  617. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Popen</span><span class="p">([</span><span class="s1">&#39;/bin/sh&#39;</span><span class="p">,</span> <span class="s1">&#39;-c&#39;</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>
  618. </pre></div>
  619. </div>
  620. <p>On Windows with <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">COMSPEC</span></code> environment variable
  621. specifies the default shell. The only time you need to specify
  622. <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> on Windows is when the command you wish to execute is built
  623. into the shell (e.g. <strong class="command">dir</strong> or <strong class="command">copy</strong>). You do not need
  624. <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> to run a batch file or console-based executable.</p>
  625. <div class="admonition note">
  626. <p class="admonition-title">Note</p>
  627. <p>Read the <a class="reference internal" href="#security-considerations">Security Considerations</a> section before using <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>.</p>
  628. </div>
  629. <p><em>bufsize</em> will be supplied as the corresponding argument to the
  630. <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> function when creating the stdin/stdout/stderr pipe
  631. file objects:</p>
  632. <ul class="simple">
  633. <li><p><code class="docutils literal notranslate"><span class="pre">0</span></code> means unbuffered (read and write are one
  634. system call and can return short)</p></li>
  635. <li><p><code class="docutils literal notranslate"><span class="pre">1</span></code> means line buffered
  636. (only usable if <code class="docutils literal notranslate"><span class="pre">text=True</span></code> or <code class="docutils literal notranslate"><span class="pre">universal_newlines=True</span></code>)</p></li>
  637. <li><p>any other positive value means use a buffer of approximately that
  638. size</p></li>
  639. <li><p>negative bufsize (the default) means the system default of
  640. io.DEFAULT_BUFFER_SIZE will be used.</p></li>
  641. </ul>
  642. <div class="versionchanged">
  643. <p><span class="versionmodified changed">Changed in version 3.3.1: </span><em>bufsize</em> now defaults to -1 to enable buffering by default to match the
  644. behavior that most code expects. In versions prior to Python 3.2.4 and
  645. 3.3.1 it incorrectly defaulted to <code class="docutils literal notranslate"><span class="pre">0</span></code> which was unbuffered
  646. and allowed short reads. This was unintentional and did not match the
  647. behavior of Python 2 as most code expected.</p>
  648. </div>
  649. <p>The <em>executable</em> argument specifies a replacement program to execute. It
  650. is very seldom needed. When <code class="docutils literal notranslate"><span class="pre">shell=False</span></code>, <em>executable</em> replaces the
  651. program to execute specified by <em>args</em>. However, the original <em>args</em> is
  652. still passed to the program. Most programs treat the program specified
  653. by <em>args</em> as the command name, which can then be different from the program
  654. actually executed. On POSIX, the <em>args</em> name
  655. becomes the display name for the executable in utilities such as
  656. <strong class="program">ps</strong>. If <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, on POSIX the <em>executable</em> argument
  657. specifies a replacement shell for the default <code class="file docutils literal notranslate"><span class="pre">/bin/sh</span></code>.</p>
  658. <div class="versionchanged">
  659. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>executable</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> on POSIX.</p>
  660. </div>
  661. <div class="versionchanged">
  662. <p><span class="versionmodified changed">Changed in version 3.8: </span><em>executable</em> parameter accepts a bytes and <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>
  663. on Windows.</p>
  664. </div>
  665. <div class="versionchanged">
  666. <p><span class="versionmodified changed">Changed in version 3.12: </span>Changed Windows shell search order for <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>. The current
  667. directory and <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> are replaced with <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> and
  668. <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>. As a result, dropping a
  669. malicious program named <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> into a current directory no
  670. longer works.</p>
  671. </div>
  672. <p><em>stdin</em>, <em>stdout</em> and <em>stderr</em> specify the executed program’s standard input,
  673. standard output and standard error file handles, respectively. Valid values
  674. are <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>, an existing file descriptor (a
  675. positive integer), and an existing <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> with a valid file
  676. descriptor. With the default settings of <code class="docutils literal notranslate"><span class="pre">None</span></code>, no redirection will
  677. occur. <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> indicates that a new pipe to the child should be
  678. created. <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> indicates that the special file <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>
  679. will be used. Additionally, <em>stderr</em> can be <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>, which indicates
  680. that the stderr data from the applications should be captured into the same
  681. file handle as for <em>stdout</em>.</p>
  682. <p>If <em>preexec_fn</em> is set to a callable object, this object will be called in the
  683. child process just before the child is executed.
  684. (POSIX only)</p>
  685. <div class="admonition warning">
  686. <p class="admonition-title">Warning</p>
  687. <p>The <em>preexec_fn</em> parameter is NOT SAFE to use in the presence of threads
  688. in your application. The child process could deadlock before exec is
  689. called.</p>
  690. </div>
  691. <div class="admonition note">
  692. <p class="admonition-title">Note</p>
  693. <p>If you need to modify the environment for the child use the <em>env</em>
  694. parameter rather than doing it in a <em>preexec_fn</em>.
  695. The <em>start_new_session</em> and <em>process_group</em> parameters should take the place of
  696. code using <em>preexec_fn</em> to call <a class="reference internal" href="os.html#os.setsid" title="os.setsid"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.setsid()</span></code></a> or <a class="reference internal" href="os.html#os.setpgid" title="os.setpgid"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.setpgid()</span></code></a> in the child.</p>
  697. </div>
  698. <div class="versionchanged">
  699. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>preexec_fn</em> parameter is no longer supported in subinterpreters.
  700. The use of the parameter in a subinterpreter raises
  701. <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>. The new restriction may affect applications that
  702. are deployed in mod_wsgi, uWSGI, and other embedded environments.</p>
  703. </div>
  704. <p>If <em>close_fds</em> is true, all file descriptors except <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">1</span></code> and
  705. <code class="docutils literal notranslate"><span class="pre">2</span></code> will be closed before the child process is executed. Otherwise
  706. when <em>close_fds</em> is false, file descriptors obey their inheritable flag
  707. as described in <a class="reference internal" href="os.html#fd-inheritance"><span class="std std-ref">Inheritance of File Descriptors</span></a>.</p>
  708. <p>On Windows, if <em>close_fds</em> is true then no handles will be inherited by the
  709. child process unless explicitly passed in the <code class="docutils literal notranslate"><span class="pre">handle_list</span></code> element of
  710. <a class="reference internal" href="#subprocess.STARTUPINFO.lpAttributeList" title="subprocess.STARTUPINFO.lpAttributeList"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STARTUPINFO.lpAttributeList</span></code></a>, or by standard handle redirection.</p>
  711. <div class="versionchanged">
  712. <p><span class="versionmodified changed">Changed in version 3.2: </span>The default for <em>close_fds</em> was changed from <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> to
  713. what is described above.</p>
  714. </div>
  715. <div class="versionchanged">
  716. <p><span class="versionmodified changed">Changed in version 3.7: </span>On Windows the default for <em>close_fds</em> was changed from <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> to
  717. <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> when redirecting the standard handles. It’s now possible to
  718. set <em>close_fds</em> to <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> when redirecting the standard handles.</p>
  719. </div>
  720. <p><em>pass_fds</em> is an optional sequence of file descriptors to keep open
  721. between the parent and child. Providing any <em>pass_fds</em> forces
  722. <em>close_fds</em> to be <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 only)</p>
  723. <div class="versionchanged">
  724. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>pass_fds</em> parameter was added.</p>
  725. </div>
  726. <p>If <em>cwd</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function changes the working directory to
  727. <em>cwd</em> before executing the child. <em>cwd</em> can be a string, bytes or
  728. <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like</span></a> object. On POSIX, the function
  729. looks for <em>executable</em> (or for the first item in <em>args</em>) relative to <em>cwd</em>
  730. if the executable path is a relative path.</p>
  731. <div class="versionchanged">
  732. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>cwd</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> on POSIX.</p>
  733. </div>
  734. <div class="versionchanged">
  735. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>cwd</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> on Windows.</p>
  736. </div>
  737. <div class="versionchanged">
  738. <p><span class="versionmodified changed">Changed in version 3.8: </span><em>cwd</em> parameter accepts a bytes object on Windows.</p>
  739. </div>
  740. <p>If <em>restore_signals</em> is true (the default) all signals that Python has set to
  741. SIG_IGN are restored to SIG_DFL in the child process before the exec.
  742. Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals.
  743. (POSIX only)</p>
  744. <div class="versionchanged">
  745. <p><span class="versionmodified changed">Changed in version 3.2: </span><em>restore_signals</em> was added.</p>
  746. </div>
  747. <p>If <em>start_new_session</em> is true the <code class="docutils literal notranslate"><span class="pre">setsid()</span></code> system call will be made in the
  748. child process prior to the execution of the subprocess.</p>
  749. <div class="availability docutils container">
  750. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  751. </div>
  752. <div class="versionchanged">
  753. <p><span class="versionmodified changed">Changed in version 3.2: </span><em>start_new_session</em> was added.</p>
  754. </div>
  755. <p>If <em>process_group</em> is a non-negative integer, the <code class="docutils literal notranslate"><span class="pre">setpgid(0,</span> <span class="pre">value)</span></code> system call will
  756. be made in the child process prior to the execution of the subprocess.</p>
  757. <div class="availability docutils container">
  758. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  759. </div>
  760. <div class="versionchanged">
  761. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>process_group</em> was added.</p>
  762. </div>
  763. <p>If <em>group</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the setregid() system call will be made in the
  764. child process prior to the execution of the subprocess. If the provided
  765. value is a string, it will be looked up via <a class="reference internal" href="grp.html#grp.getgrnam" title="grp.getgrnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">grp.getgrnam()</span></code></a> and
  766. the value in <code class="docutils literal notranslate"><span class="pre">gr_gid</span></code> will be used. If the value is an integer, it
  767. will be passed verbatim. (POSIX only)</p>
  768. <div class="availability docutils container">
  769. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  770. </div>
  771. <div class="versionadded">
  772. <p><span class="versionmodified added">New in version 3.9.</span></p>
  773. </div>
  774. <p>If <em>extra_groups</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the setgroups() system call will be
  775. made in the child process prior to the execution of the subprocess.
  776. Strings provided in <em>extra_groups</em> will be looked up via
  777. <a class="reference internal" href="grp.html#grp.getgrnam" title="grp.getgrnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">grp.getgrnam()</span></code></a> and the values in <code class="docutils literal notranslate"><span class="pre">gr_gid</span></code> will be used.
  778. Integer values will be passed verbatim. (POSIX only)</p>
  779. <div class="availability docutils container">
  780. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  781. </div>
  782. <div class="versionadded">
  783. <p><span class="versionmodified added">New in version 3.9.</span></p>
  784. </div>
  785. <p>If <em>user</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the setreuid() system call will be made in the
  786. child process prior to the execution of the subprocess. If the provided
  787. value is a string, it will be looked up via <a class="reference internal" href="pwd.html#pwd.getpwnam" title="pwd.getpwnam"><code class="xref py py-func docutils literal notranslate"><span class="pre">pwd.getpwnam()</span></code></a> and
  788. the value in <code class="docutils literal notranslate"><span class="pre">pw_uid</span></code> will be used. If the value is an integer, it will
  789. be passed verbatim. (POSIX only)</p>
  790. <div class="availability docutils container">
  791. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  792. </div>
  793. <div class="versionadded">
  794. <p><span class="versionmodified added">New in version 3.9.</span></p>
  795. </div>
  796. <p>If <em>umask</em> is not negative, the umask() system call will be made in the
  797. child process prior to the execution of the subprocess.</p>
  798. <div class="availability docutils container">
  799. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX</p>
  800. </div>
  801. <div class="versionadded">
  802. <p><span class="versionmodified added">New in version 3.9.</span></p>
  803. </div>
  804. <p>If <em>env</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be a mapping that defines the environment
  805. variables for the new process; these are used instead of the default
  806. behavior of inheriting the current process’ environment. This mapping can be
  807. str to str on any platform or bytes to bytes on POSIX platforms much like
  808. <a class="reference internal" href="os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> or <a class="reference internal" href="os.html#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a>.</p>
  809. <div class="admonition note">
  810. <p class="admonition-title">Note</p>
  811. <p>If specified, <em>env</em> must provide any variables required for the program to
  812. execute. On Windows, in order to run a <a class="reference external" href="https://en.wikipedia.org/wiki/Side-by-Side_Assembly">side-by-side assembly</a> the
  813. specified <em>env</em> <strong>must</strong> include a valid <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">SystemRoot</span></code>.</p>
  814. </div>
  815. <p>If <em>encoding</em> or <em>errors</em> are specified, or <em>text</em> is true, the file objects
  816. <em>stdin</em>, <em>stdout</em> and <em>stderr</em> are opened in text mode with the specified
  817. <em>encoding</em> and <em>errors</em>, as described above in <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">Frequently Used Arguments</span></a>.
  818. The <em>universal_newlines</em> argument is equivalent to <em>text</em> and is provided
  819. for backwards compatibility. By default, file objects are opened in binary mode.</p>
  820. <div class="versionadded">
  821. <p><span class="versionmodified added">New in version 3.6: </span><em>encoding</em> and <em>errors</em> were added.</p>
  822. </div>
  823. <div class="versionadded">
  824. <p><span class="versionmodified added">New in version 3.7: </span><em>text</em> was added as a more readable alias for <em>universal_newlines</em>.</p>
  825. </div>
  826. <p>If given, <em>startupinfo</em> will be a <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> object, which is
  827. passed to the underlying <code class="docutils literal notranslate"><span class="pre">CreateProcess</span></code> function.</p>
  828. <p>If given, <em>creationflags</em>, can be one or more of the following flags:</p>
  829. <ul class="simple">
  830. <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>
  831. <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>
  832. <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>
  833. <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>
  834. <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>
  835. <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>
  836. <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>
  837. <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>
  838. <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>
  839. <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>
  840. <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>
  841. <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>
  842. </ul>
  843. <p><em>pipesize</em> can be used to change the size of the pipe when
  844. <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> is used for <em>stdin</em>, <em>stdout</em> or <em>stderr</em>. The size of the pipe
  845. is only changed on platforms that support this (only Linux at this time of
  846. writing). Other platforms will ignore this parameter.</p>
  847. <div class="versionchanged">
  848. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>pipesize</em> parameter.</p>
  849. </div>
  850. <p>Popen objects are supported as context managers via the <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> statement:
  851. on exit, standard file descriptors are closed, and the process is waited for.</p>
  852. <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">&quot;ifconfig&quot;</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>
  853. <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>
  854. </pre></div>
  855. </div>
  856. <p class="audit-hook"><p>Popen and the other functions in this module that use it raise an
  857. <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">subprocess.Popen</span></code> with arguments
  858. <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>, and <code class="docutils literal notranslate"><span class="pre">env</span></code>. The value for <code class="docutils literal notranslate"><span class="pre">args</span></code>
  859. may be a single string or a list of strings, depending on platform.</p>
  860. </p>
  861. <div class="versionchanged">
  862. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added context manager support.</p>
  863. </div>
  864. <div class="versionchanged">
  865. <p><span class="versionmodified changed">Changed in version 3.6: </span>Popen destructor now emits a <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> warning if the child
  866. process is still running.</p>
  867. </div>
  868. <div class="versionchanged">
  869. <p><span class="versionmodified changed">Changed in version 3.8: </span>Popen can use <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> in some cases for better
  870. performance. On Windows Subsystem for Linux and QEMU User Emulation,
  871. Popen constructor using <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> no longer raise an
  872. exception on errors like missing program, but the child process fails
  873. with a non-zero <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>
  874. </div>
  875. </dd></dl>
  876. </section>
  877. <section id="exceptions">
  878. <h3>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h3>
  879. <p>Exceptions raised in the child process, before the new program has started to
  880. execute, will be re-raised in the parent.</p>
  881. <p>The most common exception raised is <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>. This occurs, for example,
  882. when trying to execute a non-existent file. Applications should prepare for
  883. <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> exceptions. Note that, when <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, <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>
  884. will be raised by the child only if the selected shell itself was not found.
  885. To determine if the shell failed to find the requested application, it is
  886. necessary to check the return code or output from the subprocess.</p>
  887. <p>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> will be raised if <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> is called with invalid
  888. arguments.</p>
  889. <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> and <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> will raise
  890. <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> if the called process returns a non-zero return
  891. code.</p>
  892. <p>All of the functions and methods that accept a <em>timeout</em> parameter, such as
  893. <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> and <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> will raise <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> if
  894. the timeout expires before the process exits.</p>
  895. <p>Exceptions defined in this module all inherit from <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>
  896. <div class="versionadded">
  897. <p><span class="versionmodified added">New in version 3.3: </span>The <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> base class was added.</p>
  898. </div>
  899. </section>
  900. </section>
  901. <section id="security-considerations">
  902. <span id="subprocess-security"></span><h2>Security Considerations<a class="headerlink" href="#security-considerations" title="Link to this heading">¶</a></h2>
  903. <p>Unlike some other popen functions, this implementation will never
  904. implicitly call a system shell. This means that all characters,
  905. including shell metacharacters, can safely be passed to child processes.
  906. If the shell is invoked explicitly, via <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>, it is the application’s
  907. responsibility to ensure that all whitespace and metacharacters are
  908. quoted appropriately to avoid
  909. <a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>
  910. vulnerabilities. On <a class="reference internal" href="shlex.html#shlex-quote-warning"><span class="std std-ref">some platforms</span></a>, it is possible
  911. to use <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> for this escaping.</p>
  912. </section>
  913. <section id="popen-objects">
  914. <h2>Popen Objects<a class="headerlink" href="#popen-objects" title="Link to this heading">¶</a></h2>
  915. <p>Instances of the <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> class have the following methods:</p>
  916. <dl class="py method">
  917. <dt class="sig sig-object py" id="subprocess.Popen.poll">
  918. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">poll</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.poll" title="Link to this definition">¶</a></dt>
  919. <dd><p>Check if child process has terminated. Set and return
  920. <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> attribute. Otherwise, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  921. </dd></dl>
  922. <dl class="py method">
  923. <dt class="sig sig-object py" id="subprocess.Popen.wait">
  924. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.wait" title="Link to this definition">¶</a></dt>
  925. <dd><p>Wait for child process to terminate. Set and return
  926. <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> attribute.</p>
  927. <p>If the process does not terminate after <em>timeout</em> seconds, raise a
  928. <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> exception. It is safe to catch this exception and
  929. retry the wait.</p>
  930. <div class="admonition note">
  931. <p class="admonition-title">Note</p>
  932. <p>This will deadlock when using <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> or <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code>
  933. and the child process generates enough output to a pipe such that
  934. it blocks waiting for the OS pipe buffer to accept more data.
  935. Use <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> when using pipes to avoid that.</p>
  936. </div>
  937. <div class="admonition note">
  938. <p class="admonition-title">Note</p>
  939. <p>When the <code class="docutils literal notranslate"><span class="pre">timeout</span></code> parameter is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, then (on POSIX) the
  940. function is implemented using a busy loop (non-blocking call and short
  941. sleeps). Use the <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> module for an asynchronous wait: see
  942. <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>
  943. </div>
  944. <div class="versionchanged">
  945. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>timeout</em> was added.</p>
  946. </div>
  947. </dd></dl>
  948. <dl class="py method">
  949. <dt class="sig sig-object py" id="subprocess.Popen.communicate">
  950. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">communicate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.communicate" title="Link to this definition">¶</a></dt>
  951. <dd><p>Interact with process: Send data to stdin. Read data from stdout and stderr,
  952. until end-of-file is reached. Wait for process to terminate and set the
  953. <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> attribute. The optional <em>input</em> argument should be
  954. data to be sent to the child process, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, if no data should be sent
  955. to the child. If streams were opened in text mode, <em>input</em> must be a string.
  956. Otherwise, it must be bytes.</p>
  957. <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> returns a tuple <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code>.
  958. The data will be strings if streams were opened in text mode; otherwise,
  959. bytes.</p>
  960. <p>Note that if you want to send data to the process’s stdin, you need to create
  961. the Popen object with <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code>. Similarly, to get anything other than
  962. <code class="docutils literal notranslate"><span class="pre">None</span></code> in the result tuple, you need to give <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> and/or
  963. <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> too.</p>
  964. <p>If the process does not terminate after <em>timeout</em> seconds, a
  965. <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> exception will be raised. Catching this exception and
  966. retrying communication will not lose any output.</p>
  967. <p>The child process is not killed if the timeout expires, so in order to
  968. cleanup properly a well-behaved application should kill the child process and
  969. finish communication:</p>
  970. <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>
  971. <span class="k">try</span><span class="p">:</span>
  972. <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>
  973. <span class="k">except</span> <span class="n">TimeoutExpired</span><span class="p">:</span>
  974. <span class="n">proc</span><span class="o">.</span><span class="n">kill</span><span class="p">()</span>
  975. <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>
  976. </pre></div>
  977. </div>
  978. <div class="admonition note">
  979. <p class="admonition-title">Note</p>
  980. <p>The data read is buffered in memory, so do not use this method if the data
  981. size is large or unlimited.</p>
  982. </div>
  983. <div class="versionchanged">
  984. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>timeout</em> was added.</p>
  985. </div>
  986. </dd></dl>
  987. <dl class="py method">
  988. <dt class="sig sig-object py" id="subprocess.Popen.send_signal">
  989. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">send_signal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signal</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.send_signal" title="Link to this definition">¶</a></dt>
  990. <dd><p>Sends the signal <em>signal</em> to the child.</p>
  991. <p>Do nothing if the process completed.</p>
  992. <div class="admonition note">
  993. <p class="admonition-title">Note</p>
  994. <p>On Windows, SIGTERM is an alias for <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 and
  995. CTRL_BREAK_EVENT can be sent to processes started with a <em>creationflags</em>
  996. parameter which includes <code class="docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></code>.</p>
  997. </div>
  998. </dd></dl>
  999. <dl class="py method">
  1000. <dt class="sig sig-object py" id="subprocess.Popen.terminate">
  1001. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">terminate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.terminate" title="Link to this definition">¶</a></dt>
  1002. <dd><p>Stop the child. On POSIX OSs the method sends <a class="reference internal" href="signal.html#signal.SIGTERM" title="signal.SIGTERM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGTERM</span></code></a> to the
  1003. child. On Windows the Win32 API function <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is called
  1004. to stop the child.</p>
  1005. </dd></dl>
  1006. <dl class="py method">
  1007. <dt class="sig sig-object py" id="subprocess.Popen.kill">
  1008. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">kill</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.Popen.kill" title="Link to this definition">¶</a></dt>
  1009. <dd><p>Kills the child. On POSIX OSs the function sends SIGKILL to the child.
  1010. On 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> is an alias for <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>
  1011. </dd></dl>
  1012. <p>The following attributes are also set by the class for you to access.
  1013. Reassigning them to new values is unsupported:</p>
  1014. <dl class="py attribute">
  1015. <dt class="sig sig-object py" id="subprocess.Popen.args">
  1016. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">args</span></span><a class="headerlink" href="#subprocess.Popen.args" title="Link to this definition">¶</a></dt>
  1017. <dd><p>The <em>args</em> argument as it was passed to <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
  1018. sequence of program arguments or else a single string.</p>
  1019. <div class="versionadded">
  1020. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1021. </div>
  1022. </dd></dl>
  1023. <dl class="py attribute">
  1024. <dt class="sig sig-object py" id="subprocess.Popen.stdin">
  1025. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">stdin</span></span><a class="headerlink" href="#subprocess.Popen.stdin" title="Link to this definition">¶</a></dt>
  1026. <dd><p>If the <em>stdin</em> argument was <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>, this attribute is a writeable
  1027. stream object as returned by <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>. If the <em>encoding</em> or <em>errors</em>
  1028. arguments were specified or the <em>text</em> or <em>universal_newlines</em> argument
  1029. was <code class="docutils literal notranslate"><span class="pre">True</span></code>, the stream is a text stream, otherwise it is a byte stream.
  1030. If the <em>stdin</em> argument was not <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>, this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1031. </dd></dl>
  1032. <dl class="py attribute">
  1033. <dt class="sig sig-object py" id="subprocess.Popen.stdout">
  1034. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">stdout</span></span><a class="headerlink" href="#subprocess.Popen.stdout" title="Link to this definition">¶</a></dt>
  1035. <dd><p>If the <em>stdout</em> argument was <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>, this attribute is a readable
  1036. stream object as returned by <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>. Reading from the stream provides
  1037. output from the child process. If the <em>encoding</em> or <em>errors</em> arguments were
  1038. specified or the <em>text</em> or <em>universal_newlines</em> argument was <code class="docutils literal notranslate"><span class="pre">True</span></code>, the
  1039. stream is a text stream, otherwise it is a byte stream. If the <em>stdout</em>
  1040. argument was not <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>, this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1041. </dd></dl>
  1042. <dl class="py attribute">
  1043. <dt class="sig sig-object py" id="subprocess.Popen.stderr">
  1044. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">stderr</span></span><a class="headerlink" href="#subprocess.Popen.stderr" title="Link to this definition">¶</a></dt>
  1045. <dd><p>If the <em>stderr</em> argument was <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>, this attribute is a readable
  1046. stream object as returned by <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>. Reading from the stream provides
  1047. error output from the child process. If the <em>encoding</em> or <em>errors</em> arguments
  1048. were specified or the <em>text</em> or <em>universal_newlines</em> argument was <code class="docutils literal notranslate"><span class="pre">True</span></code>, the
  1049. stream is a text stream, otherwise it is a byte stream. If the <em>stderr</em> argument
  1050. was not <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>, this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1051. </dd></dl>
  1052. <div class="admonition warning">
  1053. <p class="admonition-title">Warning</p>
  1054. <p>Use <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> rather than <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>,
  1055. <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> or <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> to avoid
  1056. deadlocks due to any of the other OS pipe buffers filling up and blocking the
  1057. child process.</p>
  1058. </div>
  1059. <dl class="py attribute">
  1060. <dt class="sig sig-object py" id="subprocess.Popen.pid">
  1061. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">pid</span></span><a class="headerlink" href="#subprocess.Popen.pid" title="Link to this definition">¶</a></dt>
  1062. <dd><p>The process ID of the child process.</p>
  1063. <p>Note that if you set the <em>shell</em> argument to <code class="docutils literal notranslate"><span class="pre">True</span></code>, this is the process ID
  1064. of the spawned shell.</p>
  1065. </dd></dl>
  1066. <dl class="py attribute">
  1067. <dt class="sig sig-object py" id="subprocess.Popen.returncode">
  1068. <span class="sig-prename descclassname"><span class="pre">Popen.</span></span><span class="sig-name descname"><span class="pre">returncode</span></span><a class="headerlink" href="#subprocess.Popen.returncode" title="Link to this definition">¶</a></dt>
  1069. <dd><p>The child return code. Initially <code class="docutils literal notranslate"><span class="pre">None</span></code>, <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> is set by
  1070. a call to the <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>, or <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> methods
  1071. if they detect that the process has terminated.</p>
  1072. <p>A <code class="docutils literal notranslate"><span class="pre">None</span></code> value indicates that the process hadn’t yet terminated at the
  1073. time of the last method call.</p>
  1074. <p>A negative value <code class="docutils literal notranslate"><span class="pre">-N</span></code> indicates that the child was terminated by signal
  1075. <code class="docutils literal notranslate"><span class="pre">N</span></code> (POSIX only).</p>
  1076. </dd></dl>
  1077. </section>
  1078. <section id="windows-popen-helpers">
  1079. <h2>Windows Popen Helpers<a class="headerlink" href="#windows-popen-helpers" title="Link to this heading">¶</a></h2>
  1080. <p>The <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> class and following constants are only available
  1081. on Windows.</p>
  1082. <dl class="py class">
  1083. <dt class="sig sig-object py" id="subprocess.STARTUPINFO">
  1084. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STARTUPINFO</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dwFlags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hStdInput</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hStdOutput</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hStdError</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wShowWindow</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lpAttributeList</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.STARTUPINFO" title="Link to this definition">¶</a></dt>
  1085. <dd><p>Partial support of the Windows
  1086. <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms686331(v=vs.85).aspx">STARTUPINFO</a>
  1087. structure is used for <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> creation. The following attributes can
  1088. be set by passing them as keyword-only arguments.</p>
  1089. <div class="versionchanged">
  1090. <p><span class="versionmodified changed">Changed in version 3.7: </span>Keyword-only argument support was added.</p>
  1091. </div>
  1092. <dl class="py attribute">
  1093. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.dwFlags">
  1094. <span class="sig-name descname"><span class="pre">dwFlags</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.dwFlags" title="Link to this definition">¶</a></dt>
  1095. <dd><p>A bit field that determines whether certain <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>
  1096. attributes are used when the process creates a window.</p>
  1097. <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>
  1098. <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>
  1099. </pre></div>
  1100. </div>
  1101. </dd></dl>
  1102. <dl class="py attribute">
  1103. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.hStdInput">
  1104. <span class="sig-name descname"><span class="pre">hStdInput</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.hStdInput" title="Link to this definition">¶</a></dt>
  1105. <dd><p>If <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> specifies <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>, this attribute
  1106. is the standard input handle for the process. If
  1107. <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> is not specified, the default for standard
  1108. input is the keyboard buffer.</p>
  1109. </dd></dl>
  1110. <dl class="py attribute">
  1111. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.hStdOutput">
  1112. <span class="sig-name descname"><span class="pre">hStdOutput</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.hStdOutput" title="Link to this definition">¶</a></dt>
  1113. <dd><p>If <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> specifies <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>, this attribute
  1114. is the standard output handle for the process. Otherwise, this attribute
  1115. is ignored and the default for standard output is the console window’s
  1116. buffer.</p>
  1117. </dd></dl>
  1118. <dl class="py attribute">
  1119. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.hStdError">
  1120. <span class="sig-name descname"><span class="pre">hStdError</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.hStdError" title="Link to this definition">¶</a></dt>
  1121. <dd><p>If <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> specifies <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>, this attribute
  1122. is the standard error handle for the process. Otherwise, this attribute is
  1123. ignored and the default for standard error is the console window’s buffer.</p>
  1124. </dd></dl>
  1125. <dl class="py attribute">
  1126. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.wShowWindow">
  1127. <span class="sig-name descname"><span class="pre">wShowWindow</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.wShowWindow" title="Link to this definition">¶</a></dt>
  1128. <dd><p>If <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> specifies <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>, this attribute
  1129. can be any of the values that can be specified in the <code class="docutils literal notranslate"><span class="pre">nCmdShow</span></code>
  1130. parameter for the
  1131. <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx">ShowWindow</a>
  1132. function, except for <code class="docutils literal notranslate"><span class="pre">SW_SHOWDEFAULT</span></code>. Otherwise, this attribute is
  1133. ignored.</p>
  1134. <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> is provided for this attribute. It is used when
  1135. <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> is called with <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>.</p>
  1136. </dd></dl>
  1137. <dl class="py attribute">
  1138. <dt class="sig sig-object py" id="subprocess.STARTUPINFO.lpAttributeList">
  1139. <span class="sig-name descname"><span class="pre">lpAttributeList</span></span><a class="headerlink" href="#subprocess.STARTUPINFO.lpAttributeList" title="Link to this definition">¶</a></dt>
  1140. <dd><p>A dictionary of additional attributes for process creation as given in
  1141. <code class="docutils literal notranslate"><span class="pre">STARTUPINFOEX</span></code>, see
  1142. <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms686880(v=vs.85).aspx">UpdateProcThreadAttribute</a>.</p>
  1143. <p>Supported attributes:</p>
  1144. <dl>
  1145. <dt><strong>handle_list</strong></dt><dd><p>Sequence of handles that will be inherited. <em>close_fds</em> must be true if
  1146. non-empty.</p>
  1147. <p>The handles must be temporarily made inheritable by
  1148. <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> when passed to the <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>
  1149. constructor, else <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> will be raised with Windows error
  1150. <code class="docutils literal notranslate"><span class="pre">ERROR_INVALID_PARAMETER</span></code> (87).</p>
  1151. <div class="admonition warning">
  1152. <p class="admonition-title">Warning</p>
  1153. <p>In a multithreaded process, use caution to avoid leaking handles
  1154. that are marked inheritable when combining this feature with
  1155. concurrent calls to other process creation functions that inherit
  1156. all handles such as <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>. This also applies to
  1157. standard handle redirection, which temporarily creates inheritable
  1158. handles.</p>
  1159. </div>
  1160. </dd>
  1161. </dl>
  1162. <div class="versionadded">
  1163. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1164. </div>
  1165. </dd></dl>
  1166. </dd></dl>
  1167. <section id="windows-constants">
  1168. <h3>Windows Constants<a class="headerlink" href="#windows-constants" title="Link to this heading">¶</a></h3>
  1169. <p>The <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> module exposes the following constants.</p>
  1170. <dl class="py data">
  1171. <dt class="sig sig-object py" id="subprocess.STD_INPUT_HANDLE">
  1172. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STD_INPUT_HANDLE</span></span><a class="headerlink" href="#subprocess.STD_INPUT_HANDLE" title="Link to this definition">¶</a></dt>
  1173. <dd><p>The standard input device. Initially, this is the console input buffer,
  1174. <code class="docutils literal notranslate"><span class="pre">CONIN$</span></code>.</p>
  1175. </dd></dl>
  1176. <dl class="py data">
  1177. <dt class="sig sig-object py" id="subprocess.STD_OUTPUT_HANDLE">
  1178. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STD_OUTPUT_HANDLE</span></span><a class="headerlink" href="#subprocess.STD_OUTPUT_HANDLE" title="Link to this definition">¶</a></dt>
  1179. <dd><p>The standard output device. Initially, this is the active console screen
  1180. buffer, <code class="docutils literal notranslate"><span class="pre">CONOUT$</span></code>.</p>
  1181. </dd></dl>
  1182. <dl class="py data">
  1183. <dt class="sig sig-object py" id="subprocess.STD_ERROR_HANDLE">
  1184. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STD_ERROR_HANDLE</span></span><a class="headerlink" href="#subprocess.STD_ERROR_HANDLE" title="Link to this definition">¶</a></dt>
  1185. <dd><p>The standard error device. Initially, this is the active console screen
  1186. buffer, <code class="docutils literal notranslate"><span class="pre">CONOUT$</span></code>.</p>
  1187. </dd></dl>
  1188. <dl class="py data">
  1189. <dt class="sig sig-object py" id="subprocess.SW_HIDE">
  1190. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">SW_HIDE</span></span><a class="headerlink" href="#subprocess.SW_HIDE" title="Link to this definition">¶</a></dt>
  1191. <dd><p>Hides the window. Another window will be activated.</p>
  1192. </dd></dl>
  1193. <dl class="py data">
  1194. <dt class="sig sig-object py" id="subprocess.STARTF_USESTDHANDLES">
  1195. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STARTF_USESTDHANDLES</span></span><a class="headerlink" href="#subprocess.STARTF_USESTDHANDLES" title="Link to this definition">¶</a></dt>
  1196. <dd><p>Specifies that the <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>,
  1197. <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>, and <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> attributes
  1198. contain additional information.</p>
  1199. </dd></dl>
  1200. <dl class="py data">
  1201. <dt class="sig sig-object py" id="subprocess.STARTF_USESHOWWINDOW">
  1202. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">STARTF_USESHOWWINDOW</span></span><a class="headerlink" href="#subprocess.STARTF_USESHOWWINDOW" title="Link to this definition">¶</a></dt>
  1203. <dd><p>Specifies that the <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> attribute contains
  1204. additional information.</p>
  1205. </dd></dl>
  1206. <dl class="py data">
  1207. <dt class="sig sig-object py" id="subprocess.CREATE_NEW_CONSOLE">
  1208. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CREATE_NEW_CONSOLE</span></span><a class="headerlink" href="#subprocess.CREATE_NEW_CONSOLE" title="Link to this definition">¶</a></dt>
  1209. <dd><p>The new process has a new console, instead of inheriting its parent’s
  1210. console (the default).</p>
  1211. </dd></dl>
  1212. <dl class="py data">
  1213. <dt class="sig sig-object py" id="subprocess.CREATE_NEW_PROCESS_GROUP">
  1214. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></span><a class="headerlink" href="#subprocess.CREATE_NEW_PROCESS_GROUP" title="Link to this definition">¶</a></dt>
  1215. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1216. group will be created. This flag is necessary for using <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>
  1217. on the subprocess.</p>
  1218. <p>This flag is ignored if <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> is specified.</p>
  1219. </dd></dl>
  1220. <dl class="py data">
  1221. <dt class="sig sig-object py" id="subprocess.ABOVE_NORMAL_PRIORITY_CLASS">
  1222. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">ABOVE_NORMAL_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.ABOVE_NORMAL_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1223. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1224. will have an above average priority.</p>
  1225. <div class="versionadded">
  1226. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1227. </div>
  1228. </dd></dl>
  1229. <dl class="py data">
  1230. <dt class="sig sig-object py" id="subprocess.BELOW_NORMAL_PRIORITY_CLASS">
  1231. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">BELOW_NORMAL_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.BELOW_NORMAL_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1232. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1233. will have a below average priority.</p>
  1234. <div class="versionadded">
  1235. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1236. </div>
  1237. </dd></dl>
  1238. <dl class="py data">
  1239. <dt class="sig sig-object py" id="subprocess.HIGH_PRIORITY_CLASS">
  1240. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">HIGH_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.HIGH_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1241. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1242. will have a high priority.</p>
  1243. <div class="versionadded">
  1244. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1245. </div>
  1246. </dd></dl>
  1247. <dl class="py data">
  1248. <dt class="sig sig-object py" id="subprocess.IDLE_PRIORITY_CLASS">
  1249. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">IDLE_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.IDLE_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1250. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1251. will have an idle (lowest) priority.</p>
  1252. <div class="versionadded">
  1253. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1254. </div>
  1255. </dd></dl>
  1256. <dl class="py data">
  1257. <dt class="sig sig-object py" id="subprocess.NORMAL_PRIORITY_CLASS">
  1258. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">NORMAL_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.NORMAL_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1259. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1260. will have an normal priority. (default)</p>
  1261. <div class="versionadded">
  1262. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1263. </div>
  1264. </dd></dl>
  1265. <dl class="py data">
  1266. <dt class="sig sig-object py" id="subprocess.REALTIME_PRIORITY_CLASS">
  1267. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">REALTIME_PRIORITY_CLASS</span></span><a class="headerlink" href="#subprocess.REALTIME_PRIORITY_CLASS" title="Link to this definition">¶</a></dt>
  1268. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1269. will have realtime priority.
  1270. You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts
  1271. system threads that manage mouse input, keyboard input, and background disk
  1272. flushing. This class can be appropriate for applications that “talk” directly
  1273. to hardware or that perform brief tasks that should have limited interruptions.</p>
  1274. <div class="versionadded">
  1275. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1276. </div>
  1277. </dd></dl>
  1278. <dl class="py data">
  1279. <dt class="sig sig-object py" id="subprocess.CREATE_NO_WINDOW">
  1280. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CREATE_NO_WINDOW</span></span><a class="headerlink" href="#subprocess.CREATE_NO_WINDOW" title="Link to this definition">¶</a></dt>
  1281. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1282. will not create a window.</p>
  1283. <div class="versionadded">
  1284. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1285. </div>
  1286. </dd></dl>
  1287. <dl class="py data">
  1288. <dt class="sig sig-object py" id="subprocess.DETACHED_PROCESS">
  1289. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">DETACHED_PROCESS</span></span><a class="headerlink" href="#subprocess.DETACHED_PROCESS" title="Link to this definition">¶</a></dt>
  1290. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1291. will not inherit its parent’s console.
  1292. This value cannot be used with CREATE_NEW_CONSOLE.</p>
  1293. <div class="versionadded">
  1294. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1295. </div>
  1296. </dd></dl>
  1297. <dl class="py data">
  1298. <dt class="sig sig-object py" id="subprocess.CREATE_DEFAULT_ERROR_MODE">
  1299. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CREATE_DEFAULT_ERROR_MODE</span></span><a class="headerlink" href="#subprocess.CREATE_DEFAULT_ERROR_MODE" title="Link to this definition">¶</a></dt>
  1300. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1301. does not inherit the error mode of the calling process. Instead, the new
  1302. process gets the default error mode.
  1303. This feature is particularly useful for multithreaded shell applications
  1304. that run with hard errors disabled.</p>
  1305. <div class="versionadded">
  1306. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1307. </div>
  1308. </dd></dl>
  1309. <dl class="py data">
  1310. <dt class="sig sig-object py" id="subprocess.CREATE_BREAKAWAY_FROM_JOB">
  1311. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">CREATE_BREAKAWAY_FROM_JOB</span></span><a class="headerlink" href="#subprocess.CREATE_BREAKAWAY_FROM_JOB" title="Link to this definition">¶</a></dt>
  1312. <dd><p>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">creationflags</span></code> parameter to specify that a new process
  1313. is not associated with the job.</p>
  1314. <div class="versionadded">
  1315. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1316. </div>
  1317. </dd></dl>
  1318. </section>
  1319. </section>
  1320. <section id="older-high-level-api">
  1321. <span id="call-function-trio"></span><h2>Older high-level API<a class="headerlink" href="#older-high-level-api" title="Link to this heading">¶</a></h2>
  1322. <p>Prior to Python 3.5, these three functions comprised the high level API to
  1323. subprocess. You can now use <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> in many cases, but lots of existing code
  1324. calls these functions.</p>
  1325. <dl class="py function">
  1326. <dt class="sig sig-object py" id="subprocess.call">
  1327. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">call</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shell</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">other_popen_kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.call" title="Link to this definition">¶</a></dt>
  1328. <dd><p>Run the command described by <em>args</em>. Wait for command to complete, then
  1329. return the <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> attribute.</p>
  1330. <p>Code needing to capture stdout or stderr should use <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> instead:</p>
  1331. <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>
  1332. </pre></div>
  1333. </div>
  1334. <p>To suppress stdout or stderr, supply a value of <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>
  1335. <p>The arguments shown above are merely some common ones.
  1336. The full function signature is the
  1337. same as that of the <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> constructor - this function passes all
  1338. supplied arguments other than <em>timeout</em> directly through to that interface.</p>
  1339. <div class="admonition note">
  1340. <p class="admonition-title">Note</p>
  1341. <p>Do not use <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> or <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> with this
  1342. function. The child process will block if it generates enough
  1343. output to a pipe to fill up the OS pipe buffer as the pipes are
  1344. not being read from.</p>
  1345. </div>
  1346. <div class="versionchanged">
  1347. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>timeout</em> was added.</p>
  1348. </div>
  1349. <div class="versionchanged">
  1350. <p><span class="versionmodified changed">Changed in version 3.12: </span>Changed Windows shell search order for <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>. The current
  1351. directory and <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> are replaced with <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> and
  1352. <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>. As a result, dropping a
  1353. malicious program named <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> into a current directory no
  1354. longer works.</p>
  1355. </div>
  1356. </dd></dl>
  1357. <dl class="py function">
  1358. <dt class="sig sig-object py" id="subprocess.check_call">
  1359. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">check_call</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shell</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">other_popen_kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.check_call" title="Link to this definition">¶</a></dt>
  1360. <dd><p>Run command with arguments. Wait for command to complete. If the return
  1361. code was zero then return, otherwise raise <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>. The
  1362. <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> object will have the return code in the
  1363. <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> attribute.
  1364. If <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> was unable to start the process it will propagate the exception
  1365. that was raised.</p>
  1366. <p>Code needing to capture stdout or stderr should use <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> instead:</p>
  1367. <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>
  1368. </pre></div>
  1369. </div>
  1370. <p>To suppress stdout or stderr, supply a value of <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>
  1371. <p>The arguments shown above are merely some common ones.
  1372. The full function signature is the
  1373. same as that of the <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> constructor - this function passes all
  1374. supplied arguments other than <em>timeout</em> directly through to that interface.</p>
  1375. <div class="admonition note">
  1376. <p class="admonition-title">Note</p>
  1377. <p>Do not use <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> or <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> with this
  1378. function. The child process will block if it generates enough
  1379. output to a pipe to fill up the OS pipe buffer as the pipes are
  1380. not being read from.</p>
  1381. </div>
  1382. <div class="versionchanged">
  1383. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>timeout</em> was added.</p>
  1384. </div>
  1385. <div class="versionchanged">
  1386. <p><span class="versionmodified changed">Changed in version 3.12: </span>Changed Windows shell search order for <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>. The current
  1387. directory and <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> are replaced with <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> and
  1388. <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>. As a result, dropping a
  1389. malicious program named <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> into a current directory no
  1390. longer works.</p>
  1391. </div>
  1392. </dd></dl>
  1393. <dl class="py function">
  1394. <dt class="sig sig-object py" id="subprocess.check_output">
  1395. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">check_output</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shell</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">universal_newlines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">other_popen_kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.check_output" title="Link to this definition">¶</a></dt>
  1396. <dd><p>Run command with arguments and return its output.</p>
  1397. <p>If the return code was non-zero it raises 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>. The
  1398. <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> object will have the return code in the
  1399. <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> attribute and any output in the
  1400. <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> attribute.</p>
  1401. <p>This is equivalent to:</p>
  1402. <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>
  1403. </pre></div>
  1404. </div>
  1405. <p>The arguments shown above are merely some common ones.
  1406. The full function signature is largely the same as that of <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> -
  1407. most arguments are passed directly through to that interface.
  1408. One API deviation from <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> behavior exists: passing <code class="docutils literal notranslate"><span class="pre">input=None</span></code>
  1409. will behave the same as <code class="docutils literal notranslate"><span class="pre">input=b''</span></code> (or <code class="docutils literal notranslate"><span class="pre">input=''</span></code>, depending on other
  1410. arguments) rather than using the parent’s standard input file handle.</p>
  1411. <p>By default, this function will return the data as encoded bytes. The actual
  1412. encoding of the output data may depend on the command being invoked, so the
  1413. decoding to text will often need to be handled at the application level.</p>
  1414. <p>This behaviour may be overridden by setting <em>text</em>, <em>encoding</em>, <em>errors</em>,
  1415. or <em>universal_newlines</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> as described in
  1416. <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">Frequently Used Arguments</span></a> and <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>
  1417. <p>To also capture standard error in the result, use
  1418. <code class="docutils literal notranslate"><span class="pre">stderr=subprocess.STDOUT</span></code>:</p>
  1419. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">(</span>
  1420. <span class="gp">... </span> <span class="s2">&quot;ls non_existent_file; exit 0&quot;</span><span class="p">,</span>
  1421. <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>
  1422. <span class="gp">... </span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  1423. <span class="go">&#39;ls: non_existent_file: No such file or directory\n&#39;</span>
  1424. </pre></div>
  1425. </div>
  1426. <div class="versionadded">
  1427. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1428. </div>
  1429. <div class="versionchanged">
  1430. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>timeout</em> was added.</p>
  1431. </div>
  1432. <div class="versionchanged">
  1433. <p><span class="versionmodified changed">Changed in version 3.4: </span>Support for the <em>input</em> keyword argument was added.</p>
  1434. </div>
  1435. <div class="versionchanged">
  1436. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>encoding</em> and <em>errors</em> were added. See <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> for details.</p>
  1437. </div>
  1438. <div class="versionadded">
  1439. <p><span class="versionmodified added">New in version 3.7: </span><em>text</em> was added as a more readable alias for <em>universal_newlines</em>.</p>
  1440. </div>
  1441. <div class="versionchanged">
  1442. <p><span class="versionmodified changed">Changed in version 3.12: </span>Changed Windows shell search order for <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>. The current
  1443. directory and <code class="docutils literal notranslate"><span class="pre">%PATH%</span></code> are replaced with <code class="docutils literal notranslate"><span class="pre">%COMSPEC%</span></code> and
  1444. <code class="docutils literal notranslate"><span class="pre">%SystemRoot%\System32\cmd.exe</span></code>. As a result, dropping a
  1445. malicious program named <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code> into a current directory no
  1446. longer works.</p>
  1447. </div>
  1448. </dd></dl>
  1449. </section>
  1450. <section id="replacing-older-functions-with-the-subprocess-module">
  1451. <span id="subprocess-replacements"></span><h2>Replacing Older Functions with the <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> Module<a class="headerlink" href="#replacing-older-functions-with-the-subprocess-module" title="Link to this heading">¶</a></h2>
  1452. <p>In this section, “a becomes b” means that b can be used as a replacement for a.</p>
  1453. <div class="admonition note">
  1454. <p class="admonition-title">Note</p>
  1455. <p>All “a” functions in this section fail (more or less) silently if the
  1456. executed program cannot be found; the “b” replacements raise <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>
  1457. instead.</p>
  1458. <p>In addition, the replacements using <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> will fail with a
  1459. <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> if the requested operation produces a non-zero
  1460. return code. The output is still available as the
  1461. <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> attribute of the raised exception.</p>
  1462. </div>
  1463. <p>In the following examples, we assume that the relevant functions have already
  1464. been imported from the <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> module.</p>
  1465. <section id="replacing-bin-sh-shell-command-substitution">
  1466. <h3>Replacing <strong class="program">/bin/sh</strong> shell command substitution<a class="headerlink" href="#replacing-bin-sh-shell-command-substitution" title="Link to this heading">¶</a></h3>
  1467. <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>
  1468. </pre></div>
  1469. </div>
  1470. <p>becomes:</p>
  1471. <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">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">])</span>
  1472. </pre></div>
  1473. </div>
  1474. </section>
  1475. <section id="replacing-shell-pipeline">
  1476. <h3>Replacing shell pipeline<a class="headerlink" href="#replacing-shell-pipeline" title="Link to this heading">¶</a></h3>
  1477. <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>
  1478. </pre></div>
  1479. </div>
  1480. <p>becomes:</p>
  1481. <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">&quot;dmesg&quot;</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
  1482. <span class="n">p2</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;grep&quot;</span><span class="p">,</span> <span class="s2">&quot;hda&quot;</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>
  1483. <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>
  1484. <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>
  1485. </pre></div>
  1486. </div>
  1487. <p>The <code class="docutils literal notranslate"><span class="pre">p1.stdout.close()</span></code> call after starting the p2 is important in order for
  1488. p1 to receive a SIGPIPE if p2 exits before p1.</p>
  1489. <p>Alternatively, for trusted input, the shell’s own pipeline support may still
  1490. be used directly:</p>
  1491. <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>
  1492. </pre></div>
  1493. </div>
  1494. <p>becomes:</p>
  1495. <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">&quot;dmesg | grep hda&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  1496. </pre></div>
  1497. </div>
  1498. </section>
  1499. <section id="replacing-os-system">
  1500. <h3>Replacing <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="Link to this heading">¶</a></h3>
  1501. <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">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s2">&quot; myarg&quot;</span><span class="p">)</span>
  1502. <span class="c1"># becomes</span>
  1503. <span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">(</span><span class="s2">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s2">&quot; myarg&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  1504. </pre></div>
  1505. </div>
  1506. <p>Notes:</p>
  1507. <ul class="simple">
  1508. <li><p>Calling the program through the shell is usually not required.</p></li>
  1509. <li><p>The <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> return value is encoded differently to that of
  1510. <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></li>
  1511. <li><p>The <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> function ignores SIGINT and SIGQUIT signals while
  1512. the command is running, but the caller must do this separately when
  1513. using the <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> module.</p></li>
  1514. </ul>
  1515. <p>A more realistic example would look like this:</p>
  1516. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  1517. <span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">(</span><span class="s2">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s2">&quot; myarg&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  1518. <span class="k">if</span> <span class="n">retcode</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
  1519. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Child was terminated by signal&quot;</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>
  1520. <span class="k">else</span><span class="p">:</span>
  1521. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Child returned&quot;</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>
  1522. <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  1523. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Execution failed:&quot;</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>
  1524. </pre></div>
  1525. </div>
  1526. </section>
  1527. <section id="replacing-the-os-spawn-family">
  1528. <h3>Replacing the <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> family<a class="headerlink" href="#replacing-the-os-spawn-family" title="Link to this heading">¶</a></h3>
  1529. <p>P_NOWAIT example:</p>
  1530. <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">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">)</span>
  1531. <span class="o">==&gt;</span>
  1532. <span class="n">pid</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">])</span><span class="o">.</span><span class="n">pid</span>
  1533. </pre></div>
  1534. </div>
  1535. <p>P_WAIT example:</p>
  1536. <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">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">)</span>
  1537. <span class="o">==&gt;</span>
  1538. <span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">([</span><span class="s2">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">])</span>
  1539. </pre></div>
  1540. </div>
  1541. <p>Vector example:</p>
  1542. <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>
  1543. <span class="o">==&gt;</span>
  1544. <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>
  1545. </pre></div>
  1546. </div>
  1547. <p>Environment example:</p>
  1548. <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">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
  1549. <span class="o">==&gt;</span>
  1550. <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">env</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;PATH&quot;</span><span class="p">:</span> <span class="s2">&quot;/usr/bin&quot;</span><span class="p">})</span>
  1551. </pre></div>
  1552. </div>
  1553. </section>
  1554. <section id="replacing-os-popen-os-popen2-os-popen3">
  1555. <h3>Replacing <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="Link to this heading">¶</a></h3>
  1556. <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>
  1557. <span class="o">==&gt;</span>
  1558. <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>
  1559. <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>
  1560. <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>
  1561. </pre></div>
  1562. </div>
  1563. <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>
  1564. <span class="n">child_stdout</span><span class="p">,</span>
  1565. <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>
  1566. <span class="o">==&gt;</span>
  1567. <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>
  1568. <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>
  1569. <span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span>
  1570. <span class="n">child_stdout</span><span class="p">,</span>
  1571. <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>
  1572. </pre></div>
  1573. </div>
  1574. <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>
  1575. <span class="o">==&gt;</span>
  1576. <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>
  1577. <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>
  1578. <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>
  1579. </pre></div>
  1580. </div>
  1581. <p>Return code handling translates as follows:</p>
  1582. <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">&#39;w&#39;</span><span class="p">)</span>
  1583. <span class="o">...</span>
  1584. <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>
  1585. <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">&gt;&gt;</span> <span class="mi">8</span><span class="p">:</span>
  1586. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;There were some errors&quot;</span><span class="p">)</span>
  1587. <span class="o">==&gt;</span>
  1588. <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>
  1589. <span class="o">...</span>
  1590. <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>
  1591. <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>
  1592. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;There were some errors&quot;</span><span class="p">)</span>
  1593. </pre></div>
  1594. </div>
  1595. </section>
  1596. <section id="replacing-functions-from-the-popen2-module">
  1597. <h3>Replacing functions from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> module<a class="headerlink" href="#replacing-functions-from-the-popen2-module" title="Link to this heading">¶</a></h3>
  1598. <div class="admonition note">
  1599. <p class="admonition-title">Note</p>
  1600. <p>If the cmd argument to popen2 functions is a string, the command is executed
  1601. through /bin/sh. If it is a list, the command is directly executed.</p>
  1602. </div>
  1603. <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">&quot;somestring&quot;</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
  1604. <span class="o">==&gt;</span>
  1605. <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="s2">&quot;somestring&quot;</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>
  1606. <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>
  1607. <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>
  1608. </pre></div>
  1609. </div>
  1610. <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">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
  1611. <span class="o">==&gt;</span>
  1612. <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s2">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
  1613. <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>
  1614. <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>
  1615. </pre></div>
  1616. </div>
  1617. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">popen2.Popen3</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">popen2.Popen4</span></code> basically work as
  1618. <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>, except that:</p>
  1619. <ul class="simple">
  1620. <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> raises an exception if the execution fails.</p></li>
  1621. <li><p>The <em>capturestderr</em> argument is replaced with the <em>stderr</em> argument.</p></li>
  1622. <li><p><code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code> and <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> must be specified.</p></li>
  1623. <li><p>popen2 closes all file descriptors by default, but you have to specify
  1624. <code class="docutils literal notranslate"><span class="pre">close_fds=True</span></code> with <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> to guarantee this behavior on
  1625. all platforms or past Python versions.</p></li>
  1626. </ul>
  1627. </section>
  1628. </section>
  1629. <section id="legacy-shell-invocation-functions">
  1630. <h2>Legacy Shell Invocation Functions<a class="headerlink" href="#legacy-shell-invocation-functions" title="Link to this heading">¶</a></h2>
  1631. <p>This module also provides the following legacy functions from the 2.x
  1632. <code class="docutils literal notranslate"><span class="pre">commands</span></code> module. These operations implicitly invoke the system shell and
  1633. none of the guarantees described above regarding security and exception
  1634. handling consistency are valid for these functions.</p>
  1635. <dl class="py function">
  1636. <dt class="sig sig-object py" id="subprocess.getstatusoutput">
  1637. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">getstatusoutput</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.getstatusoutput" title="Link to this definition">¶</a></dt>
  1638. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">(exitcode,</span> <span class="pre">output)</span></code> of executing <em>cmd</em> in a shell.</p>
  1639. <p>Execute the string <em>cmd</em> in a shell with <code class="xref py py-meth docutils literal notranslate"><span class="pre">Popen.check_output()</span></code> and
  1640. return a 2-tuple <code class="docutils literal notranslate"><span class="pre">(exitcode,</span> <span class="pre">output)</span></code>.
  1641. <em>encoding</em> and <em>errors</em> are used to decode output;
  1642. see the notes on <a class="reference internal" href="#frequently-used-arguments"><span class="std std-ref">Frequently Used Arguments</span></a> for more details.</p>
  1643. <p>A trailing newline is stripped from the output.
  1644. The exit code for the command can be interpreted as the return code
  1645. of subprocess. Example:</p>
  1646. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
  1647. <span class="go">(0, &#39;/bin/ls&#39;)</span>
  1648. <span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">&#39;cat /bin/junk&#39;</span><span class="p">)</span>
  1649. <span class="go">(1, &#39;cat: /bin/junk: No such file or directory&#39;)</span>
  1650. <span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">&#39;/bin/junk&#39;</span><span class="p">)</span>
  1651. <span class="go">(127, &#39;sh: /bin/junk: not found&#39;)</span>
  1652. <span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s1">&#39;/bin/kill $$&#39;</span><span class="p">)</span>
  1653. <span class="go">(-15, &#39;&#39;)</span>
  1654. </pre></div>
  1655. </div>
  1656. <div class="availability docutils container">
  1657. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1658. </div>
  1659. <div class="versionchanged">
  1660. <p><span class="versionmodified changed">Changed in version 3.3.4: </span>Windows support was added.</p>
  1661. <p>The function now returns (exitcode, output) instead of (status, output)
  1662. as it did in Python 3.3.3 and earlier. exitcode has the same value as
  1663. <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>
  1664. </div>
  1665. <div class="versionchanged">
  1666. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>encoding</em> and <em>errors</em> parameters.</p>
  1667. </div>
  1668. </dd></dl>
  1669. <dl class="py function">
  1670. <dt class="sig sig-object py" id="subprocess.getoutput">
  1671. <span class="sig-prename descclassname"><span class="pre">subprocess.</span></span><span class="sig-name descname"><span class="pre">getoutput</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#subprocess.getoutput" title="Link to this definition">¶</a></dt>
  1672. <dd><p>Return output (stdout and stderr) of executing <em>cmd</em> in a shell.</p>
  1673. <p>Like <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>, except the exit code is ignored and the return
  1674. value is a string containing the command’s output. Example:</p>
  1675. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getoutput</span><span class="p">(</span><span class="s1">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
  1676. <span class="go">&#39;/bin/ls&#39;</span>
  1677. </pre></div>
  1678. </div>
  1679. <div class="availability docutils container">
  1680. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1681. </div>
  1682. <div class="versionchanged">
  1683. <p><span class="versionmodified changed">Changed in version 3.3.4: </span>Windows support added</p>
  1684. </div>
  1685. <div class="versionchanged">
  1686. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>encoding</em> and <em>errors</em> parameters.</p>
  1687. </div>
  1688. </dd></dl>
  1689. </section>
  1690. <section id="notes">
  1691. <h2>Notes<a class="headerlink" href="#notes" title="Link to this heading">¶</a></h2>
  1692. <section id="converting-an-argument-sequence-to-a-string-on-windows">
  1693. <span id="converting-argument-sequence"></span><h3>Converting an argument sequence to a string on Windows<a class="headerlink" href="#converting-an-argument-sequence-to-a-string-on-windows" title="Link to this heading">¶</a></h3>
  1694. <p>On Windows, an <em>args</em> sequence is converted to a string that can be parsed
  1695. using the following rules (which correspond to the rules used by the MS C
  1696. runtime):</p>
  1697. <ol class="arabic simple">
  1698. <li><p>Arguments are delimited by white space, which is either a
  1699. space or a tab.</p></li>
  1700. <li><p>A string surrounded by double quotation marks is
  1701. interpreted as a single argument, regardless of white space
  1702. contained within. A quoted string can be embedded in an
  1703. argument.</p></li>
  1704. <li><p>A double quotation mark preceded by a backslash is
  1705. interpreted as a literal double quotation mark.</p></li>
  1706. <li><p>Backslashes are interpreted literally, unless they
  1707. immediately precede a double quotation mark.</p></li>
  1708. <li><p>If backslashes immediately precede a double quotation mark,
  1709. every pair of backslashes is interpreted as a literal
  1710. backslash. If the number of backslashes is odd, the last
  1711. backslash escapes the next double quotation mark as
  1712. described in rule 3.</p></li>
  1713. </ol>
  1714. <div class="admonition seealso">
  1715. <p class="admonition-title">See also</p>
  1716. <dl class="simple">
  1717. <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>Module which provides function to parse and escape command lines.</p>
  1718. </dd>
  1719. </dl>
  1720. </div>
  1721. </section>
  1722. <section id="disabling-use-of-vfork-or-posix-spawn">
  1723. <span id="disable-posix-spawn"></span><span id="disable-vfork"></span><h3>Disabling use of <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> or <code class="docutils literal notranslate"><span class="pre">posix_spawn()</span></code><a class="headerlink" href="#disabling-use-of-vfork-or-posix-spawn" title="Link to this heading">¶</a></h3>
  1724. <p>On Linux, <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> defaults to using the <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> system call
  1725. internally when it is safe to do so rather than <code class="docutils literal notranslate"><span class="pre">fork()</span></code>. This greatly
  1726. improves performance.</p>
  1727. <p>If you ever encounter a presumed highly unusual situation where you need to
  1728. prevent <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> from being used by Python, you can set the
  1729. <code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess._USE_VFORK</span></code> attribute to a false value.</p>
  1730. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">subprocess</span><span class="o">.</span><span class="n">_USE_VFORK</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># See CPython issue gh-NNNNNN.</span>
  1731. </pre></div>
  1732. </div>
  1733. <p>Setting this has no impact on use of <code class="docutils literal notranslate"><span class="pre">posix_spawn()</span></code> which could use
  1734. <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> internally within its libc implementation. There is a similar
  1735. <code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess._USE_POSIX_SPAWN</span></code> attribute if you need to prevent use of
  1736. that.</p>
  1737. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">subprocess</span><span class="o">.</span><span class="n">_USE_POSIX_SPAWN</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># See CPython issue gh-NNNNNN.</span>
  1738. </pre></div>
  1739. </div>
  1740. <p>It is safe to set these to false on any Python version. They will have no
  1741. effect on older versions when unsupported. Do not assume the attributes are
  1742. available to read. Despite their names, a true value does not indicate that the
  1743. corresponding function will be used, only that it may be.</p>
  1744. <p>Please file issues any time you have to use these private knobs with a way to
  1745. reproduce the issue you were seeing. Link to that issue from a comment in your
  1746. code.</p>
  1747. <div class="versionadded">
  1748. <p><span class="versionmodified added">New in version 3.8: </span><code class="docutils literal notranslate"><span class="pre">_USE_POSIX_SPAWN</span></code></p>
  1749. </div>
  1750. <div class="versionadded">
  1751. <p><span class="versionmodified added">New in version 3.11: </span><code class="docutils literal notranslate"><span class="pre">_USE_VFORK</span></code></p>
  1752. </div>
  1753. </section>
  1754. </section>
  1755. </section>
  1756. <div class="clearer"></div>
  1757. </div>
  1758. </div>
  1759. </div>
  1760. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1761. <div class="sphinxsidebarwrapper">
  1762. <div>
  1763. <h3><a href="../contents.html">Table of Contents</a></h3>
  1764. <ul>
  1765. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a><ul>
  1766. <li><a class="reference internal" href="#using-the-subprocess-module">Using the <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> Module</a><ul>
  1767. <li><a class="reference internal" href="#frequently-used-arguments">Frequently Used Arguments</a></li>
  1768. <li><a class="reference internal" href="#popen-constructor">Popen Constructor</a></li>
  1769. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  1770. </ul>
  1771. </li>
  1772. <li><a class="reference internal" href="#security-considerations">Security Considerations</a></li>
  1773. <li><a class="reference internal" href="#popen-objects">Popen Objects</a></li>
  1774. <li><a class="reference internal" href="#windows-popen-helpers">Windows Popen Helpers</a><ul>
  1775. <li><a class="reference internal" href="#windows-constants">Windows Constants</a></li>
  1776. </ul>
  1777. </li>
  1778. <li><a class="reference internal" href="#older-high-level-api">Older high-level API</a></li>
  1779. <li><a class="reference internal" href="#replacing-older-functions-with-the-subprocess-module">Replacing Older Functions with the <code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> Module</a><ul>
  1780. <li><a class="reference internal" href="#replacing-bin-sh-shell-command-substitution">Replacing <strong class="program">/bin/sh</strong> shell command substitution</a></li>
  1781. <li><a class="reference internal" href="#replacing-shell-pipeline">Replacing shell pipeline</a></li>
  1782. <li><a class="reference internal" href="#replacing-os-system">Replacing <code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a></li>
  1783. <li><a class="reference internal" href="#replacing-the-os-spawn-family">Replacing the <code class="xref py py-func docutils literal notranslate"><span class="pre">os.spawn</span></code> family</a></li>
  1784. <li><a class="reference internal" href="#replacing-os-popen-os-popen2-os-popen3">Replacing <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>
  1785. <li><a class="reference internal" href="#replacing-functions-from-the-popen2-module">Replacing functions from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> module</a></li>
  1786. </ul>
  1787. </li>
  1788. <li><a class="reference internal" href="#legacy-shell-invocation-functions">Legacy Shell Invocation Functions</a></li>
  1789. <li><a class="reference internal" href="#notes">Notes</a><ul>
  1790. <li><a class="reference internal" href="#converting-an-argument-sequence-to-a-string-on-windows">Converting an argument sequence to a string on Windows</a></li>
  1791. <li><a class="reference internal" href="#disabling-use-of-vfork-or-posix-spawn">Disabling use of <code class="docutils literal notranslate"><span class="pre">vfork()</span></code> or <code class="docutils literal notranslate"><span class="pre">posix_spawn()</span></code></a></li>
  1792. </ul>
  1793. </li>
  1794. </ul>
  1795. </li>
  1796. </ul>
  1797. </div>
  1798. <div>
  1799. <h4>Previous topic</h4>
  1800. <p class="topless"><a href="concurrent.futures.html"
  1801. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a></p>
  1802. </div>
  1803. <div>
  1804. <h4>Next topic</h4>
  1805. <p class="topless"><a href="sched.html"
  1806. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> — Event scheduler</a></p>
  1807. </div>
  1808. <div role="note" aria-label="source link">
  1809. <h3>This Page</h3>
  1810. <ul class="this-page-menu">
  1811. <li><a href="../bugs.html">Report a Bug</a></li>
  1812. <li>
  1813. <a href="https://github.com/python/cpython/blob/main/Doc/library/subprocess.rst"
  1814. rel="nofollow">Show Source
  1815. </a>
  1816. </li>
  1817. </ul>
  1818. </div>
  1819. </div>
  1820. <div id="sidebarbutton" title="Collapse sidebar">
  1821. <span>«</span>
  1822. </div>
  1823. </div>
  1824. <div class="clearer"></div>
  1825. </div>
  1826. <div class="related" role="navigation" aria-label="related navigation">
  1827. <h3>Navigation</h3>
  1828. <ul>
  1829. <li class="right" style="margin-right: 10px">
  1830. <a href="../genindex.html" title="General Index"
  1831. >index</a></li>
  1832. <li class="right" >
  1833. <a href="../py-modindex.html" title="Python Module Index"
  1834. >modules</a> |</li>
  1835. <li class="right" >
  1836. <a href="sched.html" title="sched — Event scheduler"
  1837. >next</a> |</li>
  1838. <li class="right" >
  1839. <a href="concurrent.futures.html" title="concurrent.futures — Launching parallel tasks"
  1840. >previous</a> |</li>
  1841. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1842. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1843. <li class="switchers">
  1844. <div class="language_switcher_placeholder"></div>
  1845. <div class="version_switcher_placeholder"></div>
  1846. </li>
  1847. <li>
  1848. </li>
  1849. <li id="cpython-language-and-version">
  1850. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1851. </li>
  1852. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1853. <li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> &#187;</li>
  1854. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a></li>
  1855. <li class="right">
  1856. <div class="inline-search" role="search">
  1857. <form class="inline-search" action="../search.html" method="get">
  1858. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1859. <input type="submit" value="Go" />
  1860. </form>
  1861. </div>
  1862. |
  1863. </li>
  1864. <li class="right">
  1865. <label class="theme-selector-label">
  1866. Theme
  1867. <select class="theme-selector" oninput="activateTheme(this.value)">
  1868. <option value="auto" selected>Auto</option>
  1869. <option value="light">Light</option>
  1870. <option value="dark">Dark</option>
  1871. </select>
  1872. </label> |</li>
  1873. </ul>
  1874. </div>
  1875. <div class="footer">
  1876. &copy;
  1877. <a href="../copyright.html">
  1878. Copyright
  1879. </a>
  1880. 2001-2024, Python Software Foundation.
  1881. <br />
  1882. This page is licensed under the Python Software Foundation License Version 2.
  1883. <br />
  1884. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1885. <br />
  1886. See <a href="/license.html">History and License</a> for more information.<br />
  1887. <br />
  1888. The Python Software Foundation is a non-profit corporation.
  1889. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1890. <br />
  1891. <br />
  1892. Last updated on Apr 09, 2024 (13:47 UTC).
  1893. <a href="/bugs.html">Found a bug</a>?
  1894. <br />
  1895. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1896. </div>
  1897. </body>
  1898. </html>
上海开阖软件有限公司 沪ICP备12045867号-1