{"id":176348,"date":"2024-05-08T19:09:19","date_gmt":"2024-05-08T11:09:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176348.html"},"modified":"2024-05-08T19:09:26","modified_gmt":"2024-05-08T11:09:26","slug":"python-%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%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\/176348.html","title":{"rendered":"Python \u600e\u4e48\u83b7\u53d6\u5f53\u524d\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\/27055934\/afd53369-abe6-4dd2-9786-ef3ada6c5071.webp\" alt=\"Python \u600e\u4e48\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\" \/><\/p>\n<p><p>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u5728Python\u7f16\u7a0b\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002<strong>\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\uff1a\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027\u3001\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u3001\u5229\u7528<code>Path<\/code>\u5bf9\u8c61<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u90fd\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c<code>__file__<\/code>\u5c5e\u6027\u53ef\u4ee5\u76f4\u63a5\u63d0\u4f9b\u5f53\u524d\u6267\u884c\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u9002\u5408\u4e8e\u5df2\u7ecf\u660e\u786e\u6587\u4ef6\u540d\u4e14\u9700\u8981\u76f4\u63a5\u8def\u5f84\u7684\u573a\u666f\u3002\u5b83\u662f\u4e00\u79cd\u5feb\u6377\u800c\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e2a\u5c5e\u6027\u53ef\u80fd\u4e0d\u53ef\u7528\uff0c\u4f8b\u5982\u5728\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u6216\u8005\u67d0\u4e9b\u6253\u5305\u7684\u7a0b\u5e8f\u4e2d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>Python\u6587\u4ef6\u5728\u6267\u884c\u65f6\u4f1a\u6709\u4e00\u4e2a<code>__file__<\/code>\u5c5e\u6027\uff0c\u5b83\u5305\u542b\u4e86\u8be5\u6587\u4ef6\u7684\u8def\u5f84\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>__file__<\/code>\u5c5e\u6027\u63d0\u4f9b\u7684\u662f\u76f8\u5bf9\u8def\u5f84\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u5b83\u51fd\u6570\u8f6c\u6362\u6210\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>current_file_path = __file__<\/p>\n<h2><strong>\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u6210\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_file_path = os.path.abspath(current_file_path)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u662f: {current_file_path}&quot;)<\/p>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u662f: {abs_file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p><code>os.path<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u64cd\u4f5c\u548c\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u4fe1\u606f\u3002<code>os.path.realpath()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u83b7\u5f97\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u4e0e<code>os.path.abspath()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u8fd8\u80fd\u89e3\u6790\u8def\u5f84\u4e2d\u7684\u7b26\u53f7\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_file_path = os.path.realpath(__file__)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u662f: {abs_file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u5229\u7528<code>Path<\/code>\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<code>Path<\/code>\u5bf9\u8c61\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\uff0c\u5e76\u63d0\u4f9b\u4e86\u8def\u5f84\u6240\u9700\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>current_file_path = Path(__file__).resolve()<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u662f: {current_file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001<strong>\u8003\u8651\u4e0d\u540c\u8fd0\u884c\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u6b8a\u7684\u8fd0\u884c\u73af\u5883\u4e2d\uff0c\u5982\u6253\u5305\u5e94\u7528\u6216\u5bb9\u5668\u4e2d\uff0c\u6807\u51c6\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u83b7\u5f97\u6b63\u786e\u7684\u8def\u5f84\u3002\u8fd9\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6216\u8005\u5176\u4ed6\u5b9a\u5236\u5316\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<h2><strong>\u6253\u5305\u5e94\u7528\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>if getattr(sys, &#039;frozen&#039;, False):<\/p>\n<p>    current_path = os.path.dirname(sys.executable)<\/p>\n<p>else:<\/p>\n<p>    current_path = os.path.dirname(__file__)<\/p>\n<h2><strong>\u6253\u5370\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u8def\u5f84\u662f: {current_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u5f00\u53d1\u8005\u5e94\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u8fd0\u884c\u73af\u5883\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u3002\u5fc5\u8981\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u5f02\u5e38\u5904\u7406\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58f2\u6027\u548c\u7528\u6237\u53cb\u597d\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\n\u8981\u83b7\u53d6\u5f53\u524dPython\u811a\u672c\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>__file__<\/code>\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u6253\u5370\u5f53\u524d\u811a\u672c\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ncurrent_path = os.path.abspath(__file__)\nprint(current_path)\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff1f<\/strong><br \/>\n\u8981\u83b7\u53d6\u5f53\u524dPython\u811a\u672c\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\ncurrent_directory = os.getcwd()\nprint(current_directory)\n<\/code><\/pre>\n<p>\u8be5\u4ee3\u7801\u4f1a\u6253\u5370\u51faPython\u811a\u672c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u8def\u5f84\uff1f<\/strong><br \/>\n\u8981\u83b7\u53d6\u5f53\u524dPython\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>dirname()<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u6253\u5370\u5f53\u524d\u811a\u672c\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nscript_directory = os.path.dirname(os.path.abspath(__file__))\nprint(script_directory)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8fd4\u56de\u5f53\u524d\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u5728Python\u7f16\u7a0b\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\uff1a\u4f7f\u7528__file__\u5c5e [&hellip;]","protected":false},"author":3,"featured_media":176359,"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\/176348"}],"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=176348"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176359"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}