{"id":1151570,"date":"2025-01-13T17:15:08","date_gmt":"2025-01-13T09:15:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151570.html"},"modified":"2025-01-13T17:15:11","modified_gmt":"2025-01-13T09:15:11","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e6%a1%8c%e9%9d%a2%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151570.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u684c\u9762\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181807\/f018f284-d917-4099-8f7b-053946d4a5ab.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u684c\u9762\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u6253\u5f00\u684c\u9762\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001PyAutoGUI\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>os\u6a21\u5757<\/strong>\u548c<strong>pathlib\u6a21\u5757<\/strong>\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u573a\u666f\uff0c\u800c<strong>PyAutoGUI\u5e93<\/strong>\u5219\u9002\u7528\u4e8e\u9700\u8981\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u89e3\u91ca\u4e0e\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>os<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u6765\u6784\u5efa\u6587\u4ef6\u8def\u5f84\u5e76\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84<\/strong><\/h2>\n<p>desktop_path = os.path.join(os.path.expanduser(&quot;~&quot;), &#39;Desktop&#39;)<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(desktop_path, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>os.path.expanduser(&quot;~&quot;)<\/code> \u83b7\u53d6\u7528\u6237\u7684\u4e3b\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7 <code>os.path.join<\/code> \u6784\u5efa\u51fa\u684c\u9762\u8def\u5f84\uff0c\u518d\u6784\u5efa\u51fa\u6587\u4ef6\u8def\u5f84\uff0c\u6700\u540e\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>pathlib<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u5bf9\u8c61\u5316\u5730\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u76f8\u6bd4os\u6a21\u5757\uff0cpathlib\u6a21\u5757\u66f4\u6613\u8bfb\u4e14\u66f4\u5177\u8868\u8fbe\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84<\/strong><\/h2>\n<p>desktop_path = Path.home() \/ &#39;Desktop&#39;<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = desktop_path \/ &#39;example.txt&#39;<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with file_path.open(&#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>Path.home()<\/code> \u83b7\u53d6\u7528\u6237\u7684\u4e3b\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7 <code>\/<\/code> \u8fd0\u7b97\u7b26\u6784\u5efa\u51fa\u684c\u9762\u8def\u5f84\u548c\u6587\u4ef6\u8def\u5f84\uff0c\u6700\u540e\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 PyAutoGUI \u5e93<\/h3>\n<\/p>\n<p><p>PyAutoGUI\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u64cd\u4f5c\u3002\u9002\u7528\u4e8e\u9700\u8981\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u4f8b\u5982\u5728\u684c\u9762\u4e0a\u53cc\u51fb\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84<\/strong><\/h2>\n<p>desktop_path = os.path.join(os.path.expanduser(&quot;~&quot;), &#39;Desktop&#39;)<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(desktop_path, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u901a\u8fc7PyAutoGUI\u6a21\u62df\u53cc\u51fb\u64cd\u4f5c\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>pyautogui.doubleClick(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u901a\u8fc7 <code>pyautogui.doubleClick<\/code> \u51fd\u6570\u6a21\u62df\u53cc\u51fb\u64cd\u4f5c\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>subprocess<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6267\u884c\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>subprocess<\/code> \u6a21\u5757\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728Windows\u548cmacOS\u4e0a\u90fd\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84<\/strong><\/h2>\n<p>desktop_path = os.path.join(os.path.expanduser(&quot;~&quot;), &#39;Desktop&#39;)<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(desktop_path, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u4f7f\u7528subprocess\u6a21\u5757\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>if os.name == &#39;nt&#39;:  # For Windows<\/p>\n<p>    os.startfile(file_path)<\/p>\n<p>elif os.name == &#39;posix&#39;:  # For macOS<\/p>\n<p>    subprocess.call((&#39;open&#39;, file_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u901a\u8fc7 <code>os.startfile<\/code> \u6216 <code>subprocess.call<\/code> \u51fd\u6570\u6267\u884c\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u6765\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\u90fd\u8981\u8003\u8651\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8def\u5f84\u5206\u9694\u7b26<\/strong>\uff1aWindows\u4f7f\u7528\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\uff0c\u800cmacOS\u548cLinux\u4f7f\u7528\u6b63\u659c\u6760\uff08<code>\/<\/code>\uff09\u3002\u53ef\u4ee5\u4f7f\u7528 <code>os.path.join<\/code> \u6216 <code>pathlib.Path<\/code> \u6765\u81ea\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\u3002<\/li>\n<li><strong>\u6587\u4ef6\u6743\u9650<\/strong>\uff1a\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6587\u4ef6\u6743\u9650\u7684\u5904\u7406\u4e0d\u540c\u3002\u5728\u6253\u5f00\u6587\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u6709\u8db3\u591f\u7684\u6743\u9650\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6587\u4ef6\u540d\u7f16\u7801<\/strong>\uff1a\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6587\u4ef6\u540d\u7684\u7f16\u7801\u5904\u7406\u4e0d\u540c\u3002\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u5904\u7406\u6587\u4ef6\u540d\u3002<\/li>\n<\/ol>\n<p><h3>\u5904\u7406\u6587\u4ef6\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6ca1\u6709\u6743\u9650\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code>&#8230;<code>except<\/code> \u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84<\/strong><\/h2>\n<p>desktop_path = Path.home() \/ &#39;Desktop&#39;<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = desktop_path \/ &#39;example.txt&#39;<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u5904\u7406\u5f02\u5e38<\/strong><\/h2>\n<p>try:<\/p>\n<p>    with file_path.open(&#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;File {file_path} not found.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;Permission denied to read {file_path}.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u5728Python\u4e2d\u6253\u5f00\u684c\u9762\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u3001<code>pathlib<\/code> \u6a21\u5757\u3001<code>PyAutoGUI<\/code> \u5e93\u548c <code>subprocess<\/code> \u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u548c\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5<\/strong>\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u548c\u89e3\u91ca\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u6253\u5f00\u684c\u9762\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u684c\u9762\u4e0a\u7684\u7279\u5b9a\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u6253\u5f00\u684c\u9762\u4e0a\u7684\u7279\u5b9a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u6216<code>subprocess<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u77e5\u9053\u6587\u4ef6\u7684\u786e\u5207\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u4e3a<code>example.txt<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = os.path.expanduser(&quot;~\/Desktop\/example.txt&quot;)\nos.startfile(file_path)  # Windows\n# \u6216\u8005\u4f7f\u7528 subprocess \u6a21\u5757\nimport subprocess\nsubprocess.run([&quot;open&quot;, file_path])  # macOS\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u4f1a\u5728\u9ed8\u8ba4\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6253\u5f00\u8be5\u6587\u4ef6\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u7528Python\u6253\u5f00\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff0cPython\u652f\u6301\u6253\u5f00\u591a\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u6587\u4ef6\u3001\u56fe\u50cf\u3001PDF\u7b49\u3002\u53ea\u9700\u786e\u4fdd\u4f7f\u7528\u9002\u5f53\u7684\u65b9\u6cd5\u6765\u6253\u5f00\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u6587\u672c\u6587\u4ef6\u53ef\u4ee5\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\uff0c\u800c\u56fe\u50cf\u6587\u4ef6\u5219\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u6bd4\u5982<code>PIL<\/code>\uff0c\u6765\u5c55\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6253\u5f00\u6587\u4ef6\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u6253\u5f00\u6587\u4ef6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u8def\u5f84\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u6765\u7ba1\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u80fd\u591f\u6709\u6548\u6355\u6349\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    os.startfile(file_path)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u4e86\u4e00\u4e2a\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6253\u5f00\u684c\u9762\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001PyAutoGUI\u5e93\u7b49\u3002 \u5176\u4e2d\uff0co [&hellip;]","protected":false},"author":3,"featured_media":1151580,"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\/1151570"}],"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=1151570"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151570\/revisions"}],"predecessor-version":[{"id":1151584,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151570\/revisions\/1151584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151580"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}