{"id":960083,"date":"2024-12-27T03:46:33","date_gmt":"2024-12-26T19:46:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960083.html"},"modified":"2024-12-27T03:46:34","modified_gmt":"2024-12-26T19:46:34","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0python%e7%82%b9%e5%87%bbbat","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960083.html","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0python\u70b9\u51fbbat"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102722\/1abd7128-fa82-45a4-94b0-ba96309d0c81.webp\" alt=\"\u5982\u4f55\u5b9e\u73b0python\u70b9\u51fbbat\" \/><\/p>\n<p><p> <strong>\u8981\u5b9e\u73b0Python\u70b9\u51fbbat\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528subprocess\u6a21\u5757\u662f\u6bd4\u8f83\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u529f\u80fd\u6765\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u6267\u884c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u4fbf\u6377\u63a5\u53e3\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u5b9e\u73b0\u5bf9bat\u6587\u4ef6\u7684\u70b9\u51fb\u6267\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528os.system()<\/li>\n<\/ol>\n<p><p>os.system()\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u6267\u884cbat\u6587\u4ef6\u3002\u5176\u8bed\u6cd5\u4e3aos.system(command)\uff0c\u5176\u4e2dcommand\u662f\u4f60\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528os.system()\u6267\u884cbat\u6587\u4ef6<\/strong><\/h2>\n<p>os.system(&#39;path_to_your_bat_file.bat&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u7b80\u5355\u7684\u811a\u672c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u65e0\u6cd5\u83b7\u53d6bat\u6587\u4ef6\u6267\u884c\u540e\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u4e14\u65e0\u6cd5\u63a7\u5236\u5b50\u8fdb\u7a0b\u6267\u884c\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528os.startfile()<\/li>\n<\/ol>\n<p><p>os.startfile()\u51fd\u6570\u9002\u7528\u4e8eWindows\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u6216\u6267\u884c\u4e00\u4e2a\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528os.startfile()\u6253\u5f00bat\u6587\u4ef6<\/strong><\/h2>\n<p>os.startfile(&#39;path_to_your_bat_file.bat&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u5408Windows\u7cfb\u7edf\uff0c\u4f7f\u7528\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u53ea\u9002\u7528\u4e8eWindows\uff0c\u4e14\u65e0\u6cd5\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001Subprocess\u6a21\u5757<\/p>\n<\/p>\n<p><p>subprocess\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002\u5b83\u53ef\u4ee5\u66ff\u4ee3os.system()\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u63a7\u5236\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528subprocess.run()<\/li>\n<\/ol>\n<p><p>subprocess.run()\u662fPython 3.5\u5f15\u5165\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8fd0\u884c\u6307\u5b9a\u7684\u547d\u4ee4\u5e76\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528subprocess.run()\u6267\u884cbat\u6587\u4ef6<\/strong><\/h2>\n<p>result = subprocess.run([&#39;path_to_your_bat_file.bat&#39;], capture_output=True, text=True)<\/p>\n<h2><strong>\u6253\u5370bat\u6587\u4ef6\u6267\u884c\u7684\u8f93\u51fa<\/strong><\/h2>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ef\u4ee5\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981Python 3.5\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528subprocess.Popen()<\/li>\n<\/ol>\n<p><p>subprocess.Popen()\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u5bf9\u5b50\u8fdb\u7a0b\u8fdb\u884c\u66f4\u4e3a\u7cbe\u7ec6\u7684\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528subprocess.Popen()\u6267\u884cbat\u6587\u4ef6<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;path_to_your_bat_file.bat&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<h2><strong>\u7b49\u5f85bat\u6587\u4ef6\u6267\u884c\u5b8c\u6210\u5e76\u83b7\u53d6\u8f93\u51fa<\/strong><\/h2>\n<p>stdout, stderr = process.communicate()<\/p>\n<h2><strong>\u6253\u5370\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa<\/strong><\/h2>\n<p>print(stdout.decode())<\/p>\n<p>print(stderr.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u4e3b\u8fdb\u7a0b\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u5b50\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4f7f\u7528\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\u8f93\u51fa\u6d41\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5f71\u54cdbat\u6587\u4ef6\u7684\u6267\u884c\u3002\u53ef\u4ee5\u901a\u8fc7os.environ\u6765\u8bbe\u7f6e\u6216\u4fee\u6539\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import subprocess<\/p>\n<h2><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>os.environ[&#39;MY_VARIABLE&#39;] = &#39;my_value&#39;<\/p>\n<h2><strong>\u6267\u884cbat\u6587\u4ef6<\/strong><\/h2>\n<p>subprocess.run([&#39;path_to_your_bat_file.bat&#39;], capture_output=True, text=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ef\u4ee5\u5728\u6267\u884cbat\u6587\u4ef6\u65f6\u4f20\u9012\u6240\u9700\u7684\u73af\u5883\u53d8\u91cf\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8c03\u6574\u73af\u5883\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u73af\u5883\u53d8\u91cf\u7684\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u7a0b\u5e8f\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5bf9bat\u6587\u4ef6\u7684\u70b9\u51fb\u6267\u884c\u3002\u5728\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u573a\u666f\u6765\u51b3\u5b9a\uff0c\u901a\u5e38\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757\u6765\u83b7\u5f97\u66f4\u597d\u7684\u63a7\u5236\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cBAT\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u6267\u884cBAT\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u8c03\u7528<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9aBAT\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u4ece\u800c\u5728Python\u7a0b\u5e8f\u4e2d\u8fd0\u884c\u5b83\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsubprocess.run([&#39;path_to_your_bat_file.bat&#39;])\n<\/code><\/pre>\n<p>\u786e\u4fdd\u63d0\u4f9b\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u6ce8\u610f\u5982\u679cBAT\u6587\u4ef6\u9700\u8981\u5728\u7279\u5b9a\u7684\u5de5\u4f5c\u76ee\u5f55\u4e0b\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cwd<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fd0\u884cBAT\u6587\u4ef6\u65f6\u4f1a\u9047\u5230\u4ec0\u4e48\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5e38\u89c1\u95ee\u9898\u5305\u62ec\u8def\u5f84\u9519\u8bef\u3001\u6743\u9650\u4e0d\u8db3\u548c\u73af\u5883\u53d8\u91cf\u672a\u8bbe\u7f6e\u7b49\u3002\u786e\u4fddBAT\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u4e14\u6709\u8db3\u591f\u7684\u6743\u9650\u6267\u884c\u8be5\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u662f\u5426\u9700\u8981\u8bbe\u7f6e\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbfBAT\u6587\u4ef6\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6BAT\u6587\u4ef6\u6267\u884c\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6267\u884cBAT\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u7684<code>stdout<\/code>\u53c2\u6570\u6765\u6355\u83b7\u8f93\u51fa\u7ed3\u679c\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u65f6\uff0c\u53ef\u4ee5\u5c06<code>stdout=subprocess.PIPE<\/code>\u4f20\u5165\uff0c\u4ece\u800c\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = subprocess.run([&#39;path_to_your_bat_file.bat&#39;], stdout=subprocess.PIPE, text=True)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u5728Python\u4e2d\u67e5\u770bBAT\u6587\u4ef6\u7684\u6267\u884c\u7ed3\u679c\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b9e\u73b0Python\u70b9\u51fbbat\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":960091,"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\/960083"}],"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=960083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960083\/revisions"}],"predecessor-version":[{"id":960093,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960083\/revisions\/960093"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960091"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}