{"id":1104246,"date":"2025-01-08T16:19:01","date_gmt":"2025-01-08T08:19:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104246.html"},"modified":"2025-01-08T16:19:04","modified_gmt":"2025-01-08T08:19:04","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e6%96%87%e4%bb%b6%e7%9a%84%e7%bb%9d%e5%af%b9%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104246.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065707\/0391951a-268f-4dd5-8796-49d429dea6fb.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u4e2d\u7684abspath\u51fd\u6570\u3001\u4f7f\u7528Pathlib\u6a21\u5757\u4e2d\u7684resolve\u65b9\u6cd5\u3001\u7ed3\u5408os.getcwd\u51fd\u6570\u3001\u4f7f\u7528pathlib.Path\u548cos.path.realpath\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os.path.abspath\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u4e2a\u65b9\u6cd5\u6765\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os.path.abspath\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.path\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u8def\u5f84\u7684\u51fd\u6570\u3002os.path.abspath\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;example.txt&quot;<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>os.path.abspath\u51fd\u6570\u4f1a\u6839\u636e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u6765\u89e3\u6790\u76f8\u5bf9\u8def\u5f84\uff0c\u5e76\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\u3002\u5728\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u6b63\u786e\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pathlib\u6a21\u5757\u4e2d\u7684resolve\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pathlib\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528Pathlib\u6a21\u5757\u4e2d\u7684Path\u5bf9\u8c61\u548cresolve\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;example.txt&quot;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u66f4\u5177\u73b0\u4ee3\u611f\uff0c\u4e14API\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2\u3002\u63a8\u8350\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u4f7f\u7528Pathlib\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408os.getcwd\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u62fc\u63a5\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u548c\u76f8\u5bf9\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528os.getcwd\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u518d\u4f7f\u7528os.path.join\u51fd\u6570\u62fc\u63a5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<h2><strong>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;example.txt&quot;<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.join(current_directory, relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u624b\u52a8\u5904\u7406\u8def\u5f84\u7684\u573a\u666f\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u7e41\u7410\uff0c\u4e0d\u63a8\u8350\u5728\u5e38\u89c4\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pathlib.Path\u548cos.path.realpath\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.path.realpath\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\uff0c\u5305\u62ec\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;example.txt&quot;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.realpath(relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u786e\u4fdd\u83b7\u53d6\u5230\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u4ecb\u7ecdos.path.abspath\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.path.abspath\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4f1a\u6839\u636e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u89e3\u6790\u76f8\u5bf9\u8def\u5f84\uff0c\u5e76\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;docs\/example.txt&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<h2><strong>\u4f7f\u7528os.path.abspath\u51fd\u6570\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84&quot;docs\/example.txt&quot;\u3002os.getcwd\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f8b\u5982&quot;\/home\/user\/project&quot;\u3002os.path.abspath\u51fd\u6570\u4f1a\u5c06\u76f8\u5bf9\u8def\u5f84&quot;docs\/example.txt&quot;\u89e3\u6790\u4e3a\u7edd\u5bf9\u8def\u5f84&quot;\/home\/user\/project\/docs\/example.txt&quot;\u3002<\/p>\n<\/p>\n<p><p>os.path.abspath\u51fd\u6570\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u4e0d\u4f1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u5b83\u53ea\u662f\u6839\u636e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u89e3\u6790\u76f8\u5bf9\u8def\u5f84\u3002\u5982\u679c\u9700\u8981\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u7ed3\u5408os.path.exists\u51fd\u6570\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;docs\/example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.abspath\u51fd\u6570\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(absolute_path):<\/p>\n<p>    print(&quot;Path exists:&quot;, absolute_path)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Path does not exist:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>os.path.abspath\u51fd\u6570\u7684\u53e6\u4e00\u4e2a\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u5f62\u5f0f\u7684\u76f8\u5bf9\u8def\u5f84\uff0c\u5305\u62ec\u5305\u542b&quot;..&quot;\u548c&quot;.&quot;\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b&quot;..&quot;\u548c&quot;.&quot;\u7684\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;..\/project\/docs\/.\/example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.abspath\u51fd\u6570\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cos.path.abspath\u51fd\u6570\u4f1a\u89e3\u6790&quot;..&quot;\u548c&quot;.&quot;\uff0c\u8fd4\u56de\u89c4\u8303\u5316\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u4ecb\u7ecdPathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u76f8\u6bd4\u4e8eos.path\u6a21\u5757\uff0cPathlib\u6a21\u5757\u7684API\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2\uff0c\u4e14\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Pathlib\u6a21\u5757\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;docs\/example.txt&quot;)<\/p>\n<h2><strong>\u4f7f\u7528resolve\u65b9\u6cd5\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Path\u5bf9\u8c61\u8868\u793a\u76f8\u5bf9\u8def\u5f84&quot;docs\/example.txt&quot;\u3002resolve\u65b9\u6cd5\u4f1a\u5c06\u76f8\u5bf9\u8def\u5f84\u89e3\u6790\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u5b83\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684API\u6765\u5904\u7406\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;docs\/example.txt&quot;)<\/p>\n<h2><strong>\u4f7f\u7528resolve\u65b9\u6cd5\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<h2><strong>\u83b7\u53d6\u7236\u76ee\u5f55<\/strong><\/h2>\n<p>parent_directory = absolute_path.parent<\/p>\n<p>print(&quot;Parent directory:&quot;, parent_directory)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_name = absolute_path.name<\/p>\n<p>print(&quot;File name:&quot;, file_name)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540e\u7f00<\/strong><\/h2>\n<p>file_suffix = absolute_path.suffix<\/p>\n<p>print(&quot;File suffix:&quot;, file_suffix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Path\u5bf9\u8c61\u7684parent\u3001name\u548csuffix\u5c5e\u6027\u5206\u522b\u83b7\u53d6\u7236\u76ee\u5f55\u3001\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u540e\u7f00\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u4ecb\u7ecdos.getcwd\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.getcwd\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u62fc\u63a5\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u548c\u76f8\u5bf9\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528os.getcwd\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u518d\u4f7f\u7528os.path.join\u51fd\u6570\u62fc\u63a5\u8def\u5f84\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;docs\/example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.join\u51fd\u6570\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.join(current_directory, relative_path)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cos.getcwd\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f8b\u5982&quot;\/home\/user\/project&quot;\u3002os.path.join\u51fd\u6570\u4f1a\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u548c\u76f8\u5bf9\u8def\u5f84\u62fc\u63a5\u4e3a\u7edd\u5bf9\u8def\u5f84&quot;\/home\/user\/project\/docs\/example.txt&quot;\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528os.getcwd\u51fd\u6570\u548cos.path.join\u51fd\u6570\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u8def\u5f84\u7684\u7ec4\u6210\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<h2><strong>\u5b9a\u4e49\u76ee\u5f55\u548c\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>directory = &quot;docs&quot;<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.join\u51fd\u6570\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.join(current_directory, directory, file_name)<\/p>\n<p>print(&quot;Absolute path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u5b9a\u4e49\u4e86\u76ee\u5f55\u548c\u6587\u4ef6\u540d\uff0c\u5e76\u4f7f\u7528os.path.join\u51fd\u6570\u62fc\u63a5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u4ecb\u7ecdos.path.realpath\u51fd\u6570<\/h3>\n<\/p>\n<p><p>os.path.realpath\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\uff0c\u5305\u62ec\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;docs\/example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.path.realpath\u51fd\u6570\u83b7\u53d6\u771f\u5b9e\u8def\u5f84<\/strong><\/h2>\n<p>real_path = os.path.realpath(relative_path)<\/p>\n<p>print(&quot;Real path:&quot;, real_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cos.path.realpath\u51fd\u6570\u4f1a\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\uff0c\u5e76\u8fd4\u56de\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>os.path.realpath\u51fd\u6570\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u53ef\u4ee5\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u7684\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5<\/strong><\/h2>\n<p>os.symlink(&quot;docs\/example.txt&quot;, &quot;example_link.txt&quot;)<\/p>\n<h2><strong>\u4f7f\u7528os.path.realpath\u51fd\u6570\u83b7\u53d6\u771f\u5b9e\u8def\u5f84<\/strong><\/h2>\n<p>real_path = os.path.realpath(&quot;example_link.txt&quot;)<\/p>\n<p>print(&quot;Real path:&quot;, real_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u6307\u5411&quot;docs\/example.txt&quot;\u7684\u7b26\u53f7\u94fe\u63a5&quot;example_link.txt&quot;\u3002os.path.realpath\u51fd\u6570\u4f1a\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\uff0c\u5e76\u8fd4\u56de\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os.path.abspath\u51fd\u6570\u548cPathlib\u6a21\u5757\u4e2d\u7684resolve\u65b9\u6cd5\u3002os.path.abspath\u51fd\u6570\u6839\u636e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u89e3\u6790\u76f8\u5bf9\u8def\u5f84\uff0c\u5e76\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\uff0c\u800cPathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0cAPI\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u7ed3\u5408os.getcwd\u51fd\u6570\u624b\u52a8\u62fc\u63a5\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u548c\u76f8\u5bf9\u8def\u5f84\uff0c\u6216\u8005\u4f7f\u7528os.path.realpath\u51fd\u6570\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u7b80\u6d01\u76f4\u89c2\u7684API\uff0c\u63a8\u8350\u4f7f\u7528Pathlib\u6a21\u5757\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u7b26\u53f7\u94fe\u63a5\uff0c\u63a8\u8350\u4f7f\u7528os.path.realpath\u51fd\u6570\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u7684<code>abspath()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u53ea\u9700\u4f20\u5165\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\uff0c\u51fd\u6570\u5c31\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nrelative_path = &#39;example.txt&#39;\nabsolute_path = os.path.abspath(relative_path)\nprint(absolute_path)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86<code>os.path<\/code>\u6a21\u5757\u5916\uff0c<code>pathlib<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u83b7\u53d6\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u5f97\u7edd\u5bf9\u8def\u5f84\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pathlib import Path\nfile_path = Path(&#39;example.txt&#39;)\nabsolute_path = file_path.resolve()\nprint(absolute_path)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u629b\u51fa\u5f02\u5e38\u6216\u8fd4\u56de\u7279\u5b9a\u7684\u6d88\u606f\u3002\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nrelative_path = &#39;example.txt&#39;\nif os.path.exists(relative_path):\n    absolute_path = os.path.abspath(relative_path)\n    print(absolute_path)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u3002&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u4e2d\u7684abspath\u51fd\u6570\u3001\u4f7f\u7528Pathlib\u6a21\u5757\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1104262,"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\/1104246"}],"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=1104246"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104246\/revisions"}],"predecessor-version":[{"id":1104264,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104246\/revisions\/1104264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104262"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}