{"id":992022,"date":"2024-12-27T08:33:53","date_gmt":"2024-12-27T00:33:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/992022.html"},"modified":"2024-12-27T08:33:55","modified_gmt":"2024-12-27T00:33:55","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%ad%90%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/992022.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u5b50\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070043\/c230990a-20c1-4566-a7e3-d6ccbba2c14f.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u5b50\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528\u5b50\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>os.system<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982<code>fabric<\/code>\u7b49\u5b9e\u73b0\u3002\u5728\u6548\u7387\u3001\u5b89\u5168\u6027\u548c\u529f\u80fd\u6027\u65b9\u9762\uff0c<code>subprocess<\/code>\u6a21\u5757\u662f\u8c03\u7528\u5b50\u7a0b\u5e8f\u7684\u63a8\u8350\u65b9\u5f0f\u3002<\/strong> <code>subprocess<\/code>\u6a21\u5757\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u8f93\u51fa\u6d41\u3001\u6355\u83b7\u8f93\u51fa\u3001\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528\u5b50\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>SUBPROCESS<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u6a21\u5757\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u8fdb\u7a0b\u3001\u8fde\u63a5\u5176\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\u4ee5\u53ca\u83b7\u53d6\u8fd4\u56de\u503c\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u80fd\u529b\u3002<\/p>\n<\/p>\n<ol>\n<li><code>subprocess.run()<\/code><\/li>\n<\/ol>\n<p><p><code>subprocess.run()<\/code>\u662f<code>subprocess<\/code>\u6a21\u5757\u7684\u4e3b\u8981\u63a5\u53e3\u4e4b\u4e00\uff0c\u7528\u4e8e\u6267\u884c\u5b50\u7a0b\u5e8f\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5b9e\u4f8b\uff0c\u5305\u542b\u5b50\u7a0b\u5e8f\u7684\u8fd4\u56de\u7801\u3001\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/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(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>subprocess.run()<\/code>\u6267\u884c\u4e86<code>ls -l<\/code>\u547d\u4ee4\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002\u901a\u8fc7<code>capture_output=True<\/code>\u6355\u83b7\u8f93\u51fa\uff0c\u901a\u8fc7<code>text=True<\/code>\u5c06\u8f93\u51fa\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><code>subprocess.Popen()<\/code><\/li>\n<\/ol>\n<p><p><code>subprocess.Popen()<\/code>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u5b50\u7a0b\u5e8f\u4ea4\u4e92\u7684\u573a\u666f\u3002\u5b83\u4e0d\u4f1a\u7b49\u5f85\u5b50\u7a0b\u5e8f\u5b8c\u6210\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de\u4e00\u4e2a<code>Popen<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;ls&#39;, &#39;-l&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Popen<\/code>\u5bf9\u8c61\u7684<code>communicate()<\/code>\u65b9\u6cd5\u7528\u4e8e\u4e0e\u5b50\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<code>OS.SYSTEM<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>os.system()<\/code>\u7528\u4e8e\u5728\u5b50shell\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u7b80\u5355\u6613\u7528\uff0c\u4f46\u529f\u80fd\u6709\u9650\uff0c\u65e0\u6cd5\u6355\u83b7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&#39;ls -l&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>os.system()<\/code>\u65b9\u6cd5\u7b80\u5355\uff0c\u4f46\u4e0d\u63a8\u8350\u5728\u9700\u8981\u5904\u7406\u8f93\u51fa\u6216\u8fdb\u884c\u590d\u6742\u4ea4\u4e92\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001<code>FABRIC<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>fabric<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u5728\u591a\u4e2a\u670d\u52a1\u5668\u4e0a\u6267\u884c\u547d\u4ee4\u3002\u5b83\u63d0\u4f9b\u4e86\u7c7b\u4f3cSSH\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>fabric<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install fabric<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>fabric<\/code>\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fabric import Connection<\/p>\n<p>c = Connection(&#39;hostname&#39;)<\/p>\n<p>result = c.run(&#39;ls -l&#39;, hide=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>fabric.Connection<\/code>\u5bf9\u8c61\u7528\u4e8e\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u6267\u884c\u547d\u4ee4\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u5b50\u7a0b\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002<strong>\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>subprocess.run()<\/code><\/strong>\uff1a\u5f53\u9700\u8981\u7b80\u5355\u6267\u884c\u547d\u4ee4\u5e76\u6355\u83b7\u8f93\u51fa\u65f6\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>subprocess.Popen()<\/code><\/strong>\uff1a\u5f53\u9700\u8981\u590d\u6742\u7684\u5b50\u7a0b\u5e8f\u4ea4\u4e92\u65f6\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>os.system()<\/code><\/strong>\uff1a\u4ec5\u5728\u975e\u5e38\u7b80\u5355\u7684\u573a\u5408\u4f7f\u7528\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>fabric<\/code><\/strong>\uff1a\u5f53\u9700\u8981\u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\u65f6\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u5730\u5728Python\u7a0b\u5e8f\u4e2d\u8c03\u7528\u5b50\u7a0b\u5e8f\uff0c\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u8c03\u7528\u5b50\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b50\u7a0b\u5e8f\u901a\u5e38\u662f\u6307\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u968f\u540e\u901a\u8fc7\u5176\u540d\u79f0\u6765\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u6cd5\u51fd\u6570\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n<\/code><\/pre>\n<p>\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u53c2\u6570\u6765\u5b9e\u73b0\uff1a<code>result = add(5, 3)<\/code>\u3002\u8fd9\u6837\uff0c<code>result<\/code>\u5c06\u4f1a\u662f8\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f20\u9012\u591a\u5c11\u4e2a\u53c2\u6570\u7ed9\u5b50\u7a0b\u5e8f\uff1f<\/strong><br \/>Python\u7684\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u661f\u53f7\uff08<code>*<\/code>\uff09\u548c\u53cc\u661f\u53f7\uff08<code>**<\/code>\uff09\u6765\u5904\u7406\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u3002\u4f7f\u7528\u5355\u4e2a\u661f\u53f7\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u800c\u53cc\u661f\u53f7\u5219\u5141\u8bb8\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def variable_args(*args, **kwargs):\n    print(args)\n    print(kwargs)\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a<code>variable_args(1, 2, 3, name=&quot;Alice&quot;, age=30)<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u5b50\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u8c03\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u4f60\u9700\u8981\u5148\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u6216\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u540d\u4e3a<code>math_functions.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>multiply<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a  <\/p>\n<pre><code class=\"language-python\">import math_functions\nresult = math_functions.multiply(4, 5)\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">from math_functions import multiply\nresult = multiply(4, 5)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u5730\u8c03\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u5b50\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528\u5b50\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528subprocess\u6a21\u5757\u3001os.system\u65b9\u6cd5\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982fabric [&hellip;]","protected":false},"author":3,"featured_media":992028,"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\/992022"}],"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=992022"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992022\/revisions"}],"predecessor-version":[{"id":992030,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992022\/revisions\/992030"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/992028"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=992022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=992022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=992022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}