{"id":1042719,"date":"2024-12-31T12:59:48","date_gmt":"2024-12-31T04:59:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042719.html"},"modified":"2024-12-31T12:59:51","modified_gmt":"2024-12-31T04:59:51","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e7%9b%b8%e5%af%b9%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042719.html","title":{"rendered":"python\u5982\u4f55\u5199\u76f8\u5bf9\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/be070809-fc31-4a89-bf5f-f8405d45d647.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5199\u76f8\u5bf9\u8def\u5f84\" \/><\/p>\n<p><p> <strong>Python\u7f16\u7a0b\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bed\u6cd5\u3001\u4f7f\u7528os.path.join\u65b9\u6cd5\u3001\u907f\u514d\u786c\u7f16\u7801\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u6587\u4ef6\u8def\u5f84\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8bbf\u95ee\u6216\u64cd\u4f5c\u6587\u4ef6\u65f6\u3002\u8def\u5f84\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002\u76f8\u5bf9\u8def\u5f84\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u901a\u5e38\u66f4\u4fbf\u4e8e\u4ee3\u7801\u7684\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u7684\u57fa\u672c\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bbf\u95ee\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u5305\u542b\u5b8c\u6574\u7684\u8def\u5f84\u4fe1\u606f\uff0c\u800c\u662f\u6839\u636e\u5f53\u524d\u6267\u884c\u7684\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\u8fdb\u884c\u89e3\u6790\u3002\u76f8\u5bf9\u8def\u5f84\u901a\u5e38\u4f7f\u7528\u70b9\u7b26\u53f7\uff08. \u548c ..\uff09\u6765\u8868\u793a\u5f53\u524d\u76ee\u5f55\u548c\u4e0a\u7ea7\u76ee\u5f55\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_dir = os.getcwd()<\/p>\n<h2><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;.\/subdirectory\/file.txt&quot;<\/p>\n<p>full_path = os.path.join(current_dir, relative_path)<\/p>\n<p>print(&quot;Full path:&quot;, full_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>os.getcwd()<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7<code>os.path.join()<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u548c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u51fd\u6570\uff0c\u7279\u522b\u662f\u4e0e\u8def\u5f84\u76f8\u5173\u7684\u64cd\u4f5c\u3002\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<code>os.getcwd()<\/code><\/li>\n<li>\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<code>os.chdir(path)<\/code><\/li>\n<li>\u62fc\u63a5\u8def\u5f84\uff1a<code>os.path.join(path, *paths)<\/code><\/li>\n<li>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\uff1a<code>os.path.abspath(path)<\/code><\/li>\n<\/ol>\n<p><p>\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_dir = os.getcwd()<\/p>\n<p>print(&quot;Current Directory:&quot;, current_dir)<\/p>\n<h2><strong>\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;..&#39;)<\/p>\n<p>print(&quot;Changed Directory:&quot;, os.getcwd())<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>new_path = os.path.join(current_dir, &#39;subdirectory&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;New Path:&quot;, new_path)<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = os.path.abspath(new_path)<\/p>\n<p>print(&quot;Absolute Path:&quot;, abs_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bed\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u76f8\u5bf9\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.<\/code>\u8868\u793a\u5f53\u524d\u76ee\u5f55\uff0c<code>..<\/code>\u8868\u793a\u4e0a\u7ea7\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u53ef\u4ee5\u7528<code>.\/filename<\/code>\u8868\u793a\uff0c\u4e0a\u7ea7\u76ee\u5f55\u7684\u6587\u4ef6\u53ef\u4ee5\u7528<code>..\/filename<\/code>\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>current_file = &quot;.\/file.txt&quot;<\/p>\n<p>print(&quot;Current File:&quot;, os.path.abspath(current_file))<\/p>\n<h2><strong>\u4e0a\u7ea7\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>parent_file = &quot;..\/file.txt&quot;<\/p>\n<p>print(&quot;Parent File:&quot;, os.path.abspath(parent_file))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528os.path.join\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>os.path.join<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u5408\u6210\u4e00\u4e2a\u8def\u5f84\uff0c\u907f\u514d\u4e86\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c<code>os.path.join<\/code>\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = os.path.join(&#39;subdirectory&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;Relative Path:&quot;, os.path.abspath(relative_path))<\/p>\n<h2><strong>\u4f7f\u7528..\u8868\u793a\u4e0a\u7ea7\u76ee\u5f55<\/strong><\/h2>\n<p>parent_path = os.path.join(&#39;..&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;Parent Path:&quot;, os.path.abspath(parent_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u907f\u514d\u786c\u7f16\u7801\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u907f\u514d\u786c\u7f16\u7801\u8def\u5f84\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002\u786c\u7f16\u7801\u8def\u5f84\u4e0d\u4ec5\u964d\u4f4e\u4e86\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff0c\u8fd8\u589e\u52a0\u4e86\u7ef4\u62a4\u96be\u5ea6\u3002\u76f8\u53cd\uff0c\u5e94\u8be5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u6765\u52a8\u6001\u83b7\u53d6\u8def\u5f84\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4ece\u73af\u5883\u53d8\u91cf\u83b7\u53d6\u8def\u5f84<\/strong><\/h2>\n<p>config_path = os.getenv(&#39;CONFIG_PATH&#39;, &#39;.\/config&#39;)<\/p>\n<p>print(&quot;Config Path:&quot;, os.path.abspath(config_path))<\/p>\n<h2><strong>\u4ece\u914d\u7f6e\u6587\u4ef6\u83b7\u53d6\u8def\u5f84<\/strong><\/h2>\n<p>with open(&#39;config.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data_path = file.readline().strip()<\/p>\n<p>    print(&quot;Data Path:&quot;, os.path.abspath(data_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u793a\u4f8b\uff1a\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u5b9e\u9645\u7684\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u591a\u4e2a\u6587\u4ef6\u548c\u76ee\u5f55\u7ed3\u6784\u3002\u901a\u8fc7\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5178\u578b\u7684\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 src\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 data\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 dataset.csv<\/p>\n<p>\u2502<\/p>\n<p>\u2514\u2500\u2500 config\/<\/p>\n<p>    \u2514\u2500\u2500 settings.json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u5bf9\u8def\u5f84\u8bbf\u95ee<code>data<\/code>\u548c<code>config<\/code>\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u62fc\u63a5\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>data_path = os.path.join(current_dir, &#39;..&#39;, &#39;data&#39;, &#39;dataset.csv&#39;)<\/p>\n<p>config_path = os.path.join(current_dir, &#39;..&#39;, &#39;config&#39;, &#39;settings.json&#39;)<\/p>\n<h2><strong>\u6253\u5370\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;Data Path:&quot;, os.path.abspath(data_path))<\/p>\n<p>print(&quot;Config Path:&quot;, os.path.abspath(config_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u6709\u65f6\u4f1a\u9047\u5230\u8def\u5f84\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u8def\u5f84\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u7b26\u53f7\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u6765\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5305\u542b\u7a7a\u683c\u7684\u8def\u5f84<\/strong><\/h2>\n<p>path_with_space = &quot;.\/my folder\/file.txt&quot;<\/p>\n<p>print(&quot;Path with Space:&quot;, os.path.abspath(path_with_space))<\/p>\n<h2><strong>\u5305\u542b\u7279\u6b8a\u7b26\u53f7\u7684\u8def\u5f84<\/strong><\/h2>\n<p>path_with_special_chars = &quot;.\/folder\/!@#$%^&amp;*()_+.txt&quot;<\/p>\n<p>print(&quot;Path with Special Chars:&quot;, os.path.abspath(path_with_special_chars))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528 pathlib \u6a21\u5757<\/p>\n<\/p>\n<p><p>Python 3.4 \u53ca\u4ee5\u4e0a\u7248\u672c\u5f15\u5165\u4e86<code>pathlib<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u65b9\u5f0f\u3002\u76f8\u6bd4\u4e8e<code>os<\/code>\u6a21\u5757\uff0c<code>pathlib<\/code>\u6a21\u5757\u66f4\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = Path.cwd()<\/p>\n<p>print(&quot;Current Directory:&quot;, current_dir)<\/p>\n<h2><strong>\u62fc\u63a5\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = current_dir \/ &#39;subdirectory&#39; \/ &#39;file.txt&#39;<\/p>\n<p>print(&quot;Relative Path:&quot;, relative_path)<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = relative_path.resolve()<\/p>\n<p>print(&quot;Absolute Path:&quot;, abs_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u76f8\u5bf9\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6587\u4ef6\uff0c\u5904\u7406\u76ee\u5f55\u7684\u76f8\u5bf9\u8def\u5f84\u4e5f\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u76ee\u5f55\u3001\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>os.makedirs(&#39;.\/new_folder\/subfolder&#39;, exist_ok=True)<\/p>\n<h2><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/h2>\n<p>dir_content = os.listdir(&#39;.\/new_folder&#39;)<\/p>\n<p>print(&quot;Directory Content:&quot;, dir_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u76f8\u5bf9\u8def\u5f84\u4e0b\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>new_folder = Path(&#39;.\/new_folder\/subfolder&#39;)<\/p>\n<p>new_folder.mkdir(parents=True, exist_ok=True)<\/p>\n<h2><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/h2>\n<p>dir_content = list(new_folder.parent.iterdir())<\/p>\n<p>print(&quot;Directory Content:&quot;, dir_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u6587\u4ef6\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u548c\u4fbf\u6377\u3002\u4f8b\u5982\uff0c\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u914d\u7f6e\u4fe1\u606f\uff0c\u6216\u8005\u5c06\u5904\u7406\u7ed3\u679c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>file_path = &#39;.\/data\/sample.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;File Content:&quot;, content)<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>output_path = &#39;.\/output\/result.txt&#39;<\/p>\n<p>os.makedirs(os.path.dirname(output_path), exist_ok=True)<\/p>\n<p>with open(output_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is a sample output.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>file_path = Path(&#39;.\/data\/sample.txt&#39;)<\/p>\n<p>content = file_path.read_text()<\/p>\n<p>print(&quot;File Content:&quot;, content)<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>output_path = Path(&#39;.\/output\/result.txt&#39;)<\/p>\n<p>output_path.parent.mkdir(parents=True, exist_ok=True)<\/p>\n<p>output_path.write_text(&quot;This is a sample output.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\uff0c\u5904\u7406\u8def\u5f84\u9700\u8981\u8003\u8651\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff0cWindows\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\uff0c\u800cLinux\u548cMac\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u5dee\u5f02\u3002<\/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>path_sep = os.path.sep<\/p>\n<p>print(&quot;Path Separator:&quot;, path_sep)<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>cross_platform_path = os.path.join(&#39;folder&#39;, &#39;subfolder&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;Cross-Platform Path:&quot;, cross_platform_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>cross_platform_path = Path(&#39;folder&#39;) \/ &#39;subfolder&#39; \/ &#39;file.txt&#39;<\/p>\n<p>print(&quot;Cross-Platform Path:&quot;, cross_platform_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u786c\u7f16\u7801\u8def\u5f84\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u548c\u52a8\u6001\u83b7\u53d6\u8def\u5f84\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u65b9\u5f0f\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u76f8\u5bf9\u8def\u5f84\u4e0e\u7edd\u5bf9\u8def\u5f84\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u800c\u7edd\u5bf9\u8def\u5f84\u5219\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u4ee3\u7801\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u66f4\u52a0\u7075\u6d3b\uff0c\u4fbf\u4e8e\u79fb\u690d\u3002\u5f53\u4f60\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u65f6\uff0cPython\u4f1a\u6839\u636e\u5f53\u524d\u6267\u884c\u7684\u811a\u672c\u4f4d\u7f6e\u6765\u89e3\u6790\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u4f7f\u7528os\u6a21\u5757\u6765\u5904\u7406\u76f8\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u4f7f\u7528<code>os.path.join()<\/code>\u53ef\u4ee5\u5b89\u5168\u5730\u7ec4\u5408\u8def\u5f84\uff0c\u800c<code>os.getcwd()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u8fd9\u6837\u4f60\u5c31\u80fd\u8f7b\u677e\u5730\u6784\u5efa\u76f8\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.path.join(os.getcwd(), &#39;subfolder&#39;, &#39;file.txt&#39;)<\/code>\u53ef\u4ee5\u751f\u6210\u6307\u5411\u5f53\u524d\u76ee\u5f55\u4e0b\u67d0\u4e2a\u5b50\u6587\u4ef6\u5939\u4e2d\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u76f8\u5bf9\u8def\u5f84\u7684\u4e66\u5199\u89c4\u5219\u662f\u5426\u6709\u6240\u4e0d\u540c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u8def\u5f84\u7684\u4e66\u5199\u65b9\u5f0f\u6709\u5dee\u5f02\u3002Windows\u4f7f\u7528\u53cd\u659c\u6760<code>\\\\<\/code>\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\uff0c\u800cLinux\u548cMac\u4f7f\u7528\u6b63\u659c\u6760<code>\/<\/code>\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u63a8\u8350\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0cPython\u4f1a\u81ea\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\u7684\u5dee\u5f02\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u786e\u627e\u5230\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7f16\u7a0b\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bed\u6cd5\u3001\u4f7f\u7528os.path.join\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1042730,"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\/1042719"}],"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=1042719"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042719\/revisions"}],"predecessor-version":[{"id":1042733,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042719\/revisions\/1042733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042730"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}