{"id":1061203,"date":"2024-12-31T15:40:42","date_gmt":"2024-12-31T07:40:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061203.html"},"modified":"2024-12-31T15:40:45","modified_gmt":"2024-12-31T07:40:45","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e7%9a%84%e7%bb%9d%e5%af%b9%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061203.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u7684\u7edd\u5bf9\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/61f90ae6-baf1-47bc-8704-1adb62400d9a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u7684\u7edd\u5bf9\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u67e5\u770bPython\u7684\u7edd\u5bf9\u8def\u5f84<\/strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757\u548c\u51fd\u6570\uff0c\u4f8b\u5982<code>os<\/code>\u548c<code>sys<\/code>\u6a21\u5757\u3002\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u3001\u811a\u672c\u6216\u6a21\u5757\u7684\u7edd\u5bf9\u8def\u5f84\u7684\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os.path.abspath<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>os.path.realpath<\/code>\u51fd\u6570\u3001\u4ee5\u53ca\u901a\u8fc7<code>sys.argv<\/code>\u83b7\u53d6\u811a\u672c\u8def\u5f84<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os.path.abspath\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.abspath<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u89c4\u8303\u5316\u7684\u7edd\u5bf9\u8def\u5f84\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>relative_path = &quot;example_directory\/example_file.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(f&quot;Absolute path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>os.path.abspath<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u5176\u5de5\u4f5c\u539f\u7406\u662f\u5c06\u4f20\u5165\u7684\u76f8\u5bf9\u8def\u5f84\u4e0e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u76f8\u7ed3\u5408\uff0c\u7136\u540e\u8fd4\u56de\u89c4\u8303\u5316\u7684\u8def\u5f84\u3002\u8fd9\u5728\u9700\u8981\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u5e76\u786e\u4fdd\u8def\u5f84\u64cd\u4f5c\u51c6\u786e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u9700\u8981\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u907f\u514d\u8def\u5f84\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os.path.realpath\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.realpath<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u3002\u4e0e<code>os.path.abspath<\/code>\u7c7b\u4f3c\uff0c\u5b83\u8fd4\u56de\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u89c4\u8303\u5316\u7edd\u5bf9\u8def\u5f84\uff0c\u4f46\u5b83\u8fd8\u4f1a\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>relative_path = &quot;example_directory\/example_file.txt&quot;<\/p>\n<p>absolute_path = os.path.realpath(relative_path)<\/p>\n<p>print(f&quot;Real absolute path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>os.path.realpath<\/code>\u4e0d\u4ec5\u4f1a\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u8fd8\u4f1a\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\uff08\u5982\u679c\u6709\uff09\u3002\u8fd9\u5728\u7b26\u53f7\u94fe\u63a5\u9891\u7e41\u4f7f\u7528\u7684\u7cfb\u7edf\uff08\u5982\u7c7bUnix\u7cfb\u7edf\uff09\u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u80fd\u591f\u786e\u4fdd\u83b7\u53d6\u5230\u5b9e\u9645\u6587\u4ef6\u7684\u8def\u5f84\u3002\u5982\u679c\u8def\u5f84\u4e2d\u5b58\u5728\u7b26\u53f7\u94fe\u63a5\uff0c<code>realpath<\/code>\u4f1a\u89e3\u6790\u5b83\u4eec\u4ee5\u8fd4\u56de\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7sys.argv\u83b7\u53d6\u811a\u672c\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>sys.argv<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>sys.argv[0]<\/code>\u901a\u5e38\u662f\u811a\u672c\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath<\/code>\u6216<code>os.path.realpath<\/code>\u8fdb\u4e00\u6b65\u83b7\u53d6\u5176\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>script_path = sys.argv[0]<\/p>\n<p>absolute_script_path = os.path.abspath(script_path)<\/p>\n<p>print(f&quot;Script absolute path: {absolute_script_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a\u901a\u8fc7<code>sys.argv[0]<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u811a\u672c\u7684\u8def\u5f84\uff0c\u5e76\u901a\u8fc7<code>os.path.abspath<\/code>\u6216<code>os.path.realpath<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u5728\u9700\u8981\u77e5\u9053\u811a\u672c\u6240\u5728\u4f4d\u7f6e\u5e76\u8fdb\u884c\u76f8\u5bf9\u8def\u5f84\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u811a\u672c\u9700\u8981\u8bbf\u95ee\u4e0e\u5176\u540c\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u8def\u5f84\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528__file__\u83b7\u53d6\u6a21\u5757\u7684\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__file__<\/code>\u5c5e\u6027\u83b7\u53d6\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5e76\u8fdb\u4e00\u6b65\u901a\u8fc7<code>os.path.abspath<\/code>\u6216<code>os.path.realpath<\/code>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>module_path = __file__<\/p>\n<p>absolute_module_path = os.path.abspath(module_path)<\/p>\n<p>print(f&quot;Module absolute path: {absolute_module_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>__file__<\/code>\u5c5e\u6027\u662f\u4e00\u4e2a\u5185\u7f6e\u5c5e\u6027\uff0c\u5305\u542b\u4e86\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\u3002\u7ed3\u5408<code>os.path.abspath<\/code>\u6216<code>os.path.realpath<\/code>\uff0c\u53ef\u4ee5\u83b7\u53d6\u6a21\u5757\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u5728\u6a21\u5757\u9700\u8981\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u6216\u8d44\u6e90\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pathlib\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>Python 3.4\u5f15\u5165\u4e86<code>pathlib<\/code>\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>Path<\/code>\u7c7b\u7684<code>resolve<\/code>\u65b9\u6cd5\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>relative_path = Path(&quot;example_directory\/example_file.txt&quot;)<\/p>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(f&quot;Pathlib absolute path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u73b0\u4ee3\u5316\u548c\u76f4\u89c2\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<code>Path<\/code>\u7c7b\u7684<code>resolve<\/code>\u65b9\u6cd5\u80fd\u591f\u89e3\u6790\u5e76\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u3002<code>pathlib<\/code>\u7684\u9762\u5411\u5bf9\u8c61\u98ce\u683c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u6e05\u6670\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408os.getcwd\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h3>\n<\/p>\n<p><p><code>os.getcwd<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u7ed3\u5408<code>os.path.join<\/code>\u53ef\u4ee5\u6784\u5efa\u76f8\u5bf9\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>current_working_directory = os.getcwd()<\/p>\n<p>relative_path = &quot;example_directory\/example_file.txt&quot;<\/p>\n<p>absolute_path = os.path.join(current_working_directory, relative_path)<\/p>\n<p>print(f&quot;Combined absolute path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a\u901a\u8fc7<code>os.getcwd<\/code>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u7ed3\u5408<code>os.path.join<\/code>\u6784\u5efa\u76f8\u5bf9\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u5728\u811a\u672c\u9700\u8981\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u5e76\u786e\u4fdd\u8def\u5f84\u64cd\u4f5c\u51c6\u786e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5728\u811a\u672c\u4e2d\u521b\u5efa\u6216\u8bbf\u95ee\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528os.path.dirname\u548cos.path.abspath\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>os.path.dirname<\/code>\u548c<code>os.path.abspath<\/code>\u7ed3\u5408\u4f7f\u7528\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>current_script_path = os.path.abspath(__file__)<\/p>\n<p>current_script_directory = os.path.dirname(current_script_path)<\/p>\n<p>print(f&quot;Current script directory absolute path: {current_script_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a\u901a\u8fc7<code>os.path.abspath<\/code>\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u518d\u4f7f\u7528<code>os.path.dirname<\/code>\u83b7\u53d6\u8be5\u8def\u5f84\u7684\u76ee\u5f55\u90e8\u5206\u3002\u8fd9\u5728\u9700\u8981\u786e\u5b9a\u811a\u672c\u6240\u5728\u76ee\u5f55\u5e76\u5728\u5176\u57fa\u7840\u4e0a\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u811a\u672c\u9700\u8981\u8bbf\u95ee\u4e0e\u5176\u540c\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90\u6587\u4ef6\u6216\u5b50\u76ee\u5f55\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u8def\u5f84\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u3001\u811a\u672c\u6216\u6a21\u5757\u7684\u7edd\u5bf9\u8def\u5f84\u3002<strong>\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os.path.abspath<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>os.path.realpath<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>sys.argv<\/code>\u83b7\u53d6\u811a\u672c\u8def\u5f84\u3001\u4f7f\u7528<code>__file__<\/code>\u83b7\u53d6\u6a21\u5757\u8def\u5f84\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u3001\u7ed3\u5408<code>os.getcwd<\/code>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3001\u4ee5\u53ca\u7ed3\u5408<code>os.path.dirname<\/code>\u548c<code>os.path.abspath<\/code>\u83b7\u53d6\u811a\u672c\u6240\u5728\u76ee\u5f55<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u8def\u5f84\u64cd\u4f5c\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5904\u7406\u8def\u5f84\u64cd\u4f5c\uff0c\u907f\u514d\u56e0\u8def\u5f84\u9519\u8bef\u5bfc\u81f4\u7684\u95ee\u9898\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\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\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_path = os.getcwd()\nprint(current_path)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u83b7\u53d6\u67d0\u4e2a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u7279\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath()<\/code>\u51fd\u6570\u3002\u53ea\u9700\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8be5\u51fd\u6570\u5c31\u4f1a\u8fd4\u56de\u8be5\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfile_path = &#39;example.txt&#39;\nabsolute_path = os.path.abspath(file_path)\nprint(absolute_path)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa<code>example.txt<\/code>\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u865a\u62df\u73af\u5883\u65f6\uff0c\u5982\u4f55\u627e\u5230\u865a\u62df\u73af\u5883\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u865a\u62df\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u865a\u62df\u73af\u5883\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f7f\u7528<code>sys.prefix<\/code>\u53ef\u4ee5\u5f97\u5230\u5f53\u524d\u865a\u62df\u73af\u5883\u7684\u6839\u76ee\u5f55\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nvirtual_env_path = sys.prefix\nprint(virtual_env_path)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u663e\u793a\u5f53\u524d\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u7684\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757\u548c\u51fd\u6570\uff0c\u4f8b\u5982os\u548csys\u6a21\u5757\u3002\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u83b7\u53d6\u5f53 [&hellip;]","protected":false},"author":3,"featured_media":1061211,"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\/1061203"}],"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=1061203"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061203\/revisions"}],"predecessor-version":[{"id":1061212,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061203\/revisions\/1061212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061211"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}