{"id":1127957,"date":"2025-01-08T20:15:24","date_gmt":"2025-01-08T12:15:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127957.html"},"modified":"2025-01-08T20:15:28","modified_gmt":"2025-01-08T12:15:28","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97%e5%bd%93%e5%89%8d%e6%96%87%e4%bb%b6%e6%89%80%e5%9c%a8%e7%9b%ae%e5%bd%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127957.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u83b7\u5f97\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094808\/0bf305b4-e896-425d-a730-cf1f71da49e5.webp\" alt=\"python\u4e2d\u5982\u4f55\u83b7\u5f97\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u51e0\u79cd\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027\u3001\u7ed3\u5408<code>os.path<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>Pathlib<\/code>\u6a21\u5757\u3002\u63a8\u8350\u4f7f\u7528<code>Pathlib<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u7b80\u6d01\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4e3a\u4e86\u8bbf\u95ee\u76f8\u5bf9\u8def\u5f84\u7684\u8d44\u6e90\uff0c\u8fd8\u662f\u4e3a\u4e86\u8fdb\u884c\u6587\u4ef6\u7ba1\u7406\uff0c\u4e86\u89e3\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u51e0\u79cd\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5176\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <strong>file<\/strong> \u5c5e\u6027<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>__file__<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5185\u7f6e\u5c5e\u6027\uff0c\u5b83\u5b58\u50a8\u4e86\u5f53\u524d\u6a21\u5757\u7684\u8def\u5f84\u3002\u901a\u8fc7\u7ed3\u5408 <code>os.path<\/code> \u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/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>current_directory = os.path.dirname(current_file_path)<\/p>\n<p>print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u7b26\u53f7\u94fe\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u5305\u542b\u7b26\u53f7\u94fe\u63a5\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os.path.realpath()<\/code> \u65b9\u6cd5\uff0c\u5c06\u7b26\u53f7\u94fe\u63a5\u89e3\u6790\u4e3a\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_file_path = os.path.realpath(__file__)<\/p>\n<p>current_directory = os.path.dirname(current_file_path)<\/p>\n<p>print(&quot;Current file path (resolved):&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory (resolved):&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p><code>os.path<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u3002\u65e0\u8bba\u662f\u5728Windows\u3001macOS\u8fd8\u662fLinux\u4e0a\uff0c\u4ee5\u4e0a\u4ee3\u7801\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/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>current_directory = os.path.dirname(current_file_path)<\/p>\n<p>print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u7ed3\u5408 os.path \u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.abspath()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u7ed3\u5408 <code>__file__<\/code> \u5c5e\u6027\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/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(&quot;Absolute file path:&quot;, current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u83b7\u53d6\u76ee\u5f55\u540d<\/h3>\n<\/p>\n<p><p><code>os.path.dirname()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u8def\u5f84\u4e2d\u7684\u76ee\u5f55\u90e8\u5206\u3002\u7ed3\u5408 <code>os.path.abspath()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002<\/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>current_directory = os.path.dirname(current_file_path)<\/p>\n<p>print(&quot;Directory name:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>os.path.splitext()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/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>file_name, file_extension = os.path.splitext(current_file_path)<\/p>\n<p>print(&quot;File name:&quot;, file_name)<\/p>\n<p>print(&quot;File extension:&quot;, file_extension)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 Pathlib \u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001Pathlib \u7b80\u4ecb<\/h3>\n<\/p>\n<p><p><code>Pathlib<\/code> \u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u76f8\u6bd4\u4e8e <code>os.path<\/code> \u6a21\u5757\uff0c<code>Pathlib<\/code> \u8bed\u6cd5\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>Pathlib<\/code> \u7684 <code>Path<\/code> \u5bf9\u8c61\u6765\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u3002<\/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(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>Path<\/code> \u5bf9\u8c61\u7684 <code>parent<\/code> \u5c5e\u6027\u6765\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>current_directory = Path(__file__).resolve().parent<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p><code>Pathlib<\/code> \u6a21\u5757\u540c\u6837\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u7528\u4e8eWindows\u3001macOS\u548cLinux\u3002<\/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>current_directory = current_file_path.parent<\/p>\n<p>print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001sys\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>sys<\/code> \u6a21\u5757\u4e2d\u7684 <code>argv<\/code> \u5217\u8868\u5305\u542b\u4e86\u811a\u672c\u7684\u6267\u884c\u8def\u5f84\u3002\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<p>current_file_path = os.path.abspath(sys.argv[0])<\/p>\n<p>current_directory = os.path.dirname(current_file_path)<\/p>\n<p>print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001inspect\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>inspect<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u83b7\u53d6\u5f53\u524d\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import os<\/p>\n<p>current_file_path = inspect.getfile(inspect.currentframe())<\/p>\n<p>current_directory = os.path.dirname(os.path.abspath(current_file_path))<\/p>\n<p>print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528 <strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u5728\u811a\u672c\u7684 <code>__main__<\/code> \u90e8\u5206\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u4e5f\u662f\u4e00\u79cd\u53ef\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    current_file_path = os.path.abspath(__file__)<\/p>\n<p>    current_directory = os.path.dirname(current_file_path)<\/p>\n<p>    print(&quot;Current file path:&quot;, current_file_path)<\/p>\n<p>    print(&quot;Current directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<strong>\u63a8\u8350\u4f7f\u7528 <code>Pathlib<\/code> \u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u7b80\u6d01\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f<\/strong>\u3002\u65e0\u8bba\u662f <code>__file__<\/code> \u5c5e\u6027\u3001<code>os.path<\/code> \u6a21\u5757\u8fd8\u662f <code>Pathlib<\/code> \u6a21\u5757\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\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\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath(__file__)<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u811a\u672c\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u5e2e\u52a9\u4f60\u4e86\u89e3\u811a\u672c\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u8fd0\u884c\u811a\u672c\u65f6\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u4e0d\u4e00\u5b9a\u662f\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>\u4f7f\u7528Pathlib\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u76ee\u5f55\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u901a\u8fc7\u4f7f\u7528<code>Pathlib<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528`Path(<strong>file<\/strong>).parent<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528__file__\u5c5e\u6027\u3001\u7ed3\u5408os.path\u6a21\u5757\u3001\u4f7f\u7528Pathl [&hellip;]","protected":false},"author":3,"featured_media":1127960,"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\/1127957"}],"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=1127957"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127957\/revisions"}],"predecessor-version":[{"id":1127961,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127957\/revisions\/1127961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}