{"id":1032602,"date":"2024-12-31T11:35:03","date_gmt":"2024-12-31T03:35:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032602.html"},"modified":"2024-12-31T11:35:06","modified_gmt":"2024-12-31T03:35:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e5%bd%93%e5%89%8d%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1032602.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5d240a76-080b-4ee2-8758-ff452bfc1817.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>os.path.abspath(__file__)\u3001\u4f7f\u7528<\/code>os.getcwd()<code>\u3001\u4f7f\u7528<\/code>pathlib.Path(<strong>file<\/strong>).resolve()<code>&lt;\/strong&gt;\u3002\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u3002\u5176\u4e2d\uff0c<\/code>os.path.abspath(<strong>file<\/strong>)` \u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><code>os.path.abspath(__file__)<\/code> \u8fd9\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u8c03\u7528 <code>os.path<\/code> \u6a21\u5757\u4e2d\u7684 <code>abspath()<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<code>__file__<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u53d8\u91cf\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524d\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002\u901a\u8fc7\u5c06 <code>__file__<\/code> \u4f20\u9012\u7ed9 <code>abspath()<\/code> \u51fd\u6570\uff0cPython\u53ef\u4ee5\u8ba1\u7b97\u51fa\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u7684\u9700\u6c42\uff0c\u5e76\u4e14\u64cd\u4f5c\u7b80\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>os.path.abspath(__file__)<\/code><\/h3>\n<\/p>\n<p><p><code>os.path<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u8def\u5f84\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u5176\u4e2d <code>abspath()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_file_path = os.path.abspath(__file__)<\/p>\n<p>print(current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.path.abspath(__file__)<\/code> \u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <code>current_file_path<\/code> \u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c<code>print(current_file_path)<\/code> \u4f1a\u5c06\u8def\u5f84\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u4fbf\u6613\u7528<\/strong>\uff1a\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570Python\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h4>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u811a\u672c\u662f\u4ee5\u6587\u4ef6\u5f62\u5f0f\u8fd0\u884c\u7684\uff0c\u800c\u4e0d\u662f\u5728\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u7684\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u5d4c\u5165\u5f0f\u73af\u5883\u4e2d\uff08\u4f8b\u5982\u67d0\u4e9bIDE\u4e2d\u7684\u5d4c\u5165\u5f0f\u7ec8\u7aef\uff09\uff0c<code>__file__<\/code> \u53d8\u91cf\u53ef\u80fd\u4e0d\u53ef\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>os.getcwd()<\/code><\/h3>\n<\/p>\n<p><p><code>os.getcwd()<\/code> \u51fd\u6570\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u867d\u7136\u5b83\u4e0d\u80fd\u76f4\u63a5\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u4f46\u53ef\u4ee5\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_working_directory = os.getcwd()<\/p>\n<p>current_file_name = __file__<\/p>\n<p>current_file_path = os.path.join(current_working_directory, current_file_name)<\/p>\n<p>print(current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.getcwd()<\/code> \u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u7136\u540e\u901a\u8fc7 <code>os.path.join()<\/code> \u5c06\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u4e0e\u6587\u4ef6\u540d\u8fde\u63a5\uff0c\u751f\u6210\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u811a\u672c\u8fd0\u884c\u73af\u5883<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u5de5\u4f5c\u76ee\u5f55\u52a8\u6001\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u811a\u672c\u6587\u4ef6\u540d\u6b63\u786e\u65e0\u8bef\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>pathlib.Path(__file__).resolve()<\/code><\/h3>\n<\/p>\n<p><p>\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>pathlib<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>current_file_path = Path(__file__).resolve()<\/p>\n<p>print(current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Path(__file__).resolve()<\/code> \u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <code>current_file_path<\/code> \u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c<code>print(current_file_path)<\/code> \u4f1a\u5c06\u8def\u5f84\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9762\u5411\u5bf9\u8c61<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u76f4\u89c2\u548c\u73b0\u4ee3\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a<code>pathlib<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><h4>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u4f7f\u7528Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u5d4c\u5165\u5f0f\u73af\u5883\u4e2d\uff08\u4f8b\u5982\u67d0\u4e9bIDE\u4e2d\u7684\u5d4c\u5165\u5f0f\u7ec8\u7aef\uff09\uff0c<code>__file__<\/code> \u53d8\u91cf\u53ef\u80fd\u4e0d\u53ef\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>relative_path = &quot;.\/some_directory\/some_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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.path.abspath(relative_path)<\/code> \u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <code>absolute_path<\/code> \u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c<code>print(absolute_path)<\/code> \u4f1a\u5c06\u7edd\u5bf9\u8def\u5f84\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u4fbf\u6613\u7528<\/strong>\uff1a\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u76f8\u5bf9\u8def\u5f84\u6b63\u786e\u65e0\u8bef\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528<code>os<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528<code>os<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<p>current_working_directory = Path(os.getcwd()).resolve()<\/p>\n<p>current_file_path = current_working_directory \/ Path(__file__).name<\/p>\n<p>print(current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>os.getcwd()<\/code>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528<code>Path().resolve()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>Path(__file__).name<\/code>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u540d\uff0c\u5e76\u5c06\u5176\u4e0e\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u8fde\u63a5\u751f\u6210\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7ed3\u5408\u4f18\u52bf<\/strong>\uff1a\u5145\u5206\u5229\u7528\u4e86<code>os<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757\u7684\u4f18\u70b9\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u591a\u79cd\u8def\u5f84\u64cd\u4f5c\u7684\u590d\u6742\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u811a\u672c\u6587\u4ef6\u540d\u6b63\u786e\u65e0\u8bef\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong><code>os.path.abspath(__file__)<\/code> \u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7b80\u4fbf\u6613\u7528\u4e14\u517c\u5bb9\u6027\u597d\u3002<\/strong> <code>os.getcwd()<\/code> \u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u5de5\u4f5c\u76ee\u5f55\u52a8\u6001\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u573a\u666f\u3002<code>pathlib.Path(__file__).resolve()<\/code> \u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u76f4\u89c2\u3002\u7ed3\u5408\u4f7f\u7528<code>os<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757\u53ef\u4ee5\u5145\u5206\u5229\u7528\u4e24\u8005\u7684\u4f18\u70b9\uff0c\u9002\u7528\u4e8e\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\uff0c\u63d0\u9ad8\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\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path.abspath<\/code>\u548c<code>__file__<\/code>\u5c5e\u6027\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_file_path = os.path.abspath(__file__)\nprint(current_file_path)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5f53\u524d\u6267\u884c\u7684Python\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u65b9\u4fbf\u4f60\u5728\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u82e5\u60f3\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4f60\u8fd0\u884cPython\u811a\u672c\u65f6\u7684\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_working_directory = os.getcwd()\nprint(current_working_directory)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u77e5\u9053\u4f60\u7684\u811a\u672c\u5728\u4f55\u5904\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u5f0f\u662f\u5426\u76f8\u540c\uff1f<\/strong><br \/>\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u662f\u76f8\u4f3c\u7684\uff0c\u4f46\u8def\u5f84\u7684\u8868\u793a\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u53ef\u4ee5\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u5dee\u5f02\u3002\u5bf9\u4e8e\u8def\u5f84\u5206\u9694\u7b26\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path.join()<\/code>\u65b9\u6cd5\u6765\u6784\u9020\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfile_name = &quot;example.txt&quot;\ncurrent_file_path = os.path.join(os.getcwd(), file_name)\nprint(current_file_path)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5728Windows\u548cUnix\u7cfb\u7edf\u4e0a\u90fd\u80fd\u591f\u6b63\u786e\u751f\u6210\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528os.path.abspath(__file__)\u3001\u4f7f\u7528os.getcwd()\u3001\u4f7f\u7528pathlib.Pat [&hellip;]","protected":false},"author":3,"featured_media":1032606,"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\/1032602"}],"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=1032602"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032602\/revisions"}],"predecessor-version":[{"id":1032611,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032602\/revisions\/1032611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032606"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}