{"id":963314,"date":"2024-12-27T04:16:43","date_gmt":"2024-12-26T20:16:43","guid":{"rendered":""},"modified":"2024-12-27T04:16:45","modified_gmt":"2024-12-26T20:16:45","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87python%e4%bd%bf%e7%94%a8cmd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963314.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7python\u4f7f\u7528cmd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180651\/db1132a1-c316-487a-a0c1-da2929b321af.webp\" alt=\"\u5982\u4f55\u901a\u8fc7python\u4f7f\u7528cmd\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7Python\u4f7f\u7528CMD\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u8f93\u51fa\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u6267\u884c\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u6267\u884cCMD\u547d\u4ee4\uff0c\u5e76\u6355\u83b7\u8f93\u51fa\u7ed3\u679c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u4e0eCMD\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SUBPROCESS\u6a21\u5757\u7b80\u4ecb<\/p>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e8\u5728\u66ff\u4ee3\u65e7\u7684\u6a21\u5757\u5982<code>os.system<\/code>\u3001<code>os.spawn*<\/code>\u7b49\u3002\u5b83\u80fd\u591f\u542f\u52a8\u5b50\u8fdb\u7a0b\u5e76\u8fde\u63a5\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u751a\u81f3\u53ef\u4ee5\u83b7\u5f97\u5b50\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>subprocess.run()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u662f\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u4e3b\u8981\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u540c\u6b65\u6267\u884c\u547d\u4ee4\u7684\u60c5\u51b5\uff0c\u5373\u5728\u547d\u4ee4\u5b8c\u6210\u4e4b\u524d\uff0cPython\u811a\u672c\u4f1a\u7b49\u5f85\u5176\u7ed3\u675f\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\u3001\u9000\u51fa\u72b6\u6001\u7b49\u4fe1\u606f\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&#39;, &#39;Hello, World!&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)  # \u8f93\u51fa\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>capture_output=True<\/code>\u8868\u793a\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\uff0c<code>text=True<\/code>\u8868\u793a\u5c06\u8f93\u51fa\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>subprocess.Popen()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u66f4\u590d\u6742\u7684\u4ea4\u4e92\uff08\u4f8b\u5982\uff0c\u5f02\u6b65\u6267\u884c\u547d\u4ee4\u3001\u4e0e\u547d\u4ee4\u8fdb\u884c\u4ea4\u4e92\uff09\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.Popen()<\/code>\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\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;ping&#39;, &#39;www.google.com&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()  # \u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1<\/p>\n<p>print(stdout.decode())  # \u6253\u5370\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u51fa<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Popen()<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u547d\u4ee4\u6267\u884c\uff0c\u5e76\u901a\u8fc7<code>communicate()<\/code>\u65b9\u6cd5\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u8f93\u5165\u8f93\u51fa\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u6355\u83b7\u547d\u4ee4\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\u662f\u8bb8\u591a\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u6838\u5fc3\u9700\u6c42\u3002\u901a\u8fc7Python\u7684<code>subprocess<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528stdout\u548cstderr<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u548c<code>subprocess.Popen()<\/code>\u90fd\u63d0\u4f9b\u4e86\u6355\u83b7\u6807\u51c6\u8f93\u51fa\uff08stdout\uff09\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa\uff08stderr\uff09\u7684\u9009\u9879\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;ls&#39;, &#39;-l&#39;], capture_output=True, text=True)<\/p>\n<p>print(&#39;Standard Output:&#39;, result.stdout)<\/p>\n<p>print(&#39;Standard Error:&#39;, result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>stdout<\/code>\u548c<code>stderr<\/code>\u5206\u522b\u6355\u83b7\u4e86\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8f93\u51fa\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u6355\u83b7\u7684\u8f93\u51fa\u901a\u5e38\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u89e3\u6790\u3001\u5b58\u50a8\u6216\u7528\u4e8e\u5176\u4ed6\u8ba1\u7b97\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u8fd9\u4e9b\u8f93\u51fa\u6570\u636e\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 re<\/p>\n<p>result = subprocess.run([&#39;ls&#39;, &#39;-l&#39;], capture_output=True, text=True)<\/p>\n<p>files = re.findall(r&#39;\\S+&#39;, result.stdout)<\/p>\n<p>print(&#39;Files:&#39;, files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u547d\u4ee4\u8f93\u51fa\u4e2d\u63d0\u53d6\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u6d41<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u547d\u4ee4\uff0c\u53ef\u80fd\u9700\u8981\u5411\u5176\u8f93\u5165\u6570\u636e\uff0c\u6216\u8005\u9700\u8981\u540c\u65f6\u5904\u7406\u5176\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<code>subprocess<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u9009\u9879\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5411\u5b50\u8fdb\u7a0b\u8f93\u5165\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Popen.communicate()<\/code>\u65b9\u6cd5\u5411\u5b50\u8fdb\u7a0b\u4f20\u9012\u8f93\u5165\u6570\u636e\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;python&#39;, &#39;script.py&#39;], stdin=subprocess.PIPE, stdout=subprocess.PIPE)<\/p>\n<p>stdout, _ = process.communicate(input=&#39;input data&#39;.encode())<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>stdin=subprocess.PIPE<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5411\u5b50\u8fdb\u7a0b\u4f20\u9012\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u540c\u65f6\u5904\u7406\u8f93\u51fa\u548c\u9519\u8bef<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Popen<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u6355\u83b7\u548c\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\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;python&#39;, &#39;script_with_errors.py&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(&#39;Output:&#39;, stdout.decode())<\/p>\n<p>print(&#39;Error:&#39;, stderr.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u5b50\u8fdb\u7a0b\u8f93\u51fa\u65f6\u4e0d\u4f1a\u9057\u6f0f\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u793a\u4f8b\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u901a\u8fc7Python\u4e0eCMD\u4ea4\u4e92\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bb8\u591a\u5b9e\u7528\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u52a8\u5316\u5907\u4efd<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u52a8\u5316\u5907\u4efd\u811a\u672c\uff0c\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u8fdb\u884c\u6587\u4ef6\u538b\u7f29\u548c\u5b58\u50a8\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<h2><strong>\u538b\u7f29\u6587\u4ef6<\/strong><\/h2>\n<p>subprocess.run([&#39;tar&#39;, &#39;-czf&#39;, &#39;backup.tar.gz&#39;, &#39;\/path\/to\/directory&#39;])<\/p>\n<h2><strong>\u79fb\u52a8\u5230\u5907\u4efd\u4f4d\u7f6e<\/strong><\/h2>\n<p>subprocess.run([&#39;mv&#39;, &#39;backup.tar.gz&#39;, &#39;\/path\/to\/backup\/location&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff0c\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u4f8b\u5982CPU\u3001\u5185\u5b58\u7b49\uff0c\u5e76\u5728Python\u4e2d\u8fdb\u884c\u5904\u7406\u548c\u76d1\u63a7\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;top&#39;, &#39;-b&#39;, &#39;-n&#39;, &#39;1&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u89e3\u6790<code>top<\/code>\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u7ba1\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u501f\u52a9Python\u548cCMD\u7684\u7ed3\u5408\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7f51\u7edc\u7ba1\u7406\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8fde\u63a5\u6d4b\u8bd5\u3001IP\u914d\u7f6e\u7b49\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<h2><strong>\u6d4b\u8bd5\u7f51\u7edc\u8fde\u63a5<\/strong><\/h2>\n<p>result = subprocess.run([&#39;ping&#39;, &#39;-c&#39;, &#39;4&#39;, &#39;www.google.com&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u89e3\u6790<code>ping<\/code>\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u5224\u65ad\u7f51\u7edc\u8fde\u63a5\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u65f6\uff0c\u907f\u514d\u76f4\u63a5\u4f20\u9012\u7528\u6237\u8f93\u5165\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u653b\u51fb\u3002\u53ef\u4ee5\u901a\u8fc7\u9a8c\u8bc1\u548c\u6e05\u7406\u7528\u6237\u8f93\u5165\u6765\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u548c\u53c2\u6570\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u811a\u672c\u65f6\uff0c\u5e94\u6ce8\u610f\u547d\u4ee4\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u5e94\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u548c\u68c0\u67e5\u8fd4\u56de\u7801\u6765\u5904\u7406\u6f5c\u5728\u9519\u8bef\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;wrong_command&#39;], check=True)<\/p>\n<p>except subprocess.CalledProcessError as e:<\/p>\n<p>    print(&#39;Command f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led with error:&#39;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u4f60\u53ef\u4ee5\u5229\u7528Python\u5f3a\u5927\u7684<code>subprocess<\/code>\u6a21\u5757\uff0c\u5b9e\u73b0\u4e0eCMD\u7684\u9ad8\u6548\u4ea4\u4e92\uff0c\u4ece\u800c\u5b8c\u6210\u5404\u79cd\u81ea\u52a8\u5316\u4efb\u52a1\u548c\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u8c03\u7528CMD\u547d\u4ee4\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528CMD\u547d\u4ee4\u3002\u901a\u8fc7<code>subprocess.run()<\/code>\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c<code>dir<\/code>\u547d\u4ee4\u5e76\u6253\u5370\u7ed3\u679c\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nresult = subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;dir&#39;], capture_output=True, text=True)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u5728Python\u811a\u672c\u4e2d\u6267\u884c\u4efb\u610fCMD\u547d\u4ee4\u5e76\u5904\u7406\u5176\u8f93\u51fa\u3002<\/p>\n<p><strong>Python\u8c03\u7528CMD\u65f6\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u7684<code>returncode<\/code>\u5c5e\u6027\u6765\u8bc6\u522b\u547d\u4ee4\u6267\u884c\u662f\u5426\u6210\u529f\u3002\u5982\u679c\u8fd4\u56de\u503c\u4e3a0\uff0c\u8868\u793a\u547d\u4ee4\u6210\u529f\u6267\u884c\uff1b\u5982\u679c\u4e3a\u975e\u96f6\u503c\uff0c\u5219\u8868\u793a\u53d1\u751f\u4e86\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u6349\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    result = subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;some_invalid_command&#39;], check=True, capture_output=True, text=True)\nexcept subprocess.CalledProcessError as e:\n    print(f&quot;Error occurred: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u6267\u884c\u547d\u4ee4\u65f6\u80fd\u591f\u5b89\u5168\u5904\u7406\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9CMD\u547d\u4ee4\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u7684\u65b9\u5f0f\u4f20\u9012\u53c2\u6570\u7ed9CMD\u547d\u4ee4\u3002\u5728<code>subprocess.run()<\/code>\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5c06\u547d\u4ee4\u53ca\u5176\u53c2\u6570\u4f5c\u4e3a\u5217\u8868\u4f20\u5165\u3002\u4f8b\u5982\uff0c\u6267\u884c<code>ping<\/code>\u547d\u4ee4\u5e76\u6307\u5b9a\u4e00\u4e2aIP\u5730\u5740\uff1a  <\/p>\n<pre><code class=\"language-python\">subprocess.run([&#39;cmd&#39;, &#39;\/c&#39;, &#39;ping&#39;, &#39;8.8.8.8&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u53c2\u6570\u7684\u6b63\u786e\u4f20\u9012\uff0c\u5e76\u907f\u514d\u4e86\u547d\u4ee4\u884c\u6ce8\u5165\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python\u4f7f\u7528CMD\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u8f93\u51fa\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528Pytho [&hellip;]","protected":false},"author":3,"featured_media":963320,"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\/963314"}],"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=963314"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963314\/revisions"}],"predecessor-version":[{"id":963322,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963314\/revisions\/963322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963320"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}