{"id":936124,"date":"2024-12-26T19:11:03","date_gmt":"2024-12-26T11:11:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936124.html"},"modified":"2024-12-26T19:11:06","modified_gmt":"2024-12-26T11:11:06","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8shell","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936124.html","title":{"rendered":"python\u5982\u4f55\u7528shell"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072523\/b7d44d6e-f6ac-4e67-8426-758863bc39e2.webp\" alt=\"python\u5982\u4f55\u7528shell\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5b50\u8fdb\u7a0b\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u7ed3\u5408shell\u811a\u672c\u7b49\u65b9\u5f0f\u6765\u6267\u884cshell\u547d\u4ee4\u3002<\/strong> \u901a\u8fc7\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>os.system<\/code>\u51fd\u6570\u3001\u4ee5\u53ca\u7f16\u5199\u548c\u6267\u884cshell\u811a\u672c\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u8fd0\u884cshell\u547d\u4ee4\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757<\/strong>\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8c03\u7528\u5b50\u8fdb\u7a0b<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>subprocess<\/code>\u6a21\u5757\u662f\u6267\u884cshell\u547d\u4ee4\u7684\u9996\u9009\u65b9\u6cd5\u3002\u8fd9\u4e2a\u6a21\u5757\u5141\u8bb8\u4f60\u751f\u6210\u65b0\u7684\u8fdb\u7a0b\u3001\u8fde\u63a5\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884cshell\u547d\u4ee4\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/strong><br \/><code>subprocess.run()<\/code>\u662f\u6700\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728Python\u4e2d\u6267\u884cshell\u547d\u4ee4\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u547d\u4ee4\u7684\u5217\u8868\u6765\u8fd0\u884c\u5b83\u3002\u6bd4\u5982\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(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ls -l<\/code>\u547d\u4ee4\u5728shell\u4e2d\u6267\u884c\uff0c\u5e76\u4e14\u5176\u8f93\u51fa\u88ab\u6355\u83b7\u5e76\u6253\u5370\u51fa\u6765\u3002<code>capture_output=True<\/code>\u53c2\u6570\u7528\u4e8e\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\uff0c<code>text=True<\/code>\u4f7f\u8f93\u51fa\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef<\/strong><br \/>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>stdout<\/code>\u548c<code>stderr<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u547d\u4ee4\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001<\/strong><br \/><code>subprocess.run()<\/code>\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5bf9\u8c61\uff0c\u5176\u4e2d<code>returncode<\/code>\u5c5e\u6027\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if result.returncode != 0:<\/p>\n<p>    print(&#39;An error occurred:&#39;, result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528shell=True<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u8981\u6267\u884c\u4e00\u4e2a\u590d\u6742\u7684shell\u547d\u4ee4\uff08\u6bd4\u5982\u4f7f\u7528\u7ba1\u9053\u6216\u91cd\u5b9a\u5411\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shell=True<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">subprocess.run(&#39;ls -l | grep py&#39;, shell=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a\u4f7f\u7528<code>shell=True<\/code>\u53ef\u80fd\u4f1a\u5f15\u5165\u5b89\u5168\u98ce\u9669\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u5efa\u8bae\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u9664\u4e86<code>subprocess<\/code>\u6a21\u5757\uff0cPython\u4e2d\u7684<code>os<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u7b80\u5355\u7684shell\u547d\u4ee4\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5df2\u7ecf\u4e0d\u88ab\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u7f3a\u4e4f\u7075\u6d3b\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>os.system()<\/strong><br \/><code>os.system()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6267\u884c\u4e00\u4e2ashell\u547d\u4ee4\uff0c\u4f46\u5b83\u65e0\u6cd5\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\uff1a<\/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>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u7531\u4e8e\u65e0\u6cd5\u83b7\u53d6\u547d\u4ee4\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u65e0\u9700\u6355\u83b7\u8f93\u51fa\u7684\u7b80\u5355\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7ed3\u5408shell\u811a\u672c<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6267\u884c\u590d\u6742\u7684shell\u547d\u4ee4\uff0c\u6216\u8005\u9700\u8981\u5728\u591a\u4e2aPython\u811a\u672c\u4e4b\u95f4\u5171\u4eabshell\u903b\u8f91\uff0c\u7f16\u5199\u72ec\u7acb\u7684shell\u811a\u672c\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199shell\u811a\u672c<\/strong><br \/>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u4f60\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4\u7684shell\u811a\u672c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>script.sh<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">#!\/bin\/bash<\/p>\n<p>echo &quot;This is a shell script&quot;<\/p>\n<p>ls -l<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u6267\u884cshell\u811a\u672c<\/strong><br \/>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run()<\/code>\u6216<code>os.system()<\/code>\u5728Python\u4e2d\u6267\u884c\u8be5\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">subprocess.run([&#39;bash&#39;, &#39;script.sh&#39;], capture_output=True, text=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5141\u8bb8\u4f60\u5c06\u590d\u6742\u7684shell\u903b\u8f91\u4e0ePython\u4ee3\u7801\u5206\u5f00\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6267\u884cshell\u547d\u4ee4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<code>subprocess<\/code>\u6a21\u5757\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u547d\u4ee4\uff0c<code>os.system()<\/code>\u53ef\u80fd\u8db3\u591f\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u4efb\u52a1\u6216\u9700\u8981\u6355\u83b7\u8f93\u51fa\uff0c<code>subprocess<\/code>\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u53cd\u590d\u4f7f\u7528\u76f8\u540c\u7684shell\u903b\u8f91\uff0c\u7f16\u5199shell\u811a\u672c\u5e76\u5728Python\u4e2d\u8c03\u7528\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u51cf\u5c11\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u60c5\u51b5\uff0c\u5c24\u5176\u8981\u5c0f\u5fc3<code>shell=True<\/code>\u7684\u4f7f\u7528\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u653b\u51fb\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528Python\u548cshell\u547d\u4ee4\uff0c\u53ef\u4ee5\u6784\u5efa\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5145\u5206\u5229\u7528\u4e24\u8005\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cShell\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884cShell\u547d\u4ee4\u3002\u901a\u8fc7<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fd0\u884c\u4efb\u4f55Shell\u547d\u4ee4\u5e76\u83b7\u53d6\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nresult = subprocess.run([&#39;ls&#39;, &#39;-l&#39;], capture_output=True, text=True)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u5217\u51fa\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fd0\u884cShell\u811a\u672c\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5f53\u9700\u8981\u5728Python\u4e2d\u8fd0\u884cShell\u811a\u672c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u8c03\u7528\u811a\u672c\uff0c\u4ee5\u907f\u514d\u8def\u5f84\u95ee\u9898\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u80fd\u591f\u66f4\u597d\u5730\u6355\u83b7\u9519\u8bef\u4fe1\u606f\u5e76\u5904\u7406\u8f93\u51fa\u3002\u786e\u4fdd\u811a\u672c\u5177\u6709\u6267\u884c\u6743\u9650\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Shell\u547d\u4ee4\u6267\u884c\u4e2d\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u6267\u884cShell\u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u672a\u627e\u5230\u6216\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5<code>returncode<\/code>\u6765\u5224\u65ad\u547d\u4ee4\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u5982\u679c\u8fd4\u56de\u7801\u4e0d\u4e3a0\uff0c\u8868\u793a\u547d\u4ee4\u6267\u884c\u5931\u8d25\uff0c\u53ef\u4ee5\u901a\u8fc7<code>stderr<\/code>\u5c5e\u6027\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = subprocess.run([&#39;some_command&#39;], capture_output=True, text=True)\nif result.returncode != 0:\n    print(f&quot;Error occurred: {result.stderr}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u53ca\u65f6\u53d1\u73b0\u5e76\u5904\u7406\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5b50\u8fdb\u7a0b\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u7ed3\u5408shell\u811a\u672c\u7b49\u65b9\u5f0f\u6765\u6267\u884cshell\u547d\u4ee4\u3002 \u901a\u8fc7\u4f7f\u7528subp [&hellip;]","protected":false},"author":3,"featured_media":936126,"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\/936124"}],"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=936124"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936124\/revisions"}],"predecessor-version":[{"id":936127,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936124\/revisions\/936127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936126"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}