{"id":1186626,"date":"2025-01-15T19:55:00","date_gmt":"2025-01-15T11:55:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186626.html"},"modified":"2025-01-15T19:55:02","modified_gmt":"2025-01-15T11:55:02","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186626.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u5916\u90e8\u547d\u4ee4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135408\/9e65667b-2b85-4421-8a28-3d973e8c1de0.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u5916\u90e8\u547d\u4ee4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001os.system()\u51fd\u6570\u3001os.popen()\u51fd\u6570\u3001\u4ee5\u53cash\u6a21\u5757\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<strong>subprocess\u6a21\u5757<\/strong>\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u5b89\u5168\u6027\u9ad8\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528subprocess\u6a21\u5757\u7684\u65b9\u6cd5\uff0c\u5e76\u7b80\u8981\u63d0\u53ca\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1.1 subprocess.run()<\/h4>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u662fPython 3.5\u5f15\u5165\u7684\u65b0\u51fd\u6570\uff0c\u7528\u4e8e\u8fd0\u884c\u5916\u90e8\u547d\u4ee4\u5e76\u7b49\u5f85\u547d\u4ee4\u5b8c\u6210\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<ul>\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\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u8282\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><p><code>subprocess.run()<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5b9e\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u547d\u4ee4\u7684\u8fd4\u56de\u7801\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>1.2 subprocess.Popen()<\/h4>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u662f\u4e00\u4e2a\u66f4\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u5b83\u4e0d\u4f1a\u7b49\u5f85\u547d\u4ee4\u5b8c\u6210\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u547d\u4ee4\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, text=True)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout)<\/p>\n<p><\/code><\/pre>\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>process.communicate()<\/strong>\uff1a\u7b49\u5f85\u547d\u4ee4\u5b8c\u6210\u5e76\u83b7\u53d6\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001os.system()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os.system()<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u4f46\u4e0d\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7684\u529f\u80fd\u8f83\u4e3a\u6709\u9650\uff0c\u4e14\u4e0d\u5b89\u5168\uff08\u5bb9\u6613\u53d7\u5230shell\u6ce8\u5165\u653b\u51fb\uff09\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><code>os.system()<\/code>\u7684\u8fd4\u56de\u503c\u662f\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u7801\uff0c\u800c\u4e0d\u662f\u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001os.popen()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os.popen()<\/code>\u7528\u4e8e\u6253\u5f00\u4e00\u4e2a\u7ba1\u9053\u8fde\u63a5\uff0c\u53ef\u4ee5\u8bfb\u53d6\u6216\u5199\u5165\u5916\u90e8\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>with os.popen(&#39;ls -l&#39;) as stream:<\/p>\n<p>    output = stream.read()<\/p>\n<p>    print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>os.popen()<\/code>\u6bd4<code>os.system()<\/code>\u7a0d\u5fae\u7075\u6d3b\u4e00\u4e9b\uff0c\u4f46\u4ecd\u4e0d\u5982<code>subprocess<\/code>\u6a21\u5757\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001sh\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>sh<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3aPythonic\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u3002\u9700\u8981\u5148\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import sh<\/p>\n<p>print(sh.ls(&#39;-l&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>sh<\/code>\u6a21\u5757\u7684API\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u662f\u4e0d\u5728\u6807\u51c6\u5e93\u4e2d\uff0c\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6bd4\u8f83\u4e0e\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u573a\u666f\u4e0b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u5173\u952e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>subprocess.run()<\/strong>\uff1a\u63a8\u8350\u7528\u4e8e\u540c\u6b65\u6267\u884c\u547d\u4ee4\uff0c\u7b80\u5355\u4e14\u5b89\u5168\u3002<\/li>\n<li><strong>subprocess.Popen()<\/strong>\uff1a\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u6267\u884c\u6216\u66f4\u590d\u6742\u7684\u547d\u4ee4\u64cd\u4f5c\u3002<\/li>\n<li><strong>os.system()<\/strong>\uff1a\u4e0d\u63a8\u8350\uff0c\u529f\u80fd\u6709\u9650\u4e14\u4e0d\u5b89\u5168\u3002<\/li>\n<li><strong>os.popen()<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8f93\u51fa\u6355\u83b7\uff0c\u4f46\u529f\u80fd\u4e0d\u5982<code>subprocess<\/code>\u3002<\/li>\n<li><strong>sh\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7b80\u6d01\u4ee3\u7801\u7684\u60c5\u51b5\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5b9e\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><h4>6.1 \u540c\u6b65\u6267\u884c\u547d\u4ee4\u5e76\u6355\u83b7\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess.run()<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_command(command):<\/p>\n<p>    result = subprocess.run(command, capture_output=True, text=True)<\/p>\n<p>    if result.returncode == 0:<\/p>\n<p>        return result.stdout<\/p>\n<p>    else:<\/p>\n<p>        return result.stderr<\/p>\n<p>output = run_command([&#39;ls&#39;, &#39;-l&#39;])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u5f02\u6b65\u6267\u884c\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess.Popen()<\/code>\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_command_async(command):<\/p>\n<p>    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)<\/p>\n<p>    return process<\/p>\n<p>process = run_command_async([&#39;ping&#39;, &#39;google.com&#39;, &#39;-c&#39;, &#39;4&#39;])<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 \u5904\u7406\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u7684\u56de\u8c03\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_long_command(command):<\/p>\n<p>    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)<\/p>\n<p>    while True:<\/p>\n<p>        output = process.stdout.readline()<\/p>\n<p>        if output == &#39;&#39; and process.poll() is not None:<\/p>\n<p>            break<\/p>\n<p>        if output:<\/p>\n<p>            print(output.strip())<\/p>\n<p>run_long_command([&#39;ping&#39;, &#39;google.com&#39;, &#39;-c&#39;, &#39;10&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b89\u5168\u6027\u4e0e\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u5e94\u6ce8\u610f\u5b89\u5168\u6027\u548c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u907f\u514dShell\u6ce8\u5165<\/h4>\n<\/p>\n<p><p>\u907f\u514d\u76f4\u63a5\u4f20\u9012\u7528\u6237\u8f93\u5165\u5230shell\u547d\u4ee4\u4e2d\uff0c\u4f7f\u7528<code>subprocess.run()<\/code>\u7684<code>args<\/code>\u53c2\u6570\u4f20\u9012\u547d\u4ee4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_run_command(user_input):<\/p>\n<p>    command = [&#39;ls&#39;, &#39;-l&#39;, user_input]<\/p>\n<p>    result = subprocess.run(command, capture_output=True, text=True)<\/p>\n<p>    return result.stdout<\/p>\n<p>output = safe_run_command(&#39;\/home\/user&#39;)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6355\u83b7\u548c\u5904\u7406\u547d\u4ee4\u6267\u884c\u4e2d\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_command_with_error_handling(command):<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run(command, capture_output=True, text=True, check=True)<\/p>\n<p>        return result.stdout<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        return f&quot;Command &#39;{e.cmd}&#39; returned non-zero exit status {e.returncode}.&quot;<\/p>\n<p>output = run_command_with_error_handling([&#39;ls&#39;, &#39;-l&#39;, &#39;\/nonexistent&#39;])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>8.1 \u81ea\u52a8\u5316\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u662f\u5e38\u89c1\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5907\u4efd\u6570\u636e\u5e93\u6216\u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def backup_database():<\/p>\n<p>    command = [&#39;pg_dump&#39;, &#39;mydatabase&#39;, &#39;-f&#39;, &#39;backup.sql&#39;]<\/p>\n<p>    result = subprocess.run(command, capture_output=True, text=True)<\/p>\n<p>    if result.returncode == 0:<\/p>\n<p>        print(&quot;Backup successful.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Backup f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led: {result.stderr}&quot;)<\/p>\n<p>backup_database()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u7cfb\u7edf\u76d1\u63a7<\/h4>\n<\/p>\n<p><p>\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u83b7\u53d6\u7cfb\u7edf\u72b6\u6001\u4fe1\u606f\uff0c\u4f8b\u5982CPU\u4f7f\u7528\u7387\u3001\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def get_cpu_usage():<\/p>\n<p>    command = [&#39;mpstat&#39;, &#39;1&#39;, &#39;1&#39;]<\/p>\n<p>    result = subprocess.run(command, capture_output=True, text=True)<\/p>\n<p>    if result.returncode == 0:<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to get CPU usage: {result.stderr}&quot;)<\/p>\n<p>get_cpu_usage()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8c03\u7528\u5916\u90e8\u547d\u4ee4\uff0c\u5176\u4e2d<strong>subprocess\u6a21\u5757<\/strong>\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u5177\u6709\u529f\u80fd\u5f3a\u5927\u3001\u7075\u6d3b\u6027\u9ad8\u3001\u5b89\u5168\u6027\u597d\u7b49\u4f18\u70b9\u3002\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u8c03\u7528\u548c\u7ba1\u7406\u5916\u90e8\u547d\u4ee4\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u548c\u7cfb\u7edf\u76d1\u63a7\u7b49\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u547d\u4ee4\u8c03\u7528\u8fd8\u662f\u590d\u6742\u7684\u5f02\u6b65\u6267\u884c\uff0csubprocess\u6a21\u5757\u90fd\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884c\u5916\u90e8\u547d\u4ee4\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fd0\u884c\u547d\u4ee4\u5e76\u4e0e\u5176\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\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\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u6267\u884c<code>ls -l<\/code>\u547d\u4ee4\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6355\u83b7\u5916\u90e8\u547d\u4ee4\u7684\u8f93\u51fa\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>subprocess.run()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>subprocess.Popen()<\/code>\u6765\u6355\u83b7\u8f93\u51fa\u3002<code>Popen<\/code>\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u63a7\u5236\u9009\u9879\uff0c\u5305\u62ec\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u7684\u7ba1\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nprocess = subprocess.Popen([&#39;echo&#39;, &#39;Hello, World!&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\nstdout, stderr = process.communicate()\nprint(stdout.decode())\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728\u6267\u884c\u5916\u90e8\u547d\u4ee4\u65f6\u5982\u4f55\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u6267\u884c\u5916\u90e8\u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u547d\u4ee4\u4e0d\u5b58\u5728\u3001\u6743\u9650\u95ee\u9898\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7<code>subprocess.CalledProcessError<\/code>\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\ntry:\n    subprocess.run([&#39;invalid_command&#39;], check=True)\nexcept subprocess.CalledProcessError as e:\n    print(f&quot;\u547d\u4ee4\u6267\u884c\u5931\u8d25: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u65e0\u6548\u7684\u547d\u4ee4\uff0c\u5e76\u5728\u5931\u8d25\u65f6\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001os.system()\u51fd\u6570\u3001os.po [&hellip;]","protected":false},"author":3,"featured_media":1186633,"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\/1186626"}],"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=1186626"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186626\/revisions"}],"predecessor-version":[{"id":1186636,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186626\/revisions\/1186636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186633"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}