{"id":172824,"date":"2024-05-08T17:58:31","date_gmt":"2024-05-08T09:58:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/172824.html"},"modified":"2024-05-08T17:58:38","modified_gmt":"2024-05-08T09:58:38","slug":"python-%e5%8f%af%e4%bb%a5%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e6%96%87%e4%bb%b6%e5%90%8d%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/172824.html","title":{"rendered":"Python \u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u540d\u5417"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27044550\/58404c2b-fe70-4053-af52-2eb6c3bce4d6.webp\" alt=\"Python \u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u540d\u5417\" \/><\/p>\n<p><p><strong>\u662f\u7684\uff0cPython\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u811a\u672c\u7684\u6587\u4ef6\u540d<\/strong>\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>__file__<\/code>\u5c5e\u6027\u6765\u5b9e\u73b0\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u3002\u6b64\u5916\uff0c\u6807\u51c6\u5e93\u4e2d\u7684<code>os.path<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u5904\u7406\u8def\u5f84\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.path.basename<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u6587\u4ef6\u540d\u3002<strong>\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u540d<\/strong>\uff1a\u9996\u5148\uff0c<code>__file__<\/code>\u5c5e\u6027\u4f1a\u7ed9\u51fa\u5f53\u524d\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>os.path.basename(__file__)<\/code>\u6765\u63d0\u53d6\u6587\u4ef6\u540d\u3002\u518d\u8fdb\u4e00\u6b65\uff0c\u5982\u679c\u4f60\u60f3\u5f97\u5230\u6ca1\u6709\u6269\u5c55\u540d\u7684\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.splitext(os.path.basename(__file__))[0]<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e86\u89e3<code>__file__<\/code>\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c<strong><code>__file__<\/code>\u662f\u4e00\u4e2a\u7279\u6b8a\u53d8\u91cf<\/strong>\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524d\u6267\u884c\u811a\u672c\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e2a\u5c5e\u6027\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u786e\u5b9a\u5f53\u524d\u811a\u672c\u7684\u4f4d\u7f6e\u6216\u662f\u57fa\u4e8e\u811a\u672c\u4f4d\u7f6e\u786e\u5b9a\u5176\u4ed6\u6587\u4ef6\u7684\u4f4d\u7f6e\u65f6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u5b8c\u6574\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>full_path = __file__<\/p>\n<p>print(f&quot;\u5b8c\u6574\u8def\u5f84\u4e3a: {full_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u786e\u5b9a\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dir_name = os.path.dirname(__file__)<\/p>\n<p>print(f&quot;\u76ee\u5f55\u8def\u5f84\u4e3a: {dir_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>os.path.basename<\/code>\u83b7\u53d6\u6587\u4ef6\u540d<\/h3>\n<\/p>\n<p><p><code>os.path.basename<\/code>\u51fd\u6570\u662fos.path\u6a21\u5757\u4e2d\u5904\u7406\u8def\u5f84\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u90e8\u5206\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>os.path.basename<\/code>\u63d0\u53d6\u6587\u4ef6\u540d\uff1a\n<pre><code class=\"language-python\">file_name_with_extension = os.path.basename(__file__)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\uff08\u5305\u542b\u6269\u5c55\u540d\uff09: {file_name_with_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/h3>\n<\/p>\n<p><p>\u76f8\u8f83\u4e8e\u53ea\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u8fdb\u4e00\u6b65\u5206\u79bb\u6587\u4ef6\u540d\u548c\u5176\u6269\u5c55\u540d\u3002<strong><code>os.path.splitext<\/code>\u6b63\u662f\u4e3a\u8fd9\u4e00\u9700\u6c42\u800c\u8bbe\u8ba1<\/strong>\uff0c\u5b83\u4f1a\u5c06\u6587\u4ef6\u540d\u5206\u88c2\u6210\u4e00\u4e2a\u5305\u542b\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff1a\n<pre><code class=\"language-python\">file_name, file_extension = os.path.splitext(os.path.basename(__file__))<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d: {file_name}&quot;)<\/p>\n<p>print(f&quot;\u6269\u5c55\u540d: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\uff0c\u5728\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u6216\u67d0\u4e9b\u5c01\u88c5\u7684\u73af\u5883\uff08\u5982PyInstaller\u6253\u5305\u540e\u7684\u73af\u5883\uff09\u4e2d\uff0c<code>__file__<\/code>\u53ef\u80fd\u4e0d\u4f1a\u6b63\u5e38\u5b58\u5728\u6216\u8868\u73b0\u3002\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u9700\u8981\u505a\u989d\u5916\u7684\u73af\u5883\u68c0\u67e5\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<ul>\n<li>\u6ce8\u610f\u4ea4\u4e92\u5f0f\u73af\u5883\u7f3a\u5c11<code>__file__<\/code>\uff1a\n<pre><code class=\"language-python\">try:<\/p>\n<p>  current_file = __file__<\/p>\n<p>except NameError:<\/p>\n<p>  import sys<\/p>\n<p>  current_file = sys.argv[0]<\/p>\n<p>print(f&quot;\u5f53\u524d\u6587\u4ef6\u540d: {os.path.basename(current_file)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5b9e\u9645\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e86\u89e3\u5f53\u524d\u6587\u4ef6\u540d\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u573a\u666f\uff0c\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3001\u751f\u6210\u76f8\u5bf9\u8def\u5f84\u8d44\u6e90\u7b49\u3002\u4e0b\u9762\u5c06\u7ed3\u5408\u5b9e\u8df5\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u8bb0\u5f55\u65e5\u5fd7\u662f\u76d1\u63a7\u548c\u8c03\u8bd5\u5e94\u7528\u7a0b\u5e8f\u7684\u91cd\u8981\u624b\u6bb5\u3002\u5c06\u6587\u4ef6\u540d\u5305\u542b\u5728\u65e5\u5fd7\u4e2d\uff0c\u53ef\u4ee5\u5e2e\u52a9\u66f4\u5feb\u5730\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u7684\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>\u52a8\u6001\u6a21\u5757\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2aPython\u5e94\u7528\uff0c\u5b83\u7531\u591a\u4e2a\u6a21\u5757\u7ec4\u6210\uff0c\u800c\u4f60\u5e0c\u671b\u57fa\u4e8e\u4e00\u5b9a\u7684\u89c4\u5219\u52a8\u6001\u5730\u52a0\u8f7d\u67d0\u4e9b\u6a21\u5757\uff0c\u4e86\u89e3\u5f53\u524d\u6587\u4ef6\u540d\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u8d44\u6e90\u6587\u4ef6\u8def\u5f84\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5f53\u524d\u811a\u672c\u7684\u4f4d\u7f6e\u6765\u751f\u6210\u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u6bd4\u5982\uff0c\u914d\u7f6e\u6587\u4ef6\u3001\u6570\u636e\u6587\u4ef6\u7b49\u3002\u5229\u7528<code>__file__<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6784\u5efa\u51fa\u6b63\u786e\u7684\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bed<\/h3>\n<\/p>\n<p><p>Python\u4f5c\u4e3a\u4e00\u95e8\u529f\u80fd\u4e30\u5bcc\u7684\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6a21\u5757\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u6587\u4ef6\u7684\u6587\u4ef6\u540d\uff0c\u4ee5\u53ca\u6839\u636e\u9700\u8981\u83b7\u53d6\u5206\u79bb\u7684\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u8fd9\u4e00\u529f\u80fd\u5728\u811a\u672c\u81ea\u68c0\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7b49\u65b9\u9762\u975e\u5e38\u6709\u7528\uff0c\u662f\u6bcf\u4e2aPython\u5f00\u53d1\u8005\u90fd\u5e94\u638c\u63e1\u7684\u5b9e\u7528\u6280\u80fd\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u540d\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757<code>os<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u540d\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path.basename(__file__)<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff1f<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>__file__<\/code>\u53d8\u91cf\u3002\u901a\u8fc7<code>__file__<\/code>\u53d8\u91cf\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65adPython\u811a\u672c\u662f\u5426\u88ab\u76f4\u63a5\u6267\u884c\u6216\u88ab\u5bfc\u5165\u4e3a\u6a21\u5757\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>__name__<\/code>\u53d8\u91cf\u6765\u5224\u65ad\u5f53\u524d\u811a\u672c\u662f\u5426\u88ab\u76f4\u63a5\u6267\u884c\u3002\u5f53\u811a\u672c\u88ab\u76f4\u63a5\u6267\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a<code>__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__<\/code>\uff0c\u5f53\u811a\u672c\u88ab\u5bfc\u5165\u4e3a\u6a21\u5757\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a\u6a21\u5757\u7684\u540d\u79f0\u3002\u6839\u636e<code>__name__<\/code>\u7684\u503c\u53ef\u4ee5\u505a\u51fa\u4e0d\u540c\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u662f\u7684\uff0cPython\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u811a\u672c\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684__file__\u5c5e\u6027\u6765\u5b9e\u73b0\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524d\u6587\u4ef6\u7684 [&hellip;]","protected":false},"author":3,"featured_media":172843,"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\/172824"}],"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=172824"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/172824\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/172843"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=172824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=172824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=172824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}