{"id":1162828,"date":"2025-01-15T14:48:07","date_gmt":"2025-01-15T06:48:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162828.html"},"modified":"2025-01-15T14:48:10","modified_gmt":"2025-01-15T06:48:10","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162828.html","title":{"rendered":"python\u5982\u4f55\u5199\u6587\u4ef6\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203749\/f6ed307d-fdb4-4338-90ec-37d95f9ce2dc.webp\" alt=\"python\u5982\u4f55\u5199\u6587\u4ef6\u8def\u5f84\" \/><\/p>\n<p><p> <strong>Python\u5199\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001os.path\u6a21\u5757\u548cpathlib\u6a21\u5757<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5199\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684pathlib\u6a21\u5757\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/strong>\uff1a\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4f8b\u5982\uff1a<code>C:\/Users\/YourName\/Documents\/file.txt<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u4f4d\u7f6e\u4e0d\u4f1a\u6539\u53d8\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong>\uff1a\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<code>..\/Documents\/file.txt<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u4f4d\u7f6e\u53ef\u80fd\u53d8\u5316\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os.path\u6a21\u5757<\/strong>\uff1aos.path\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u8def\u5f84\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<code>os.path.join()<\/code>\u3001<code>os.path.abspath()<\/code>\u7b49\u3002\u5b83\u53ef\u4ee5\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\uff1apathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u5904\u7406\u8def\u5f84\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u5b83\u66f4\u76f4\u89c2\u548c\u6613\u8bfb\uff0c\u5e76\u4e14\u540c\u6837\u652f\u6301\u8de8\u5e73\u53f0\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><h4>1\u3001\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u9002\u7528\u4e8e\u6587\u4ef6\u4f4d\u7f6e\u4e0d\u4f1a\u6539\u53d8\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;C:\/Users\/YourName\/Documents\/file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u7684\u597d\u5904\u662f\u660e\u786e\u548c\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u4f46\u5982\u679c\u6587\u4ef6\u4f4d\u7f6e\u6539\u53d8\uff0c\u5219\u9700\u8981\u624b\u52a8\u66f4\u65b0\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u9002\u7528\u4e8e\u6587\u4ef6\u4f4d\u7f6e\u53ef\u80fd\u53d8\u5316\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;..\/Documents\/file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u7684\u597d\u5904\u662f\u7075\u6d3b\uff0c\u53ef\u4ee5\u968f\u9879\u76ee\u7684\u76ee\u5f55\u7ed3\u6784\u53d8\u5316\u800c\u53d8\u5316\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001os.path\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u8def\u5f84\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001os.path.join()<\/h4>\n<\/p>\n<p><p>os.path.join()\u51fd\u6570\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u6216\u591a\u4e2a\u8def\u5f84\u7ec4\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5408\u5e76\u540e\u7684\u8def\u5f84\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = os.path.join(&quot;C:&quot;, &quot;Users&quot;, &quot;YourName&quot;, &quot;Documents&quot;, &quot;file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<code>C:\/Users\/YourName\/Documents\/file.txt<\/code><\/p>\n<\/p>\n<p><h4>2\u3001os.path.abspath()<\/h4>\n<\/p>\n<p><p>os.path.abspath()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>relative_path = &quot;..\/Documents\/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><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<code>C:\/Users\/YourName\/Documents\/file.txt<\/code>\uff08\u5047\u8bbe\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a<code>C:\/Users\/YourName<\/code>\uff09<\/p>\n<\/p>\n<p><h4>3\u3001os.path.dirname()\u548cos.path.basename()<\/h4>\n<\/p>\n<p><p>os.path.dirname()\u51fd\u6570\u8fd4\u56de\u8def\u5f84\u7684\u76ee\u5f55\u90e8\u5206\uff0cos.path.basename()\u51fd\u6570\u8fd4\u56de\u8def\u5f84\u7684\u6587\u4ef6\u540d\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;C:\/Users\/YourName\/Documents\/file.txt&quot;<\/p>\n<p>directory = os.path.dirname(file_path)<\/p>\n<p>filename = os.path.basename(file_path)<\/p>\n<p>print(directory)  # \u8f93\u51fa\uff1aC:\/Users\/YourName\/Documents<\/p>\n<p>print(filename)   # \u8f93\u51fa\uff1afile.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u5904\u7406\u8def\u5f84\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u5b83\u66f4\u76f4\u89c2\u548c\u6613\u8bfb\uff0c\u5e76\u4e14\u652f\u6301\u8de8\u5e73\u53f0\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efaPath\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u521b\u5efaPath\u5bf9\u8c61\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\u8def\u5f84\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;C:\/Users\/YourName\/Documents\/file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u64cd\u4f5c\u8def\u5f84<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u64cd\u4f5c\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u8def\u5f84\u7684\u7236\u76ee\u5f55<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">parent_directory = file_path.parent<\/p>\n<p>print(parent_directory)  # \u8f93\u51fa\uff1aC:\/Users\/YourName\/Documents<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">filename = file_path.name<\/p>\n<p>print(filename)  # \u8f93\u51fa\uff1afile.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">file_extension = file_path.suffix<\/p>\n<p>print(file_extension)  # \u8f93\u51fa\uff1a.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u7ec4\u5408\u8def\u5f84<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">new_path = file_path \/ &quot;new_folder&quot; \/ &quot;new_file.txt&quot;<\/p>\n<p>print(new_path)  # \u8f93\u51fa\uff1aC:\/Users\/YourName\/Documents\/new_folder\/new_file.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u68c0\u67e5\u8def\u5f84<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u68c0\u67e5\u8def\u5f84\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">exists = file_path.exists()<\/p>\n<p>print(exists)  # \u8f93\u51fa\uff1aTrue\u6216False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u662f\u5426\u4e3a\u6587\u4ef6<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">is_file = file_path.is_file()<\/p>\n<p>print(is_file)  # \u8f93\u51fa\uff1aTrue\u6216False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u662f\u5426\u4e3a\u76ee\u5f55<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">is_dir = file_path.is_dir()<\/p>\n<p>print(is_dir)  # \u8f93\u51fa\uff1aTrue\u6216False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u8fd8\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">content = file_path.read_text()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">new_file_path = Path(&quot;C:\/Users\/YourName\/Documents\/new_file.txt&quot;)<\/p>\n<p>new_file_path.write_text(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8de8\u5e73\u53f0\u517c\u5bb9<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528os.path\u6a21\u5757\u8fd8\u662fpathlib\u6a21\u5757\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u3002os.path\u6a21\u5757\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u4f7f\u7528\u6b63\u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u800cpathlib\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u7684\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>os.path\u6a21\u5757<\/strong><\/h2>\n<p>file_path = os.path.join(&quot;C:&quot;, &quot;Users&quot;, &quot;YourName&quot;, &quot;Documents&quot;, &quot;file.txt&quot;)<\/p>\n<p>print(file_path)<\/p>\n<h2><strong>pathlib\u6a21\u5757<\/strong><\/h2>\n<p>file_path = Path(&quot;C:\/Users\/YourName\/Documents\/file.txt&quot;)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5728Windows\u548cUnix\u7cfb\u7edf\u4e0a\u90fd\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528os.path\u6a21\u5757\u548cpathlib\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528os.path\u6a21\u5757<\/strong><\/h2>\n<p>relative_path = &quot;..\/Documents\/file.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;Absolute path using os.path:&quot;, absolute_path)<\/p>\n<p>directory = os.path.dirname(absolute_path)<\/p>\n<p>filename = os.path.basename(absolute_path)<\/p>\n<p>print(&quot;Directory using os.path:&quot;, directory)<\/p>\n<p>print(&quot;Filename using os.path:&quot;, filename)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong><\/h2>\n<p>file_path = Path(&quot;C:\/Users\/YourName\/Documents\/file.txt&quot;)<\/p>\n<p>print(&quot;Absolute path using pathlib:&quot;, file_path.resolve())<\/p>\n<p>parent_directory = file_path.parent<\/p>\n<p>filename = file_path.name<\/p>\n<p>print(&quot;Parent directory using pathlib:&quot;, parent_directory)<\/p>\n<p>print(&quot;Filename using pathlib:&quot;, filename)<\/p>\n<h2><strong>\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>new_file_path = Path(&quot;C:\/Users\/YourName\/Documents\/new_file.txt&quot;)<\/p>\n<p>new_file_path.write_text(&quot;Hello, World!&quot;)<\/p>\n<p>content = new_file_path.read_text()<\/p>\n<p>print(&quot;File content using pathlib:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a\u6587\u4ef6\u4f4d\u7f6e\u3002\u7edd\u5bf9\u8def\u5f84\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\uff0c\u800c\u76f8\u5bf9\u8def\u5f84\u5219\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u7edd\u5bf9\u8def\u5f84\u53ef\u80fd\u662f<code>C:\\Users\\Username\\Documents\\file.txt<\/code>\uff0c\u800c\u76f8\u5bf9\u8def\u5f84\u53ef\u80fd\u662f<code>.\/file.txt<\/code>\uff0c\u8868\u793a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u62fc\u63a5\u548c\u8f6c\u6362\u3002\u4f7f\u7528<code>os.path.join()<\/code>\u53ef\u4ee5\u786e\u4fdd\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u4f7f\u7528\u6b63\u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfile_path = os.path.join(&#39;folder&#39;, &#39;subfolder&#39;, &#39;file.txt&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u81ea\u52a8\u9002\u914dWindows\u548cUnix\/Linux\u7cfb\u7edf\u7684\u6587\u4ef6\u8def\u5f84\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8def\u5f84\u662f\u5426\u6709\u6548\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nif os.path.exists(&#39;file.txt&#39;):\n    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u4e4b\u524d\u786e\u8ba4\u8def\u5f84\u7684\u6709\u6548\u6027\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5199\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001os.path\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u4e0b\u9762\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1162835,"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\/1162828"}],"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=1162828"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162828\/revisions"}],"predecessor-version":[{"id":1162836,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162828\/revisions\/1162836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162835"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}