{"id":1015070,"date":"2024-12-27T12:01:59","date_gmt":"2024-12-27T04:01:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1015070.html"},"modified":"2024-12-27T12:02:01","modified_gmt":"2024-12-27T04:02:01","slug":"python-%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8windows%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1015070.html","title":{"rendered":"python \u5982\u4f55\u8c03\u7528windows\u547d\u4ee4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095019\/33e4995c-451d-4ae5-8b7c-62e0ad6c1229.webp\" alt=\"python \u5982\u4f55\u8c03\u7528windows\u547d\u4ee4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528Windows\u547d\u4ee4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os.system()<\/code>\u3001<code>subprocess.run()<\/code>\u548c<code>subprocess.Popen()<\/code>\uff0c\u5176\u4e2d<code>subprocess.run()<\/code>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u5b89\u5168\u6027<\/strong>\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u53ef\u4ee5\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u3001\u68c0\u67e5\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8bbe\u7f6e\u8d85\u65f6\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS.SYSTEM()<\/p>\n<\/p>\n<p><p><code>os.system()<\/code>\u662f\u6700\u7b80\u5355\u7684\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u7684\u65b9\u6cd5\u3002\u5b83\u9002\u5408\u4e8e\u6267\u884c\u7b80\u5355\u7684\u547d\u4ee4\u5e76\u4e14\u4e0d\u9700\u8981\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.system()<\/code>\u53ef\u4ee5\u76f4\u63a5\u5728Python\u811a\u672c\u4e2d\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&#39;dir&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6267\u884cWindows\u7684<code>dir<\/code>\u547d\u4ee4\uff0c\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<code>os.system()<\/code>\u4f7f\u7528\u7b80\u5355\uff0c\u9002\u5408\u5feb\u901f\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u65e0\u6cd5\u6355\u83b7\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\uff0c\u65e0\u6cd5\u83b7\u53d6\u547d\u4ee4\u7684\u8fd4\u56de\u503c\uff08\u9664\u4e86\u9000\u51fa\u72b6\u6001\uff09\uff0c\u4e0d\u9002\u5408\u9700\u8981\u5904\u7406\u547d\u4ee4\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001SUBPROCESS.RUN()<\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u662f\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u63a7\u5236\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u547d\u4ee4\u7684\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;dir&#39;], shell=True, capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u6267\u884c<code>dir<\/code>\u547d\u4ee4\uff0c\u5e76\u5c06\u8f93\u51fa\u6355\u83b7\u5230<code>result.stdout<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u529f\u80fd\u8be6\u89e3<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>shell=True<\/strong>\uff1a\u5141\u8bb8\u5728shell\u4e2d\u6267\u884c\u547d\u4ee4\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u662f\u5fc5\u8981\u7684\uff0c\u4f8b\u5982\u4f7f\u7528<code>dir<\/code>\u8fd9\u6837\u7684\u5185\u90e8\u547d\u4ee4\u3002<\/li>\n<li><strong>capture_output=True<\/strong>\uff1a\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<\/li>\n<li><strong>text=True<\/strong>\uff1a\u5c06\u8f93\u51fa\u4f5c\u4e3a\u6587\u672c\u800c\u4e0d\u662f\u5b57\u8282\u6d41\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\u3001\u68c0\u67e5\u9000\u51fa\u72b6\u6001\u3001\u8bbe\u7f6e\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u8f83<code>os.system()<\/code>\u7565\u5fae\u590d\u6742\uff0c\u9700\u8981\u4e86\u89e3\u66f4\u591a\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001SUBPROCESS.POPEN()<\/p>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u63d0\u4f9b\u66f4\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u9002\u5408\u9700\u8981\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u7684\u590d\u6742\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u53ef\u4ee5\u7528\u4e8e\u9700\u8981\u4e0e\u547d\u4ee4\u8fdb\u884c\u4ea4\u4e92\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;ping&#39;, &#39;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>\u4e0a\u8ff0\u4ee3\u7801\u6267\u884c<code>ping<\/code>\u547d\u4ee4\uff0c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u529f\u80fd\u8be6\u89e3<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>stdout=subprocess.PIPE<\/strong>\uff1a\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u7ba1\u9053\u3002<\/li>\n<li><strong>stderr=subprocess.PIPE<\/strong>\uff1a\u5c06\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u7ba1\u9053\u3002<\/li>\n<li><strong>communicate()<\/strong>\uff1a\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\uff0c\u83b7\u53d6\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u9002\u5408\u590d\u6742\u7684\u4ea4\u4e92\u573a\u666f\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u4f7f\u7528\u590d\u6742\uff0c\u9700\u8981\u66f4\u591a\u4ee3\u7801\u6765\u5904\u7406\u8fdb\u7a0b\u95f4\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5176\u4ed6\u76f8\u5173\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5\u9000\u51fa\u72b6\u6001<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u8fd4\u56de\u7684<code>CompletedProcess<\/code>\u5bf9\u8c61\u5305\u542b\u4e86\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = subprocess.run([&#39;dir&#39;], shell=True)<\/p>\n<p>print(&#39;Exit status:&#39;, result.returncode)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8d85\u65f6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a<code>subprocess.run()<\/code>\u8bbe\u7f6e\u8d85\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    subprocess.run([&#39;sleep&#39;, &#39;10&#39;], timeout=5)<\/p>\n<p>except subprocess.TimeoutExpired:<\/p>\n<p>    print(&#39;Command timed out&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u5b9a\u5411\u8f93\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>subprocess.run()<\/code>\u7684<code>input<\/code>\u53c2\u6570\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = subprocess.run([&#39;sort&#39;], input=&#39;banana\\napple\\ncherry&#39;, capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u7528Windows\u547d\u4ee4\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528<code>subprocess.run()<\/code>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u8f83\u597d\u7684\u529f\u80fd\u6027\u548c\u5b89\u5168\u6027\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u914d\u7f6e\u8fd9\u4e9b\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5728Python\u4e2d\u6267\u884c\u548c\u7ba1\u7406Windows\u547d\u4ee4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cWindows\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884cWindows\u547d\u4ee4\u3002<code>os.system()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u547d\u4ee4\uff0c\u4f46\u63a8\u8350\u4f7f\u7528<code>subprocess.run()<\/code>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u7075\u6d3b\u6027\u548c\u9519\u8bef\u5904\u7406\u80fd\u529b\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# \u4f7f\u7528subprocess\u6267\u884c\u547d\u4ee4\nresult = subprocess.run([&#39;dir&#39;], shell=True)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff0c\u66f4\u52a0\u5b89\u5168\u548c\u6709\u6548\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728Python\u4e2d\u8c03\u7528\u54ea\u4e9b\u7c7b\u578b\u7684Windows\u547d\u4ee4\uff1f<\/strong><br \/>\u51e0\u4e4e\u6240\u6709\u7684Windows\u547d\u4ee4\u90fd\u53ef\u4ee5\u901a\u8fc7Python\u6267\u884c\uff0c\u4f8b\u5982\u6587\u4ef6\u7ba1\u7406\u547d\u4ee4\uff08\u5982<code>copy<\/code>\u3001<code>del<\/code>\uff09\u3001\u7f51\u7edc\u547d\u4ee4\uff08\u5982<code>ping<\/code>\u3001<code>ipconfig<\/code>\uff09\u4ee5\u53ca\u7cfb\u7edf\u7ba1\u7406\u547d\u4ee4\uff08\u5982<code>tasklist<\/code>\u3001<code>shutdown<\/code>\uff09\u3002\u786e\u4fdd\u5728\u8c03\u7528\u547d\u4ee4\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u8bed\u6cd5\u548c\u53c2\u6570\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u53d1\u751f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5728Python\u4e2d\u6267\u884cWindows\u547d\u4ee4\u65f6\u4ea7\u751f\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u5730\u6355\u83b7\u5e76\u5904\u7406\u6267\u884c\u547d\u4ee4\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\ntry:\n    result = subprocess.run([&#39;invalid_command&#39;], shell=True, check=True)\nexcept subprocess.CalledProcessError as e:\n    print(f&quot;\u547d\u4ee4\u6267\u884c\u5931\u8d25\uff0c\u9519\u8bef\u7801\uff1a{e.returncode}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u8bc6\u522b\u5e76\u5904\u7406\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528Windows\u547d\u4ee4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os.system()\u3001subprocess.run()\u548c [&hellip;]","protected":false},"author":3,"featured_media":1015073,"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\/1015070"}],"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=1015070"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015070\/revisions"}],"predecessor-version":[{"id":1015076,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015070\/revisions\/1015076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1015073"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1015070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1015070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1015070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}