{"id":1188026,"date":"2025-01-15T20:12:17","date_gmt":"2025-01-15T12:12:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1188026.html"},"modified":"2025-01-15T20:12:19","modified_gmt":"2025-01-15T12:12:19","slug":"python%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e8%ae%b0%e4%ba%8b%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1188026.html","title":{"rendered":"python\u5982\u4f55\u8fd0\u884c\u8bb0\u4e8b\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140456\/38d304ee-fce4-4930-8c1d-6547fd662143.webp\" alt=\"python\u5982\u4f55\u8fd0\u884c\u8bb0\u4e8b\u672c\" \/><\/p>\n<p><p> <strong>Python\u8fd0\u884c\u8bb0\u4e8b\u672c\u7684\u65b9\u6cd5<\/strong>\u6709\uff1a\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>Popen<\/code>\u7c7b\u3002<strong>\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u63a5\u53e3\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002<strong>subprocess\u6a21\u5757\u66f4\u9002\u5408\u4e8e\u9700\u8981\u63a7\u5236\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u590d\u6742\u4efb\u52a1<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython 2.4\u4ee5\u540e\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u65b0\u7684\u8fdb\u7a0b\u3001\u8fde\u63a5\u5230\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\u5e76\u83b7\u5f97\u5b83\u4eec\u7684\u8fd4\u56de\u7801\u3002\u76f8\u6bd4\u4e8e<code>os.system<\/code>\uff0c<code>subprocess<\/code>\u6a21\u5757\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c<\/strong><\/h2>\n<p>subprocess.run([&#39;notepad.exe&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>subprocess.run<\/code>\u6765\u6267\u884c\u8bb0\u4e8b\u672c\u7a0b\u5e8f\u3002<code>subprocess.run<\/code>\u662f\u4e00\u4e2a\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\uff0c\u5b83\u4f1a\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6210\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<code>subprocess.run<\/code>\u662f\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u63a8\u8350\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u8bf8\u5982\u8d85\u65f6\u3001\u6355\u83b7\u8f93\u51fa\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>subprocess.run<\/code>\u7684\u53c2\u6570\u6765\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u8bb0\u4e8b\u672c\u5e76\u6355\u83b7\u5176\u8f93\u51fa<\/p>\n<p>result = subprocess.run([&#39;notepad.exe&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>capture_output=True<\/code>\u8868\u793a\u6211\u4eec\u5e0c\u671b\u6355\u83b7\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c<code>text=True<\/code>\u8868\u793a\u6211\u4eec\u5e0c\u671b\u5c06\u8f93\u51fa\u4f5c\u4e3a\u6587\u672c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u51fd\u6570\uff0c\u5176\u4e2d<code>os.system<\/code>\u53ef\u4ee5\u7528\u6765\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u3002\u867d\u7136<code>os.system<\/code>\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u8bb0\u4e8b\u672c\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u8f83\u4e3a\u7b80\u5355\uff0c\u65e0\u6cd5\u6355\u83b7\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c<\/strong><\/h2>\n<p>os.system(&#39;notepad.exe&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>os.system<\/code>\u53ef\u4ee5\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u4f46\u5b83\u7684\u5c40\u9650\u6027\u5728\u4e8e\u65e0\u6cd5\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u9ad8\u7ea7\u4ea4\u4e92\u3002\u56e0\u6b64\uff0c\u63a8\u8350\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Popen\u7c7b<\/h3>\n<\/p>\n<p><p><code>Popen<\/code>\u7c7b\u662f<code>subprocess<\/code>\u6a21\u5757\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u63a5\u53e3\u3002\u4f7f\u7528<code>Popen<\/code>\u7c7b\uff0c\u4f60\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;notepad.exe&#39;])<\/p>\n<h2><strong>\u7b49\u5f85\u8bb0\u4e8b\u672c\u5173\u95ed<\/strong><\/h2>\n<p>process.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Popen<\/code>\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4f7f\u7528<code>wait<\/code>\u65b9\u6cd5\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u590d\u6742\u4ea4\u4e92\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91casubprocess\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u65b9\u6cd5\u3002\u9664\u4e86<code>subprocess.run<\/code>\u548c<code>subprocess.Popen<\/code>\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001subprocess.run<\/h4>\n<\/p>\n<p><p><code>subprocess.run<\/code>\u662f\u4e00\u4e2a\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u5b50\u8fdb\u7a0b\u521b\u5efa\u9700\u6c42\u3002\u5b83\u4f1a\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = subprocess.run([&#39;notepad.exe&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001subprocess.Popen<\/h4>\n<\/p>\n<p><p><code>Popen<\/code>\u7c7b\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u5141\u8bb8\u4f60\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5b50\u8fdb\u7a0b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Popen<\/code>\u7684<code>stdin<\/code>, <code>stdout<\/code>, <code>stderr<\/code>\u53c2\u6570\u6765\u91cd\u5b9a\u5411\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">process = subprocess.Popen([&#39;notepad.exe&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout)<\/p>\n<p>print(stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001subprocess.call<\/h4>\n<\/p>\n<p><p><code>subprocess.call<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\uff0c\u7528\u4e8e\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u8fd4\u56de\u547d\u4ee4\u7684\u8fd4\u56de\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">return_code = subprocess.call([&#39;notepad.exe&#39;])<\/p>\n<p>print(return_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001subprocess.check_call<\/h4>\n<\/p>\n<p><p><code>subprocess.check_call<\/code>\u4e0e<code>subprocess.call<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u547d\u4ee4\u8fd4\u56de\u975e\u96f6\u8fd4\u56de\u7801\uff0c\u5b83\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    subprocess.check_call([&#39;notepad.exe&#39;])<\/p>\n<p>except subprocess.CalledProcessError as e:<\/p>\n<p>    print(f&quot;Command failed with return code {e.returncode}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001subprocess.check_output<\/h4>\n<\/p>\n<p><p><code>subprocess.check_output<\/code>\u8fd0\u884c\u547d\u4ee4\uff0c\u5e76\u8fd4\u56de\u5176\u8f93\u51fa\u3002\u5982\u679c\u547d\u4ee4\u8fd4\u56de\u975e\u96f6\u8fd4\u56de\u7801\uff0c\u5b83\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    output = subprocess.check_output([&#39;notepad.exe&#39;], text=True)<\/p>\n<p>    print(output)<\/p>\n<p>except subprocess.CalledProcessError as e:<\/p>\n<p>    print(f&quot;Command failed with return code {e.returncode}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fd0\u884c\u8bb0\u4e8b\u672c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<code>subprocess<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6700\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u63a5\u53e3\u3002<strong>\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u63a5\u53e3\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run<\/code>\u6216<code>subprocess.call<\/code>\u3002\u5bf9\u4e8e\u9700\u8981\u66f4\u591a\u63a7\u5236\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Popen<\/code>\u7c7b\u3002\u603b\u4e4b\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63a7\u5236\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u6253\u5f00\u8bb0\u4e8b\u672c\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u6216<code>subprocess<\/code>\u6a21\u5757\u6765\u6253\u5f00\u8bb0\u4e8b\u672c\u3002\u901a\u8fc7<code>os.startfile(&#39;notepad.exe&#39;)<\/code>\u53ef\u4ee5\u76f4\u63a5\u542f\u52a8\u8bb0\u4e8b\u672c\uff0c\u6216\u8005\u4f7f\u7528<code>subprocess.call([&#39;notepad.exe&#39;])<\/code>\u6765\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u65b9\u4fbf\u5730\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u7cfb\u7edf\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u521b\u5efa\u5e76\u4fdd\u5b58\u8bb0\u4e8b\u672c\u6587\u4ef6\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u521b\u5efa\u8bb0\u4e8b\u672c\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff08\u4f8b\u5982<code>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/code>\uff09\u6765\u521b\u5efa\u4e00\u4e2a\u8bb0\u4e8b\u672c\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>file.write(&#39;\u60a8\u7684\u5185\u5bb9&#39;)<\/code>\u5c06\u5185\u5bb9\u5199\u5165\u5176\u4e2d\u3002\u6267\u884c\u5b8c\u540e\uff0c\u8bb0\u4e8b\u672c\u6587\u4ef6\u4f1a\u88ab\u4fdd\u5b58\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8bb0\u4e8b\u672c\u4e2d\u6253\u5f00\u7279\u5b9a\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u8bb0\u4e8b\u672c\u4e2d\u6253\u5f00\u4e00\u4e2a\u7279\u5b9a\u7684\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528<code>os.startfile(&#39;path_to_your_file.txt&#39;)<\/code>\uff0c\u5c06<code>path_to_your_file.txt<\/code>\u66ff\u6362\u4e3a\u60a8\u6587\u4ef6\u7684\u5b9e\u9645\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u76f4\u63a5\u5728\u8bb0\u4e8b\u672c\u4e2d\u67e5\u770b\u6216\u7f16\u8f91\u6307\u5b9a\u7684\u6587\u672c\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fd0\u884c\u8bb0\u4e8b\u672c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528subprocess\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528Popen\u7c7b\u3002\u63a8\u8350\u4f7f\u7528subp [&hellip;]","protected":false},"author":3,"featured_media":1188034,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188026"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1188026"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188026\/revisions"}],"predecessor-version":[{"id":1188037,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188026\/revisions\/1188037"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188034"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}