{"id":1129129,"date":"2025-01-08T20:26:46","date_gmt":"2025-01-08T12:26:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129129.html"},"modified":"2025-01-08T20:26:48","modified_gmt":"2025-01-08T12:26:48","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e7%9b%b8%e5%af%b9%e8%b7%af%e5%be%84%e4%b8%8b%e7%9a%84%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129129.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095637\/3cb37603-33b3-4cd5-9f57-6598ed6e3f1e.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001<code>pathlib<\/code>\u6a21\u5757\u3001\u4ee5\u53ca\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u8868\u8fbe\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u8def\u5f84\u65f6\u66f4\u52a0\u76f4\u89c2\u548c\u73b0\u4ee3\u5316\u3002<strong>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u66f4\u4e3a\u7b80\u6d01\u548c\u9ad8\u6548<\/strong>\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u529f\u80fd\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u5904\u7406\u8def\u5f84\u3001\u76ee\u5f55\u4ee5\u53ca\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u7684\u76ee\u5f55\u3002\u53ef\u4ee5\u901a\u8fc7<code>os.getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u5f97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u62fc\u63a5\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.path.join()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0e\u76f8\u5bf9\u8def\u5f84\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5f62\u6210\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = os.path.join(current_directory, &#39;relative_path&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;\u5b8c\u6574\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5185\u7f6e<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib.Path()<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8def\u5f84\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;relative_path&#39;) \/ &#39;file.txt&#39;<\/p>\n<p>print(&quot;\u8def\u5f84\u5bf9\u8c61:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u7684<code>read_text()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content = file_path.read_text()<\/p>\n<p>print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8868\u8fbe\u5f0f\u6765\u8bfb\u53d6\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u62fc\u63a5\u5b8c\u6574\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u4e0e\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u6216\u6709\u56fa\u5b9a\u76f8\u5bf9\u8def\u5f84\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u76f4\u63a5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;relative_path\/file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>..<\/code>\u8868\u793a\u4e0a\u7ea7\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u4f4d\u4e8e\u4e0a\u7ea7\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>..<\/code>\u8868\u793a\u4e0a\u7ea7\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;..\/file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5e38\u89c1\u95ee\u9898<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u8def\u5f84\u4e0d\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u4e4b\u524d\uff0c\u6700\u597d\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if file_path.exists():<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u8def\u5f84\u4e0d\u5b58\u5728:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u7f16\u7801\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u6216\u975eUTF-8\u7f16\u7801\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u5931\u8d25\u3002\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;relative_path\/file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86Python\u4e2d\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u7684\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff0c\u5206\u522b\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001<code>pathlib<\/code>\u6a21\u5757\u548c\u76f8\u5bf9\u8def\u5f84\u8868\u8fbe\u5f0f\u3002<strong>\u63a8\u8350\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u76f4\u89c2\u548c\u73b0\u4ee3\u5316<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u540c\u65f6\u6ce8\u610f\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e0d\u5b58\u5728\u548c\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6307\u5b9a\u76f8\u5bf9\u8def\u5f84\u6765\u8bfb\u53d6\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\u63d0\u4f9b\u76f8\u5bf9\u8def\u5f84\u6765\u5b9e\u73b0\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4ece\u800c\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u82e5\u5f53\u524d\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2a\u540d\u4e3a<code>data.txt<\/code>\u7684\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ncurrent_directory = os.getcwd()  # \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\nfile_path = os.path.join(current_directory, &#39;data.txt&#39;)  # \u6784\u5efa\u6587\u4ef6\u8def\u5f84\n\nwith open(file_path, &#39;r&#39;) as file:\n    content = file.read()\nprint(content)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u5bfc\u81f4\u7684\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef\uff1f<\/strong><br \/>\u5f53\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230<code>FileNotFoundError<\/code>\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0e\u9884\u671f\u4e0d\u7b26\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u524d\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u6216\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">if os.path.exists(file_path):\n    with open(file_path, &#39;r&#39;) as file:\n        content = file.read()\nelse:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u7684\u6587\u4ef6\u65f6\u8def\u5f84\u7684\u7075\u6d3b\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u6839\u636e\u6b64\u8def\u5f84\u6784\u5efa\u76f8\u5bf9\u8def\u5f84\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5982\u4f55\u53d8\u5316\uff0c\u4ee3\u7801\u90fd\u80fd\u6b63\u786e\u627e\u5230\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ncurrent_file_directory = os.path.dirname(os.path.abspath(__file__))  # \u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\nfile_path = os.path.join(current_file_directory, &#39;data.txt&#39;)  # \u6784\u5efa\u76f8\u5bf9\u8def\u5f84\n\nwith open(file_path, &#39;r&#39;) as file:\n    content = file.read()\nprint(content)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u6587\u4ef6\u8bfb\u53d6\u8def\u5f84\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u4ee5\u53ca\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u8868\u8fbe\u5f0f\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1129133,"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\/1129129"}],"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=1129129"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129129\/revisions"}],"predecessor-version":[{"id":1129134,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129129\/revisions\/1129134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129133"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}