{"id":1183571,"date":"2025-01-15T19:16:10","date_gmt":"2025-01-15T11:16:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183571.html"},"modified":"2025-01-15T19:16:12","modified_gmt":"2025-01-15T11:16:12","slug":"python-%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183571.html","title":{"rendered":"python \u5982\u4f55\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133308\/d902020f-8296-4fe3-b32f-f2d170530a9e.webp\" alt=\"python \u5982\u4f55\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f8b\u5982\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u6216\u4e13\u95e8\u7684\u5e93\u5982pyautogui<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1asubprocess\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u591a\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528os.system()\u65b9\u6cd5\u5e76\u4f20\u5165\u8981\u6267\u884c\u7684\u547d\u4ee4\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00\u8bb0\u4e8b\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c<\/strong><\/h2>\n<p>os.system(&quot;notepad.exe&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00gedit\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6253\u5f00gedit<\/strong><\/h2>\n<p>os.system(&quot;gedit&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u7f3a\u70b9\u662f\u65e0\u6cd5\u83b7\u53d6\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\uff0c\u65e0\u6cd5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8fdb\u7a0b\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>subprocess\u6a21\u5757\u662fPython 2.4\u7248\u672c\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u751f\u6210\u5b50\u8fdb\u7a0b\uff0c\u8fde\u63a5\u5176\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u3002\u76f8\u5bf9\u4e8eos.system()\uff0csubprocess\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528subprocess\u6a21\u5757\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8c03\u7528subprocess.Popen()\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u8981\u6267\u884c\u7684\u547d\u4ee4\u548c\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00\u8bb0\u4e8b\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c<\/strong><\/h2>\n<p>subprocess.Popen([&quot;notepad.exe&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00gedit\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00gedit<\/strong><\/h2>\n<p>subprocess.Popen([&quot;gedit&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>subprocess\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u8bb0\u4e8b\u672c\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00\u8bb0\u4e8b\u672c\u5e76\u7b49\u5f85\u5176\u7ed3\u675f<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;notepad.exe&quot;])<\/p>\n<p>process.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<h2><strong>\u83b7\u53d6\u8fd4\u56de\u503c<\/strong><\/h2>\n<p>return_code = process.returncode<\/p>\n<p>print(f&quot;\u8bb0\u4e8b\u672c\u7684\u8fd4\u56de\u503c\u662f: {return_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528subprocess\u6a21\u5757\u7684\u4f18\u70b9\u662f\u529f\u80fd\u5f3a\u5927\u3001\u7075\u6d3b\u6027\u9ad8\uff0c\u7f3a\u70b9\u662f\u76f8\u5bf9\u4e8eos.system()\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528pyautogui\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>pyautogui\u662f\u4e00\u4e2a\u7528\u4e8e\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u652f\u6301\u8de8\u5e73\u53f0\u64cd\u4f5c\u3002\u9664\u4e86\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\u5916\uff0cpyautogui\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b89\u88c5pyautogui<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pyautogui\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pyautogui\u5e93\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8c03\u7528pyautogui.hotkey()\u65b9\u6cd5\uff0c\u6a21\u62df\u952e\u76d8\u5feb\u6377\u952e\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<h2><strong>\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>pyautogui.hotkey(&quot;ctrl&quot;, &quot;shift&quot;, &quot;esc&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pyautogui\u5e93\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5\u7b49\u573a\u666f\uff0c\u7f3a\u70b9\u662f\u4f9d\u8d56\u4e8e\u56fe\u5f62\u754c\u9762\uff0c\u4e0d\u80fd\u5728\u65e0\u56fe\u5f62\u754c\u9762\u7684\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u548cpyautogui\u5e93\u3002<strong>os\u6a21\u5757\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u529f\u80fd\u6709\u9650\uff0csubprocess\u6a21\u5757\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u9700\u8981\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f\u548c\u8fdb\u884c\u590d\u6742\u63a7\u5236\u7684\u573a\u666f<\/strong>\uff0cpyautogui\u5e93\u9002\u7528\u4e8e\u9700\u8981\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7684\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u3001\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u90e8\u5206\u9700\u6c42\u3002\u5728\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528os\u6a21\u5757\u548cpyautogui\u5e93\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u542f\u52a8\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u542f\u52a8\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u6a21\u5757\u5141\u8bb8\u4f60\u521b\u5efa\u65b0\u8fdb\u7a0b\uff0c\u8fde\u63a5\u5230\u4ed6\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u5f97\u8fd4\u56de\u7801\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u6765\u7b80\u5355\u5730\u542f\u52a8\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\nsubprocess.run([&#39;\u5e94\u7528\u7a0b\u5e8f\u8def\u5f84&#39;])\n<\/code><\/pre>\n<p><strong>\u80fd\u5426\u5728Python\u4e2d\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u5e76\u4f20\u9012\u53c2\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u901a\u8fc7<code>subprocess.run()<\/code>\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u7ed9\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ea\u9700\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u53c2\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">subprocess.run([&#39;\u5e94\u7528\u7a0b\u5e8f\u8def\u5f84&#39;, &#39;\u53c2\u65701&#39;, &#39;\u53c2\u65702&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u9700\u8981\u5728\u542f\u52a8\u65f6\u914d\u7f6e\u53c2\u6570\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>stdout<\/code>\u548c<code>stderr<\/code>\u53c2\u6570\u6765\u6355\u83b7\u5e94\u7528\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>subprocess.run()<\/code>\u65f6\uff0c\u53ef\u4ee5\u5982\u4e0b\u64cd\u4f5c\uff1a  <\/p>\n<pre><code class=\"language-python\">result = subprocess.run([&#39;\u5e94\u7528\u7a0b\u5e8f\u8def\u5f84&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001\u6216\u4e13\u95e8\u7684\u5e93\u5982py [&hellip;]","protected":false},"author":3,"featured_media":1183574,"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\/1183571"}],"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=1183571"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183571\/revisions"}],"predecessor-version":[{"id":1183576,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183571\/revisions\/1183576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183574"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}