{"id":1155593,"date":"2025-01-13T18:05:36","date_gmt":"2025-01-13T10:05:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155593.html"},"modified":"2025-01-13T18:05:39","modified_gmt":"2025-01-13T10:05:39","slug":"python%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e6%89%b9%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155593.html","title":{"rendered":"python\u5982\u4f55\u8fd0\u884c\u6279\u5904\u7406"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194214\/b2e99961-03c8-4dde-a530-0ddb9d733010.webp\" alt=\"python\u5982\u4f55\u8fd0\u884c\u6279\u5904\u7406\" \/><\/p>\n<p><p> <strong>Python \u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528 os \u6a21\u5757\u3001subprocess \u6a21\u5757\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982 Popen \u7b49\u3002\u5176\u4e2d\uff0c<strong>subprocess \u6a21\u5757\u88ab\u8ba4\u4e3a\u662f\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u7684\u6700\u4f73\u9009\u62e9<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u63a7\u5236\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 subprocess \u6a21\u5757\u6765\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001os \u6a21\u5757<\/h3>\n<\/p>\n<p><p>os \u6a21\u5757\u662f Python \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u5c3d\u7ba1 os \u6a21\u5757\u53ef\u4ee5\u7528\u6765\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u76f8\u5bf9\u8f83\u5c11\uff0c\u4e14\u4e0d\u63a8\u8350\u7528\u4e8e\u65b0\u9879\u76ee\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>os.system(&#39;path_to_your_batch_file.bat&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4e3b\u8981\u7f3a\u70b9\u662f\u65e0\u6cd5\u83b7\u53d6\u6279\u5904\u7406\u6587\u4ef6\u7684\u8f93\u51fa\uff0c\u4e5f\u4e0d\u80fd\u63a7\u5236\u5176\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001subprocess \u6a21\u5757<\/h3>\n<\/p>\n<p><p>subprocess \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u6765\u7ba1\u7406\u548c\u63a7\u5236\u5b50\u8fdb\u7a0b\u3002\u4f7f\u7528 subprocess \u6a21\u5757\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u6267\u884c\u6279\u5904\u7406\u6587\u4ef6\uff0c\u5e76\u4e14\u80fd\u591f\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. subprocess.run()<\/h4>\n<\/p>\n<p><p>\u8fd9\u662f subprocess \u6a21\u5757\u4e2d\u6700\u7b80\u5355\u7684\u51fd\u6570\uff0c\u7528\u6765\u6267\u884c\u547d\u4ee4\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>result = subprocess.run([&#39;path_to_your_batch_file.bat&#39;], capture_output=True, text=True)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(result.stdout)<\/p>\n<p>print(result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ccapture_output \u53c2\u6570\u7528\u4e8e\u6355\u83b7\u6279\u5904\u7406\u6587\u4ef6\u7684\u8f93\u51fa\uff0ctext \u53c2\u6570\u5c06\u8f93\u51fa\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>2. subprocess.Popen()<\/h4>\n<\/p>\n<p><p>subprocess.Popen() \u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u5728\u6279\u5904\u7406\u6587\u4ef6\u8fd0\u884c\u65f6\u4e0e\u5176\u8fdb\u884c\u4ea4\u4e92\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;path_to_your_batch_file.bat&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>stdout, stderr = process.communicate()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(stdout.decode())<\/p>\n<p>print(stderr.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Popen \u5bf9\u8c61\u7684 stdout \u548c stderr \u53c2\u6570\u7528\u4e8e\u6355\u83b7\u8f93\u51fa\uff0ccommunicate() \u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u6765\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\uff0c\u4f8b\u5982 sh\u3001pexpect \u7b49\u3002\u4e0b\u9762\u662f\u4f7f\u7528 sh \u5e93\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sh<\/p>\n<h2><strong>\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>output = sh.path_to_your_batch_file()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>sh \u5e93\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e Shell \u811a\u672c\u7684\u8bed\u6cd5\uff0c\u4f7f\u5f97\u7f16\u5199\u548c\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002\u5982\u679c\u53ea\u9700\u8981\u7b80\u5355\u5730\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\uff0cos.system() \u6216 subprocess.run() \u53ef\u80fd\u5df2\u7ecf\u8db3\u591f\u3002\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u63a7\u5236\u548c\u4ea4\u4e92\u529f\u80fd\uff0csubprocess.Popen() \u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u66f4\u9ad8\u7ea7\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5904\u7406\u597d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u6279\u5904\u7406\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u3002\u901a\u8fc7<code>subprocess.call()<\/code>\u6216<code>subprocess.run()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8c03\u7528<code>.bat<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u8fd0\u884c\u540d\u4e3a<code>script.bat<\/code>\u7684\u6279\u5904\u7406\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsubprocess.run([&#39;script.bat&#39;], shell=True)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u6267\u884c\u524d\uff0c\u6279\u5904\u7406\u6587\u4ef6\u7684\u8def\u5f84\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u6279\u5904\u7406\u6587\u4ef6\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u82e5\u60f3\u5728Python\u4e2d\u6355\u83b7\u6279\u5904\u7406\u6587\u4ef6\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.Popen()<\/code>\u65b9\u6cd5\u5e76\u8bbe\u7f6e<code>stdout<\/code>\u53c2\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nprocess = subprocess.Popen([&#39;script.bat&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\nstdout, stderr = process.communicate()\n\nprint(&quot;\u8f93\u51fa:&quot;, stdout.decode())\nprint(&quot;\u9519\u8bef:&quot;, stderr.decode())\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8c03\u8bd5\u6279\u5904\u7406\u6587\u4ef6\u7684\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6279\u5904\u7406\u6587\u4ef6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u6279\u5904\u7406\u6587\u4ef6\u7684\u9519\u8bef\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u7801\u6765\u5b9e\u73b0\u3002\u82e5\u8fd4\u56de\u7801\u4e0d\u4e3a\u96f6\uff0c\u8868\u793a\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7<code>process.returncode<\/code>\u6765\u83b7\u53d6\u8fd4\u56de\u7801\uff0c\u5e76\u57fa\u4e8e\u6b64\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nprocess = subprocess.run([&#39;script.bat&#39;], shell=True)\n\nif process.returncode != 0:\n    print(&quot;\u6279\u5904\u7406\u6587\u4ef6\u6267\u884c\u5931\u8d25\uff0c\u8fd4\u56de\u7801:&quot;, process.returncode)\nelse:\n    print(&quot;\u6279\u5904\u7406\u6587\u4ef6\u6267\u884c\u6210\u529f&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5e2e\u52a9\u60a8\u5728\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u65f6\u8fdb\u884c\u6709\u6548\u7684\u9519\u8bef\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\uff0c\u4f8b\u5982\u4f7f\u7528 os \u6a21\u5757\u3001subprocess \u6a21\u5757\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1155594,"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\/1155593"}],"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=1155593"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155593\/revisions"}],"predecessor-version":[{"id":1155595,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155593\/revisions\/1155595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155594"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}