{"id":1134311,"date":"2025-01-08T21:14:36","date_gmt":"2025-01-08T13:14:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134311.html"},"modified":"2025-01-08T21:14:39","modified_gmt":"2025-01-08T13:14:39","slug":"python3%e5%a6%82%e4%bd%95%e5%b0%86%e5%b1%8f%e5%b9%95%e8%be%93%e5%87%ba%e4%bd%9c%e4%b8%ba%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134311.html","title":{"rendered":"python3\u5982\u4f55\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103305\/b8fb71ca-77f5-4ec5-b1b3-5fce92e94e6e.webp\" alt=\"python3\u5982\u4f55\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\" \/><\/p>\n<p><p> <strong>Python3\u5982\u4f55\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\uff1f<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u5728Python3\u4e2d\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982<strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u4ef6\u3001\u4f7f\u7528\u7ba1\u9053\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982PyAutoGUI\uff09<\/strong>\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u5b9e\u73b0\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u4ef6\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u4fdd\u5b58\u4e0b\u6765\uff0c\u7136\u540e\u518d\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u4f5c\u4e3a\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528<code>sys.stdout<\/code>\u91cd\u5b9a\u5411<\/h4>\n<\/p>\n<p><p>Python\u7684<code>sys<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import io<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6d41<\/strong><\/h2>\n<p>output_stream = io.StringIO()<\/p>\n<h2><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u672c\u6d41<\/strong><\/h2>\n<p>sys.stdout = output_stream<\/p>\n<h2><strong>\u8f93\u51fa\u4e00\u4e9b\u5185\u5bb9<\/strong><\/h2>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u672c\u6d41\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>output_content = output_stream.getvalue()<\/p>\n<h2><strong>\u6062\u590d\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = sys.__stdout__<\/p>\n<p>print(&quot;Captured Output:&quot;, output_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a<code>StringIO<\/code>\u5bf9\u8c61\u4e2d<\/strong>\uff0c\u7136\u540e\u8bfb\u53d6\u8fd9\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u4f5c\u4e3a\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u6807\u51c6\u8f93\u51fa\u7684\u91cd\u5b9a\u5411\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import io<\/p>\n<p>from contextlib import redirect_stdout<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6d41<\/strong><\/h2>\n<p>output_stream = io.StringIO()<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>with redirect_stdout(output_stream):<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u672c\u6d41\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>output_content = output_stream.getvalue()<\/p>\n<p>print(&quot;Captured Output:&quot;, output_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7ba1\u9053<\/h3>\n<\/p>\n<p><p>\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u6765\u5c06\u4e00\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u7684\u8f93\u5165\u3002Python\u7684<code>subprocess<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u7ba1\u9053\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528<code>subprocess.PIPE<\/code><\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess.PIPE<\/code>\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u6267\u884c`echo`\u547d\u4ee4<\/strong><\/h2>\n<p>process1 = subprocess.Popen([&#39;echo&#39;, &#39;Hello, World!&#39;], stdout=subprocess.PIPE)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u8bfb\u53d6\u7b2c\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa<\/strong><\/h2>\n<p>process2 = subprocess.Popen([&#39;grep&#39;, &#39;World&#39;], stdin=process1.stdout, stdout=subprocess.PIPE)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa<\/strong><\/h2>\n<p>output, _ = process2.communicate()<\/p>\n<p>print(&quot;Captured Output:&quot;, output.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u4f7f\u7528<code>subprocess.PIPE<\/code>\u5c06<code>echo<\/code>\u547d\u4ee4\u7684\u8f93\u51fa\u4f5c\u4e3a<code>grep<\/code>\u547d\u4ee4\u7684\u8f93\u5165<\/strong>\uff0c\u7136\u540e\u83b7\u53d6<code>grep<\/code>\u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6807\u51c6\u5e93\u4e2d\u7684\u529f\u80fd\u53ef\u80fd\u4e0d\u591f\u7528\uff0c\u6216\u8005\u5b9e\u73b0\u8d77\u6765\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd9\u65f6\u5019\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982<code>PyAutoGUI<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528<code>PyAutoGUI<\/code>\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9<\/h4>\n<\/p>\n<p><p><code>PyAutoGUI<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<h2><strong>\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9<\/strong><\/h2>\n<p>screenshot = pyautogui.screenshot()<\/p>\n<h2><strong>\u5c06\u622a\u56fe\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>screenshot.save(&quot;screenshot.png&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&quot;screenshot.png&quot;, &quot;rb&quot;) as file:<\/p>\n<p>    screenshot_content = file.read()<\/p>\n<p>print(&quot;Captured Screenshot Content:&quot;, screenshot_content[:100])  # \u53ea\u663e\u793a\u524d100\u4e2a\u5b57\u8282<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u4f7f\u7528<code>PyAutoGUI<\/code>\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9\u5e76\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff0c\u7136\u540e\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528OCR\u6280\u672f\u63d0\u53d6\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4ece\u622a\u56fe\u4e2d\u63d0\u53d6\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u6280\u672f\u3002<code>pytesseract<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684OCR\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>import pytesseract<\/p>\n<h2><strong>\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9<\/strong><\/h2>\n<p>screenshot = pyautogui.screenshot()<\/p>\n<h2><strong>\u5c06\u622a\u56fe\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>screenshot.save(&quot;screenshot.png&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pytesseract\u63d0\u53d6\u6587\u672c<\/strong><\/h2>\n<p>text = pytesseract.image_to_string(&quot;screenshot.png&quot;)<\/p>\n<p>print(&quot;Extracted Text:&quot;, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u4f7f\u7528<code>pytesseract<\/code>\u4ece\u622a\u56fe\u4e2d\u63d0\u53d6\u6587\u672c<\/strong>\uff0c\u7136\u540e\u5c06\u63d0\u53d6\u7684\u6587\u672c\u4f5c\u4e3a\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python3\u4e2d\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u4ef6\u3001\u4f7f\u7528\u7ba1\u9053\u548c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982<code>PyAutoGUI<\/code>\u548c<code>pytesseract<\/code>\uff09<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u4e00\u4e9b\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5f15\u5165\u989d\u5916\u7684\u6027\u80fd\u5f00\u9500\uff0c\u4f8b\u5982\u4f7f\u7528<code>PyAutoGUI<\/code>\u622a\u53d6\u5c4f\u5e55\u5185\u5bb9\u65f6\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong>\uff1a\u67d0\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u8868\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u7ba1\u9053\u65f6\uff0c\u5728Unix\u548cLinux\u7cfb\u7edf\u4e0a\u8868\u73b0\u66f4\u597d\uff0c\u800c\u5728Windows\u4e0a\u53ef\u80fd\u9700\u8981\u989d\u5916\u914d\u7f6e\u3002<\/li>\n<li><strong>\u5b89\u5168\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u654f\u611f\u4fe1\u606f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u4f8b\u5982\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u6b63\u786e\uff0c\u907f\u514d\u4fe1\u606f\u6cc4\u9732\u3002<\/li>\n<\/ol>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5728Python3\u4e2d\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3\u4e2d\u5c06\u5c4f\u5e55\u8f93\u51fa\u6355\u83b7\u4e3a\u8f93\u5165\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6807\u51c6\u5e93\u4e2d\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u4f7f\u7528\u7ba1\u9053\u6765\u6355\u83b7\u8f93\u51fa\uff0c\u60a8\u53ef\u4ee5\u5c06\u5c4f\u5e55\u663e\u793a\u7684\u5185\u5bb9\u4f5c\u4e3a\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# \u6267\u884c\u547d\u4ee4\u5e76\u6355\u83b7\u8f93\u51fa\nprocess = subprocess.Popen([&#39;your_command&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\noutput, error = process.communicate()\n\n# \u5904\u7406\u8f93\u51fa\nif process.returncode == 0:\n    print(&quot;Captured Output:&quot;, output.decode())\nelse:\n    print(&quot;Error occurred:&quot;, error.decode())\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5c06<code>your_command<\/code>\u66ff\u6362\u4e3a\u60a8\u5e0c\u671b\u6355\u83b7\u8f93\u51fa\u7684\u5b9e\u9645\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6253\u5370\u5185\u5bb9\u91cd\u5b9a\u5411\u5230\u53d8\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>io.StringIO<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u6587\u672c\u6d41\uff0c\u60a8\u53ef\u4ee5\u5c06<code>print<\/code>\u51fd\u6570\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u8fd9\u4e2a\u6d41\u4e2d\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import io\nimport sys\n\n# \u521b\u5efa\u4e00\u4e2aStringIO\u5bf9\u8c61\noutput = io.StringIO()\n# \u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230StringIO\u5bf9\u8c61\nsys.stdout = output\n\n# \u6253\u5370\u5185\u5bb9\nprint(&quot;Hello, World!&quot;)\n\n# \u6062\u590d\u6807\u51c6\u8f93\u51fa\nsys.stdout = sys.__stdout__\n\n# \u83b7\u53d6\u8f93\u51fa\u5185\u5bb9\ncaptured_output = output.getvalue()\nprint(&quot;Captured Output:&quot;, captured_output)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6355\u83b7<code>print<\/code>\u8bed\u53e5\u8f93\u51fa\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u811a\u672c\u8bfb\u53d6\u547d\u4ee4\u884c\u8f93\u51fa\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u6267\u884c\u547d\u4ee4\u884c\u6307\u4ee4\u5e76\u8bfb\u53d6\u5176\u8f93\u51fa\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>check_output<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# \u8bfb\u53d6\u547d\u4ee4\u884c\u8f93\u51fa\ntry:\n    output = subprocess.check_output([&#39;ls&#39;, &#39;-l&#39;], text=True)\n    print(&quot;Command Output:\\n&quot;, output)\nexcept subprocess.CalledProcessError as e:\n    print(&quot;Error occurred:&quot;, e.output)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>[&#39;ls&#39;, &#39;-l&#39;]<\/code>\u662f\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66ff\u6362\u4e3a\u5176\u4ed6\u547d\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3\u5982\u4f55\u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\uff1f \u5c06\u5c4f\u5e55\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u5728Python3\u4e2d\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1134317,"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\/1134311"}],"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=1134311"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134311\/revisions"}],"predecessor-version":[{"id":1134318,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134311\/revisions\/1134318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134317"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}