{"id":939152,"date":"2024-12-26T20:28:01","date_gmt":"2024-12-26T12:28:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939152.html"},"modified":"2024-12-26T20:28:04","modified_gmt":"2024-12-26T12:28:04","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939152.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074321\/065f8351-f1bc-45a0-b0ce-1acf71fd4c5a.webp\" alt=\"python\u5982\u4f55\u6253\u5f00exe\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>os<\/code>\u6a21\u5757\u548c<code>pywinauto<\/code>\u5e93\u6765\u6253\u5f00\u548c\u63a7\u5236exe\u6587\u4ef6<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5176\u4e2d<code>subprocess<\/code>\u6a21\u5757\u662f\u8f83\u4e3a\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u6027\u3002\u901a\u8fc7<code>subprocess<\/code>\uff0c\u7528\u6237\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3001\u8fde\u63a5\u5176\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u53d6\u8fd4\u56de\u72b6\u6001\u7801\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u6253\u5f00exe\u6587\u4ef6\uff0c\u5e76\u89e3\u91ca\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SUBPROCESS\u6a21\u5757\u6253\u5f00EXE<\/p>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u4e14\u901a\u7528\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u751f\u6210\u5b50\u8fdb\u7a0b\u548c\u4e0e\u5176\u8fdb\u884c\u4ea4\u4e92\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u542f\u52a8\u548c\u63a7\u5236\u5916\u90e8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528<code>subprocess<\/code>\u6253\u5f00\u4e00\u4e2aexe\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u3002<code>subprocess.run()<\/code>\u662fPython 3.5\u4e2d\u5f15\u5165\u7684\uff0c\u9002\u5408\u6267\u884c\u7b80\u5355\u7684\u547d\u4ee4\u3002\u800c<code>subprocess.Popen()<\/code>\u63d0\u4f9b\u4e86\u66f4\u4e3a\u590d\u6742\u7684\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528 subprocess.run<\/strong><\/h2>\n<p>subprocess.run([&#39;path_to_exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;])<\/p>\n<h2><strong>\u4f7f\u7528 subprocess.Popen<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;path_to_exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;])<\/p>\n<p>process.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()  # \u7b49\u5f85\u8fdb\u7a0b\u7ed3\u675f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u65f6\u9700\u8981\u6355\u83b7exe\u7a0b\u5e8f\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u8c03\u8bd5\u3002\u53ef\u4ee5\u901a\u8fc7<code>subprocess.PIPE<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = subprocess.run([&#39;path_to_exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>output = result.stdout.decode()<\/p>\n<p>errors = result.stderr.decode()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u60a8\u8f7b\u677e\u83b7\u53d6\u548c\u5206\u6790exe\u7a0b\u5e8f\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OS\u6a21\u5757\u6253\u5f00EXE<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u867d\u7136\u529f\u80fd\u4e0d\u5982<code>subprocess<\/code>\u4e30\u5bcc\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u5b83\u7684\u4f7f\u7528\u53ef\u80fd\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>os.startfile()<\/code>\u53ef\u4ee5\u7b80\u5355\u5730\u6253\u5f00\u4e00\u4e2aexe\u6587\u4ef6\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8eWindows\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.startfile(&#39;path_to_exe&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u5c31\u50cf\u53cc\u51fb\u6587\u4ef6\u4e00\u6837\uff0c\u76f4\u63a5\u6253\u5f00\u76ee\u6807\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u5173\u5fc3\u8fd4\u56de\u503c\u6216\u6355\u83b7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528os.system()<\/strong><\/li>\n<\/ol>\n<p><p>\u867d\u7136<code>os.system()<\/code>\u4e0d\u63a8\u8350\u7528\u4e8e\u590d\u6742\u4efb\u52a1\uff0c\u4f46\u5b83\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&#39;path_to_exe arg1 arg2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u4e0d\u80fd\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PYWINAUTO\u5e93\u63a7\u5236EXE<\/p>\n<\/p>\n<p><p><code>pywinauto<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u81ea\u52a8\u5316GUI\u5e94\u7528\u7a0b\u5e8f\u7684Python\u5e93\u3002\u5b83\u5141\u8bb8\u60a8\u6253\u5f00\u548c\u63a7\u5236Windows GUI\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pywinauto<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywinauto<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8be5\u5e93\u6253\u5f00\u548c\u63a7\u5236exe\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pywinauto.application import Application<\/p>\n<p>app = Application().start(&#39;path_to_exe&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e0e\u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92<\/strong><\/li>\n<\/ol>\n<p><p><code>pywinauto<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u4e0eGUI\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\uff0c\u6bd4\u5982\u70b9\u51fb\u6309\u94ae\u3001\u8f93\u5165\u6587\u672c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dlg = app.window(title=&#39;Window Title&#39;)<\/p>\n<p>dlg.Button.click()<\/p>\n<p>dlg.Edit.type_keys(&#39;Hello World&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u81ea\u52a8\u5316\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4ec5\u9002\u7528\u4e8eWindows\u5e73\u53f0\uff0c\u5e76\u4e14\u8981\u6c42\u76ee\u6807\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u6807\u51c6\u7684Windows\u63a7\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u8fd0\u884c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.startfile()<\/code>\u6216<code>os.system()<\/code>\uff1b\u5982\u679c\u9700\u8981\u66f4\u591a\u63a7\u5236\u548c\u4ea4\u4e92\uff0c\u63a8\u8350\u4f7f\u7528<code>subprocess<\/code>\uff1b\u5982\u679c\u9700\u8981\u81ea\u52a8\u5316GUI\u64cd\u4f5c\uff0c<code>pywinauto<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6ce8\u610f\u517c\u5bb9\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u67d0\u4e9b\u65b9\u6cd5\u4ec5\u5728\u7279\u5b9a\u5e73\u53f0\u4e0a\u53ef\u7528\uff08\u4f8b\u5982<code>os.startfile()<\/code>\u548c<code>pywinauto<\/code>\u4ec5\u9002\u7528\u4e8eWindows\uff09\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u65f6\uff0c\u52a1\u5fc5\u505a\u597d\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u5e94\u5bf9\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u548c\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u68c0\u67e5exe\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3001\u786e\u4fdd\u6240\u9700\u7684\u6743\u9650\u5df2\u6388\u4e88\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u6253\u5f00\u548c\u63a7\u5236exe\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u4efb\u52a1\u8fd8\u662f\u590d\u6742\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\uff0cPython\u90fd\u80fd\u4e3a\u60a8\u63d0\u4f9b\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u4e00\u4e2aexe\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u6253\u5f00exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u5141\u8bb8\u4f60\u542f\u52a8\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u5e76\u4e0e\u5176\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsubprocess.run([&quot;C:\\\\path\\\\to\\\\yourfile.exe&quot;])\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06\u8def\u5f84\u66ff\u6362\u4e3a\u4f60\u5b9e\u9645\u7684exe\u6587\u4ef6\u8def\u5f84\u3002\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u6307\u5b9a\u7684exe\u6587\u4ef6\u5c06\u88ab\u6253\u5f00\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6253\u5f00exe\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u6253\u5f00exe\u6587\u4ef6\u65f6\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570\u3002\u53ea\u9700\u5728<code>subprocess.run()<\/code>\u4e2d\u5c06\u53c2\u6570\u4f5c\u4e3a\u5217\u8868\u7684\u4e00\u90e8\u5206\u4f20\u5165\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">subprocess.run([&quot;C:\\\\path\\\\to\\\\yourfile.exe&quot;, &quot;arg1&quot;, &quot;arg2&quot;])\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u542f\u52a8exe\u6587\u4ef6\uff0c\u5e76\u5c06<code>arg1<\/code>\u548c<code>arg2<\/code>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6253\u5f00exe\u6587\u4ef6\u540e\u7684\u8f93\u51fa\u6216\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u4f7f\u7528<code>subprocess.run()<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>stdout<\/code>\u548c<code>stderr<\/code>\u53c2\u6570\u6765\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">result = subprocess.run([&quot;C:\\\\path\\\\to\\\\yourfile.exe&quot;], capture_output=True, text=True)\nprint(result.stdout)  # \u6253\u5370\u6807\u51c6\u8f93\u51fa\nprint(result.stderr)  # \u6253\u5370\u9519\u8bef\u4fe1\u606f\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u6267\u884cexe\u6587\u4ef6\u65f6\u8fdb\u884c\u9519\u8bef\u5904\u7406\u548c\u8f93\u51fa\u76d1\u63a7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1aPython\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528subprocess\u6a21\u5757\u3001os\u6a21\u5757\u548cpywinauto\u5e93\u6765\u6253\u5f00\u548c\u63a7\u5236exe\u6587 [&hellip;]","protected":false},"author":3,"featured_media":939155,"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\/939152"}],"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=939152"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939152\/revisions"}],"predecessor-version":[{"id":939156,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939152\/revisions\/939156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/939155"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=939152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=939152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=939152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}