{"id":1160719,"date":"2025-01-13T19:08:14","date_gmt":"2025-01-13T11:08:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160719.html"},"modified":"2025-01-13T19:08:17","modified_gmt":"2025-01-13T11:08:17","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e8%b7%af%e5%be%84%e4%b8%8a%e6%8b%bc%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160719.html","title":{"rendered":"python\u5982\u4f55\u5728\u8def\u5f84\u4e0a\u62fc\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202126\/48d1c9cb-2d3a-4db6-b24e-225f215db590.webp\" alt=\"python\u5982\u4f55\u5728\u8def\u5f84\u4e0a\u62fc\u63a5\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u62fc\u63a5\u8def\u5f84\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u3002<strong>\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61\u3001\u6ce8\u610f\u8def\u5f84\u4e2d\u7684\u659c\u6760\u65b9\u5411<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<code>os.path.join<\/code>\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u8def\u5f84\u4e2d\u7684\u659c\u6760\u65b9\u5411\uff0c\u4f7f\u5f97\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Linux\u548cmacOS\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002<code>os.path.join<\/code>\u51fd\u6570\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\u81ea\u52a8\u4f7f\u7528\u6b63\u786e\u7684\u659c\u6760\u65b9\u5411\uff0c\u4ece\u800c\u907f\u514d\u4e86\u624b\u52a8\u5904\u7406\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>path2 = &quot;folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>full_path = os.path.join(path1, path2, file_name)<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.join<\/code>\u51fd\u6570\u5c06<code>path1<\/code>\u3001<code>path2<\/code>\u548c<code>file_name<\/code>\u62fc\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\uff0c\u5e76\u81ea\u52a8\u6dfb\u52a0\u5408\u9002\u7684\u659c\u6760\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u81ea\u52a8\u5904\u7406\u659c\u6760\u65b9\u5411<\/h3>\n<\/p>\n<p><p><code>os.path.join<\/code>\u51fd\u6570\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\u81ea\u52a8\u4f7f\u7528\u6b63\u786e\u7684\u659c\u6760\u65b9\u5411\uff0c\u4ece\u800c\u907f\u514d\u4e86\u624b\u52a8\u5904\u7406\u7684\u9ebb\u70e6\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Linux\u548cmacOS\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u659c\u6760\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>path2 = &quot;folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>full_path = os.path.join(path1, path2, file_name)<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.join<\/code>\u51fd\u6570\u5c06<code>path1<\/code>\u3001<code>path2<\/code>\u548c<code>file_name<\/code>\u62fc\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\uff0c\u5e76\u81ea\u52a8\u6dfb\u52a0\u5408\u9002\u7684\u659c\u6760\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u4f20\u5165\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\u65f6\uff0c<code>os.path.join<\/code>\u51fd\u6570\u4f1a\u5ffd\u7565\u524d\u9762\u7684\u6240\u6709\u8def\u5f84\uff0c\u53ea\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>absolute_path = &quot;\/absolute\/folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>full_path = os.path.join(path1, absolute_path, file_name)<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.join<\/code>\u51fd\u6570\u4f1a\u5ffd\u7565<code>path1<\/code>\uff0c\u53ea\u4f7f\u7528<code>absolute_path<\/code>\u548c<code>file_name<\/code>\u62fc\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u5728Python 3.4\u4e2d\u5f15\u5165\uff0c\u4e3a\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61\u53ef\u4ee5\u66f4\u52a0\u4f18\u96c5\u548c\u7b80\u6d01\u5730\u5904\u7406\u8def\u5f84\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pathlib.Path<\/code>\u7c7b\u521b\u5efa\u8def\u5f84\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u659c\u6760\u8fd0\u7b97\u7b26\uff08\/\uff09\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>path1 = Path(&quot;folder1&quot;)<\/p>\n<p>path2 = Path(&quot;folder2&quot;)<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>full_path = path1 \/ path2 \/ file_name<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u659c\u6760\u8fd0\u7b97\u7b26\u7528\u4e8e\u62fc\u63a5<code>path1<\/code>\u3001<code>path2<\/code>\u548c<code>file_name<\/code>\uff0c\u751f\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u4e0e<code>os.path.join<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u5f53\u4f60\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u65f6\uff0c<code>pathlib.Path<\/code>\u5bf9\u8c61\u4e5f\u4f1a\u5ffd\u7565\u524d\u9762\u7684\u6240\u6709\u8def\u5f84\uff0c\u53ea\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>path1 = Path(&quot;folder1&quot;)<\/p>\n<p>absolute_path = Path(&quot;\/absolute\/folder2&quot;)<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>full_path = path1 \/ absolute_path \/ file_name<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u659c\u6760\u8fd0\u7b97\u7b26\u4f1a\u5ffd\u7565<code>path1<\/code>\uff0c\u53ea\u4f7f\u7528<code>absolute_path<\/code>\u548c<code>file_name<\/code>\u62fc\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u6ce8\u610f\u8def\u5f84\u4e2d\u7684\u659c\u6760\u65b9\u5411<\/h2>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u7684\u659c\u6760\u65b9\u5411\u53ef\u80fd\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Linux\u548cmacOS\u4e2d\uff0c\u8def\u5f84\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u548c<code>pathlib.Path<\/code>\u5bf9\u8c61\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5904\u7406\u8def\u5f84\u659c\u6760\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u624b\u52a8\u5904\u7406\u659c\u6760\u65b9\u5411<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u6216<code>pathlib.Path<\/code>\u5bf9\u8c61\uff0c\u800c\u662f\u624b\u52a8\u62fc\u63a5\u8def\u5f84\uff0c\u90a3\u4e48\u9700\u8981\u6ce8\u610f\u8def\u5f84\u4e2d\u7684\u659c\u6760\u65b9\u5411\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u62fc\u63a5\u8def\u5f84<\/p>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>path2 = &quot;folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<h2><strong>\u624b\u52a8\u6dfb\u52a0\u659c\u6760<\/strong><\/h2>\n<p>full_path = path1 + &quot;\/&quot; + path2 + &quot;\/&quot; + file_name<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u6dfb\u52a0\u4e86\u6b63\u659c\u6760\uff08\/\uff09\u6765\u62fc\u63a5\u8def\u5f84\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>os.sep<\/code>\u83b7\u53d6\u659c\u6760<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.sep<\/code>\u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u8def\u5f84\u5206\u9694\u7b26<\/strong><\/h2>\n<p>sep = os.sep<\/p>\n<h2><strong>\u624b\u52a8\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>path2 = &quot;folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u8def\u5f84\u5206\u9694\u7b26<\/strong><\/h2>\n<p>full_path = path1 + sep + path2 + sep + file_name<\/p>\n<p>print(full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>os.sep<\/code>\u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5206\u9694\u7b26\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u9664\u4e86\u8def\u5f84\u7684\u62fc\u63a5\uff0c\u8fd8\u9700\u8981\u8003\u8651\u8def\u5f84\u7684\u76f8\u5bf9\u548c\u7edd\u5bf9\u95ee\u9898\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u67d0\u4e2a\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u800c\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath<\/code>\u51fd\u6570\u6216<code>pathlib.Path.resolve<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;folder1\/folder2\/file.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;folder1\/folder2\/file.txt&quot;)<\/p>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.abspath<\/code>\u51fd\u6570\u548c<code>pathlib.Path.resolve<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.relpath<\/code>\u51fd\u6570\u6216<code>pathlib.Path.relative_to<\/code>\u65b9\u6cd5\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/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>absolute_path = &quot;\/home\/user\/folder1\/folder2\/file.txt&quot;<\/p>\n<p>start_path = &quot;\/home\/user&quot;<\/p>\n<p>relative_path = os.path.relpath(absolute_path, start_path)<\/p>\n<p>print(relative_path)<\/p>\n<p><\/code><\/pre>\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>absolute_path = Path(&quot;\/home\/user\/folder1\/folder2\/file.txt&quot;)<\/p>\n<p>start_path = Path(&quot;\/home\/user&quot;)<\/p>\n<p>relative_path = absolute_path.relative_to(start_path)<\/p>\n<p>print(relative_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.relpath<\/code>\u51fd\u6570\u548c<code>pathlib.Path.relative_to<\/code>\u65b9\u6cd5\u5c06\u7edd\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8def\u5f84\u7684\u5176\u4ed6\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u8def\u5f84\u7684\u62fc\u63a5\uff0c<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u64cd\u4f5c\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3001\u521b\u5efa\u76ee\u5f55\u3001\u5220\u9664\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists<\/code>\u51fd\u6570\u6216<code>pathlib.Path.exists<\/code>\u65b9\u6cd5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path = &quot;folder1\/folder2\/file.txt&quot;<\/p>\n<p>exists = os.path.exists(path)<\/p>\n<p>print(exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path = Path(&quot;folder1\/folder2\/file.txt&quot;)<\/p>\n<p>exists = path.exists()<\/p>\n<p>print(exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.exists<\/code>\u51fd\u6570\u548c<code>pathlib.Path.exists<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs<\/code>\u51fd\u6570\u6216<code>pathlib.Path.mkdir<\/code>\u65b9\u6cd5\u521b\u5efa\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/h2>\n<p>path = &quot;folder1\/folder2&quot;<\/p>\n<p>os.makedirs(path, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/h2>\n<p>path = Path(&quot;folder1\/folder2&quot;)<\/p>\n<p>path.mkdir(parents=True, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.makedirs<\/code>\u51fd\u6570\u548c<code>pathlib.Path.mkdir<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.remove<\/code>\u51fd\u6570\u6216<code>pathlib.Path.unlink<\/code>\u65b9\u6cd5\u5220\u9664\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5220\u9664\u6587\u4ef6<\/strong><\/h2>\n<p>path = &quot;folder1\/folder2\/file.txt&quot;<\/p>\n<p>os.remove(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5220\u9664\u6587\u4ef6<\/strong><\/h2>\n<p>path = Path(&quot;folder1\/folder2\/file.txt&quot;)<\/p>\n<p>path.unlink()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.remove<\/code>\u51fd\u6570\u548c<code>pathlib.Path.unlink<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u9700\u8981\u5728\u591a\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u7684\u4ee3\u7801\u65f6\uff0c\u5904\u7406\u8def\u5f84\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os.path<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u8def\u5f84\u5904\u7406\u51fd\u6570\uff0c\u4f8b\u5982<code>os.path.join<\/code>\u3001<code>os.path.abspath<\/code>\u3001<code>os.path.exists<\/code>\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u683c\u5f0f\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path1 = &quot;folder1&quot;<\/p>\n<p>path2 = &quot;folder2&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>full_path = os.path.join(path1, path2, file_name)<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = os.path.abspath(full_path)<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>exists = os.path.exists(absolute_path)<\/p>\n<p>print(full_path)<\/p>\n<p>print(absolute_path)<\/p>\n<p>print(exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>os.path.join<\/code>\u3001<code>os.path.abspath<\/code>\u548c<code>os.path.exists<\/code>\u51fd\u6570\u7528\u4e8e\u5904\u7406\u8def\u5f84\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8def\u5f84\u3002\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61\u53ef\u4ee5\u66f4\u52a0\u4f18\u96c5\u548c\u7b80\u6d01\u5730\u5904\u7406\u8def\u5f84\u62fc\u63a5\u3001\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>path1 = Path(&quot;folder1&quot;)<\/p>\n<p>path2 = Path(&quot;folder2&quot;)<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>full_path = path1 \/ path2 \/ file_name<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = full_path.resolve()<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>exists = full_path.exists()<\/p>\n<p>print(full_path)<\/p>\n<p>print(absolute_path)<\/p>\n<p>print(exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>pathlib.Path<\/code>\u5bf9\u8c61\u7528\u4e8e\u5904\u7406\u8def\u5f84\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u62fc\u63a5\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u3002<strong>\u4f7f\u7528<code>os.path.join<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61\u3001\u6ce8\u610f\u8def\u5f84\u4e2d\u7684\u659c\u6760\u65b9\u5411<\/strong>\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath<\/code>\u548c<code>pathlib.Path.resolve<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u4f7f\u7528<code>os.path.exists<\/code>\u548c<code>pathlib.Path.exists<\/code>\u65b9\u6cd5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4f7f\u7528<code>os.makedirs<\/code>\u548c<code>pathlib.Path.mkdir<\/code>\u65b9\u6cd5\u521b\u5efa\u76ee\u5f55\uff0c\u4f7f\u7528<code>os.remove<\/code>\u548c<code>pathlib.Path.unlink<\/code>\u65b9\u6cd5\u5220\u9664\u6587\u4ef6\u3002\u5728\u7f16\u5199\u9700\u8981\u5728\u591a\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u7684\u4ee3\u7801\u65f6\uff0c\u5904\u7406\u8def\u5f84\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5728\u6587\u4ef6\u8def\u5f84\u4e2d\u62fc\u63a5\u591a\u4e2a\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u62fc\u63a5\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>join<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4f7f\u5f97\u62fc\u63a5\u66f4\u52a0\u5b89\u5168\u548c\u65b9\u4fbf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\npath1 = &quot;folder&quot;\npath2 = &quot;subfolder&quot;\nfilename = &quot;file.txt&quot;\n\nfull_path = os.path.join(path1, path2, filename)\nprint(full_path)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f1a\u751f\u6210\u9002\u5408\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u62fc\u63a5\u8def\u5f84\u65f6\uff0c\u5982\u4f55\u907f\u514d\u8def\u5f84\u9519\u8bef\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u8def\u5f84\u9519\u8bef\uff0c\u786e\u4fdd\u4f7f\u7528<code>os.path.join<\/code>\u8fdb\u884c\u62fc\u63a5\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u8def\u5f84\u7684\u6709\u6548\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u6765\u9a8c\u8bc1\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">if os.path.exists(full_path):\n    print(&quot;\u8def\u5f84\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u8def\u5f84\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u5904\u7406\u8def\u5f84\u62fc\u63a5\uff1f<\/strong><br \/>\u9664\u4e86<code>os.path<\/code>\u6a21\u5757\uff0cPython\u7684<code>pathlib<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4f7f\u7528<code>pathlib<\/code>\uff0c\u62fc\u63a5\u8def\u5f84\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\uff1a<\/p>\n<pre><code class=\"language-python\">from pathlib import Path\n\npath = Path(&quot;folder&quot;) \/ &quot;subfolder&quot; \/ &quot;file.txt&quot;\nprint(path)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u6613\u4e8e\u9605\u8bfb\uff0c\u8fd8\u652f\u6301\u94fe\u5f0f\u8c03\u7528\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u62fc\u63a5\u8def\u5f84\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u4f7f\u7528os.pat [&hellip;]","protected":false},"author":3,"featured_media":1160725,"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\/1160719"}],"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=1160719"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160719\/revisions"}],"predecessor-version":[{"id":1160731,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160719\/revisions\/1160731"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160725"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}