{"id":1180513,"date":"2025-01-15T18:37:38","date_gmt":"2025-01-15T10:37:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180513.html"},"modified":"2025-01-15T18:37:41","modified_gmt":"2025-01-15T10:37:41","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8cmd-exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180513.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528cmd.exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125213\/a4cdcce5-a57f-4301-8e70-73607950233b.webp\" alt=\"python\u5982\u4f55\u8c03\u7528cmd.exe\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528cmd.exe\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7shell\u811a\u672c\u8c03\u7528\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528subprocess\u6a21\u5757<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u548c\u7075\u6d3b\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdsubprocess\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757\u8c03\u7528cmd.exe<\/h3>\n<\/p>\n<p><p><strong>subprocess\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u6267\u884c\u5916\u90e8\u547d\u4ee4\u548c\u7a0b\u5e8f\u7684\u6807\u51c6\u6a21\u5757\u3002<\/strong> \u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u3002\u8fd9\u91cc\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001subprocess.run()<\/h4>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u662fsubprocess\u6a21\u5757\u4e2d\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u51fd\u6570\u4e4b\u4e00\u3002\u5b83\u7528\u4e8e\u6267\u884c\u5916\u90e8\u547d\u4ee4\uff0c\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>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;echo Hello World&#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\u793a\u4f8b\u4e2d\uff0c<code>cmd<\/code>\u548c<code>\/c<\/code>\u662fcmd.exe\u7684\u53c2\u6570\uff0c<code>echo Hello World<\/code>\u662f\u6211\u4eec\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<code>capture_output=True<\/code>\u8868\u793a\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c<code>text=True<\/code>\u8868\u793a\u5c06\u8f93\u51fa\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001subprocess.Popen()<\/h4>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u3002\u5b83\u9002\u7528\u4e8e\u9700\u8981\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u6216\u9700\u8981\u66f4\u590d\u6742\u7684\u7ba1\u9053\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;cmd&#39;, &#39;\/c&#39;, &#39;ping www.google.com&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Popen<\/code>\u65b9\u6cd5\u542f\u52a8\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u6267\u884c\u547d\u4ee4\uff0c<code>stdout=subprocess.PIPE<\/code>\u548c<code>stderr=subprocess.PIPE<\/code>\u5c06\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u7ba1\u9053\u4e2d\uff0c<code>text=True<\/code>\u8868\u793a\u5c06\u8f93\u51fa\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\u3002<code>communicate()<\/code>\u65b9\u6cd5\u7528\u4e8e\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\uff0c\u5e76\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757\u8c03\u7528cmd.exe<\/h3>\n<\/p>\n<p><p><strong>os\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u6807\u51c6\u6a21\u5757\u3002<\/strong> \u5b83\u63d0\u4f9b\u4e86\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u57fa\u672c\u529f\u80fd\uff0c\u4f46\u76f8\u6bd4subprocess\u6a21\u5757\u529f\u80fd\u8f83\u4e3a\u7b80\u5355\u548c\u6709\u9650\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001os.system()<\/h4>\n<\/p>\n<p><p><code>os.system()<\/code>\u7528\u4e8e\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u547d\u4ee4\u3002\u5b83\u7684\u8fd4\u56de\u503c\u662f\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>exit_status = os.system(&#39;cmd \/c &quot;echo Hello World&quot;&#39;)<\/p>\n<p>print(exit_status)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>cmd \/c &quot;echo Hello World&quot;<\/code>\u662f\u6211\u4eec\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002os.system()\u7684\u8fd4\u56de\u503c\u662f\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7shell\u811a\u672c\u8c03\u7528cmd.exe<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7shell\u811a\u672c\u8c03\u7528cmd.exe\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/strong> \u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u7f16\u5199.bat\u6216.cmd\u811a\u672c\uff0c\u5e76\u4f7f\u7528Python\u811a\u672c\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u7f16\u5199shell\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684.bat\u811a\u672c\uff08\u4f8b\u5982hello.bat\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-batch\">@echo off<\/p>\n<p>echo Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u4f7f\u7528subprocess\u6a21\u5757\u8c03\u7528shell\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;hello.bat&#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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>subprocess.run()<\/code>\u6765\u8c03\u7528hello.bat\u811a\u672c\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p><strong>\u5728\u9009\u62e9\u8c03\u7528cmd.exe\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u9700\u6c42<\/strong>\uff1a\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u63a7\u5236\u548c\u4ea4\u4e92\uff0c\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757\u3002\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u6267\u884c\u547d\u4ee4\uff0cos\u6a21\u5757\u4e5f\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1asubprocess\u6a21\u5757\u76f8\u5bf9\u66f4\u5b89\u5168\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u5c06\u547d\u4ee4\u548c\u53c2\u6570\u5206\u5f00\u4f20\u9012\uff0c\u907f\u514d\u547d\u4ee4\u6ce8\u5165\u98ce\u9669\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u786e\u4fdd\u811a\u672c\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u517c\u5bb9\u6027\u3002Windows\u4e0a\u4f7f\u7528cmd.exe\uff0c\u800c\u5728\u7c7bUnix\u7cfb\u7edf\u4e0a\u4f7f\u7528shell\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<p><p><strong>\u603b\u4e4b\uff0csubprocess\u6a21\u5757\u662f\u6700\u63a8\u8350\u7684\u9009\u62e9<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u573a\u666f\u7684\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0ecmd.exe\u7684\u9ad8\u6548\u4ea4\u4e92\uff0c\u63d0\u5347\u811a\u672c\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6df1\u5165\u7406\u89e3subprocess\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3subprocess\u6a21\u5757\uff0c\u6211\u4eec\u9700\u8981\u6df1\u5165\u4e86\u89e3\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u4f7f\u7528shell=True\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728shell\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u4f8b\u5982\u4f7f\u7528\u91cd\u5b9a\u5411\u3001\u7ba1\u9053\u6216\u5176\u4ed6shell\u7279\u6027\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shell=True<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run(&#39;echo Hello World | find &quot;Hello&quot;&#39;, shell=True, capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7ba1\u9053\u64cd\u4f5c\uff08|\uff09\uff0c\u5e76\u5c06<code>shell=True<\/code>\u8bbe\u7f6e\u4e3aTrue\uff0c\u4ee5\u4fbf\u5728shell\u4e2d\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u6267\u884c\u547d\u4ee4\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8bbe\u7f6e\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528<code>env<\/code>\u53c2\u6570\u6765\u4f20\u9012\u4e00\u4e2a\u5305\u542b\u73af\u5883\u53d8\u91cf\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import os<\/p>\n<p>env = os.environ.copy()<\/p>\n<p>env[&#39;MY_VAR&#39;] = &#39;Hello World&#39;<\/p>\n<p>result = subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;echo %MY_VAR%&#39;], env=env, capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u590d\u5236\u4e86\u5f53\u524d\u7684\u73af\u5883\u53d8\u91cf\u5b57\u5178\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MY_VAR<\/code>\u7684\u73af\u5883\u53d8\u91cf\uff0c\u7136\u540e\u5728\u6267\u884c\u547d\u4ee4\u65f6\u4f20\u9012\u8fd9\u4e2a\u73af\u5883\u53d8\u91cf\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>5.3\u3001\u5904\u7406\u8d85\u65f6<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u547d\u4ee4\u53ef\u80fd\u4f1a\u957f\u65f6\u95f4\u8fd0\u884c\uff0c\u751a\u81f3\u9677\u5165\u6b7b\u5faa\u73af\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>try:<\/p>\n<p>    result = subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;ping www.google.com -n 5&#39;], timeout=2, capture_output=True, text=True)<\/p>\n<p>    print(result.stdout)<\/p>\n<p>except subprocess.TimeoutExpired:<\/p>\n<p>    print(&quot;Command timed out&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u4e00\u4e2a2\u79d2\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u547d\u4ee4\u57282\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u5c06\u4f1a\u5f15\u53d1<code>TimeoutExpired<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528subprocess\u6a21\u5757\u8fdb\u884c\u8fdb\u9636\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>6.1\u3001\u4e0e\u5b50\u8fdb\u7a0b\u4ea4\u4e92<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\uff0c\u4f8b\u5982\u5411\u5b50\u8fdb\u7a0b\u53d1\u9001\u8f93\u5165\u6216\u4ece\u5b50\u8fdb\u7a0b\u8bfb\u53d6\u8f93\u51fa\u3002\u53ef\u4ee5\u4f7f\u7528<code>Popen<\/code>\u5bf9\u8c61\u7684<code>stdin<\/code>\u3001<code>stdout<\/code>\u548c<code>stderr<\/code>\u7ba1\u9053\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;cmd&#39;, &#39;\/c&#39;, &#39;more&#39;], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)<\/p>\n<p>stdout, _ = process.communicate(input=&#39;Hello World\\n&#39;)<\/p>\n<p>print(stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u542f\u52a8\u4e86\u4e00\u4e2a<code>more<\/code>\u547d\u4ee4\uff0c\u5e76\u5411\u5176\u6807\u51c6\u8f93\u5165\u53d1\u9001\u4e86<code>Hello World<\/code>\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8bfb\u53d6\u5e76\u6253\u5370\u4e86\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u91cd\u5b9a\u5411\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u6216\u5176\u4ed6\u5730\u65b9\u3002\u53ef\u4ee5\u4f7f\u7528<code>stdout<\/code>\u548c<code>stderr<\/code>\u53c2\u6570\u8fdb\u884c\u91cd\u5b9a\u5411\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;echo Hello World&#39;], stdout=f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u7f16\u5199\u8de8\u5e73\u53f0\u7684Python\u811a\u672c\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e0a\u4f7f\u7528cmd.exe\uff0c\u800c\u5728Linux\u6216macOS\u4e0a\u4f7f\u7528shell\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>7.1\u3001\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.name<\/code>\u6216<code>platform.system()<\/code>\u6765\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u6267\u884c\u4e0d\u540c\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import platform<\/p>\n<p>import subprocess<\/p>\n<p>if os.name == &#39;nt&#39;:  # Windows<\/p>\n<p>    command = [&#39;cmd&#39;, &#39;\/c&#39;, &#39;echo Hello World&#39;]<\/p>\n<p>elif platform.system() == &#39;Linux&#39;:  # Linux<\/p>\n<p>    command = [&#39;echo&#39;, &#39;Hello World&#39;]<\/p>\n<p>else:  # macOS or other<\/p>\n<p>    command = [&#39;echo&#39;, &#39;Hello World&#39;]<\/p>\n<p>subprocess.run(command)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u4e0d\u540c\u7684\u547d\u4ee4\u8fdb\u884c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6df1\u5165\u4e86\u89e3subprocess\u6a21\u5757\u53ca\u5176\u9ad8\u7ea7\u7528\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7075\u6d3b\u548c\u9ad8\u6548\u5730\u8c03\u7528cmd.exe\u6216\u5176\u4ed6\u5916\u90e8\u547d\u4ee4\u3002<strong>subprocess\u6a21\u5757\u662fPython\u4e2d\u6700\u5f3a\u5927\u548c\u63a8\u8350\u7684\u5de5\u5177<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u573a\u666f\u7684\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u53ef\u4ee5\u63d0\u5347\u811a\u672c\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6267\u884c\u547d\u4ee4\uff0c\u5e76\u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528cmd.exe\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u8c03\u7528cmd.exe\u65f6\uff0c\u786e\u4fdd\u547d\u4ee4\u7684\u8def\u5f84\u548c\u53c2\u6570\u6b63\u786e\u65e0\u8bef\u3002\u8fd8\u9700\u6ce8\u610f\u547d\u4ee4\u7684\u683c\u5f0f\uff0c\u67d0\u4e9b\u547d\u4ee4\u53ef\u80fd\u9700\u8981\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u4f20\u5165\uff0c\u5e76\u9700\u8fdb\u884c\u9002\u5f53\u7684\u8f6c\u4e49\u3002\u6b64\u5916\uff0c\u8fd0\u884c\u6743\u9650\u4e5f\u53ef\u80fd\u5f71\u54cd\u547d\u4ee4\u7684\u6267\u884c\uff0c\u786e\u4fddPython\u811a\u672c\u5177\u6709\u6267\u884c\u6240\u9700\u547d\u4ee4\u7684\u6743\u9650\u3002<\/p>\n<p><strong>Python\u811a\u672c\u4e2d\u5982\u4f55\u6355\u83b7cmd.exe\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u901a\u8fc7<code>subprocess.run()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>capture_output=True<\/code>\u53c2\u6570\u6765\u6355\u83b7\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u3002\u6267\u884c\u547d\u4ee4\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u7684<code>CompletedProcess<\/code>\u5bf9\u8c61\u7684<code>stdout<\/code>\u548c<code>stderr<\/code>\u5c5e\u6027\u83b7\u53d6\u76f8\u5e94\u7684\u4fe1\u606f\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528cmd.exe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7shell\u811a\u672c [&hellip;]","protected":false},"author":3,"featured_media":1180520,"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\/1180513"}],"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=1180513"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180513\/revisions"}],"predecessor-version":[{"id":1180523,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180513\/revisions\/1180523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180520"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}