{"id":1003293,"date":"2024-12-27T10:16:04","date_gmt":"2024-12-27T02:16:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003293.html"},"modified":"2024-12-27T10:16:08","modified_gmt":"2024-12-27T02:16:08","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e7%94%b5%e8%84%91%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1003293.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u7535\u8111\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080718\/4cc6a513-3d2f-4942-bd6a-7a36a790e42e.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u7535\u8111\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6253\u5f00\u7535\u8111\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\uff1aos.system()\u3001subprocess.Popen()\u3001pyautogui\u5e93\u3002<\/strong>\u5176\u4e2d\uff0cos.system()\u548csubprocess.Popen()\u53ef\u4ee5\u7528\u4e8e\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u6765\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\uff0c\u800cpyautogui\u5e93\u5219\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u9f20\u6807\u548c\u952e\u76d8\u64cd\u4f5c\uff0c\u6765\u6253\u5f00\u7a97\u53e3\u3002\u8fd9\u91cc\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bbasubprocess.Popen()\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>subprocess.Popen()\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u542f\u52a8\u548c\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002\u5b83\u5141\u8bb8\u4f60\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u53ef\u4ee5\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u4efb\u52a1\u548c\u811a\u672c\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001OS.SYSTEM()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>os.system()\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5141\u8bb8\u4f60\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8be5\u51fd\u6570\u6765\u6267\u884c\u547d\u4ee4\u884c\u6307\u4ee4\uff0c\u4ece\u800c\u6253\u5f00\u7a0b\u5e8f\u7a97\u53e3\u3002\u4f8b\u5982\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(&#39;notepad.exe&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u76f8\u5bf9\u8f83\u4e3a\u6709\u9650\uff0c\u65e0\u6cd5\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\uff0c\u4e5f\u4e0d\u80fd\u8f7b\u677e\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u590d\u6742\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e8c\u3001SUBPROCESS.POPEN()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>subprocess\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u529f\u80fd\u3002subprocess.Popen()\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u5141\u8bb8\u4f60\u4e0e\u4e4b\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528subprocess.Popen()\u6253\u5f00\u8bb0\u4e8b\u672c\u7684\u793a\u4f8b\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>process = subprocess.Popen([&#39;notepad.exe&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eos.system()\u4e0d\u540c\uff0csubprocess.Popen()\u4e0d\u4f1a\u7b49\u5f85\u7a0b\u5e8f\u5173\u95ed\u540e\u518d\u7ee7\u7eed\u6267\u884c\u811a\u672c\u3002\u8fd9\u610f\u5473\u7740\u4f60\u7684Python\u811a\u672c\u4f1a\u5728\u8bb0\u4e8b\u672c\u6253\u5f00\u540e\u7acb\u5373\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p><strong>\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>subprocess.Popen()\u8fd8\u5141\u8bb8\u4f60\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06stdout\u548cstderr\u53c2\u6570\u8bbe\u7f6e\u4e3asubprocess.PIPE\uff0c\u4ee5\u4fbf\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">process = subprocess.Popen([&#39;dir&#39;], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(&#39;Output:&#39;, stdout.decode())<\/p>\n<p>print(&#39;Error:&#39;, stderr.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u811a\u672c\u5728\u5b50\u8fdb\u7a0b\u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528Popen\u5bf9\u8c61\u7684w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">process = subprocess.Popen([&#39;notepad.exe&#39;])<\/p>\n<p>process.wait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u76f4\u5230\u8bb0\u4e8b\u672c\u7a97\u53e3\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e09\u3001\u4f7f\u7528PYAUTOGUI\u5e93<\/p>\n<\/p>\n<p><p>pyautogui\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u5141\u8bb8\u4f60\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\u3002\u901a\u8fc7\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u4f60\u53ef\u4ee5\u6253\u5f00\u548c\u64cd\u4f5c\u7a97\u53e3\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5pyautogui\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6253\u5f00\u7a97\u53e3\u3002\u4f8b\u5982\uff0c\u6a21\u62df\u6309\u4e0bWin\u952e\uff0c\u7136\u540e\u8f93\u5165\u7a0b\u5e8f\u540d\u79f0\u5e76\u6309\u4e0b\u56de\u8f66\u952e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>import time<\/p>\n<h2><strong>\u6309\u4e0bWin\u952e<\/strong><\/h2>\n<p>pyautogui.press(&#39;win&#39;)<\/p>\n<p>time.sleep(1)<\/p>\n<h2><strong>\u8f93\u5165\u7a0b\u5e8f\u540d\u79f0\uff0c\u4f8b\u5982&#39;notepad&#39;<\/strong><\/h2>\n<p>pyautogui.write(&#39;notepad&#39;)<\/p>\n<p>time.sleep(1)<\/p>\n<h2><strong>\u6309\u4e0b\u56de\u8f66\u952e<\/strong><\/h2>\n<p>pyautogui.press(&#39;enter&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u66f4\u597d\u5730\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cpyautogui\u7684\u64cd\u4f5c\u662f\u57fa\u4e8e\u5c4f\u5e55\u5750\u6807\u7684\uff0c\u56e0\u6b64\u5728\u4e0d\u540c\u5206\u8fa8\u7387\u4e0b\u53ef\u80fd\u9700\u8981\u8c03\u6574\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5f00\u7535\u8111\u7a97\u53e3\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u5982\u679c\u4f60\u53ea\u9700\u8981\u7b80\u5355\u5730\u6253\u5f00\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\uff0cos.system()\u548csubprocess.Popen()\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u4ea4\u4e92\uff0c\u6216\u8005\u9700\u8981\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0cpyautogui\u53ef\u80fd\u66f4\u9002\u5408\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u7279\u5b9a\u7684\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\uff1f<\/strong><br \/>\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7<code>os<\/code>\u6a21\u5757\u6216<code>subprocess<\/code>\u6a21\u5757\u6765\u6253\u5f00\u7279\u5b9a\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6253\u5f00\u8bb0\u4e8b\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.startfile(&#39;notepad.exe&#39;)\n<\/code><\/pre>\n<p>\u6216\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\nsubprocess.Popen([&#39;notepad.exe&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7Python\u4ee3\u7801\u8f7b\u677e\u6253\u5f00\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u7a97\u53e3\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u521b\u5efa\u81ea\u5b9a\u4e49\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tkinter<\/code>\u5e93\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nwindow = tk.Tk()\nwindow.title(&quot;\u6211\u7684\u7a97\u53e3&quot;)\nwindow.geometry(&quot;400x300&quot;)\nwindow.mainloop()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7a97\u53e3\u3002<\/p>\n<p><strong>Python\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u7a97\u53e3\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>tkinter<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u7a97\u53e3\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002\u5728\u521b\u5efa\u7a97\u53e3\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>geometry<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">window.geometry(&quot;800x600+100+100&quot;)  # \u5bbd800\uff0c\u9ad8600\uff0c\u5de6\u4e0a\u89d2\u5750\u6807(100, 100)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a800&#215;600\u5927\u5c0f\u7684\u7a97\u53e3\uff0c\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u5c4f\u5e55\u7684\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6253\u5f00\u7535\u8111\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\uff1aos.system()\u3001subprocess.Popen()\u3001pyaut [&hellip;]","protected":false},"author":3,"featured_media":1003303,"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\/1003293"}],"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=1003293"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003293\/revisions"}],"predecessor-version":[{"id":1003306,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003293\/revisions\/1003306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003303"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}