{"id":1172821,"date":"2025-01-15T16:54:52","date_gmt":"2025-01-15T08:54:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172821.html"},"modified":"2025-01-15T16:54:54","modified_gmt":"2025-01-15T08:54:54","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172821.html","title":{"rendered":"python\u5982\u4f55\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074809\/5b45a89d-1dea-4b7e-9408-6d863709dab8.webp\" alt=\"python\u5982\u4f55\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u95ee\u9898\" \/><\/p>\n<p><p> <strong>Python\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u7edd\u5bf9\u8def\u5f84\u3001os\u6a21\u5757\u3001sys\u6a21\u5757\u3001Pathlib\u6a21\u5757\u6765\u89e3\u51b3<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u662f\u6700\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<strong>os\u6a21\u5757\u53ef\u4ee5\u5904\u7406\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u7528\u4e8e\u8def\u5f84\u64cd\u4f5c<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.join<\/code>\u6765\u6784\u5efa\u8de8\u5e73\u53f0\u8def\u5f84\uff0c\u4f7f\u7528<code>os.path.abspath<\/code>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\uff0c\u4f7f\u7528<code>os.path.dirname<\/code>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684\u4e00\u4e2a\u8be6\u7ec6\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>\u2514\u2500\u2500 folder\/<\/p>\n<p>    \u2514\u2500\u2500 script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5728<code>main.py<\/code>\u4e2d\u5f15\u5165<code>folder\/script.py<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6784\u5efa\u9700\u8981\u5f15\u5165\u6587\u4ef6\u7684\u8def\u5f84<\/strong><\/h2>\n<p>script_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<h2><strong>\u5c06\u811a\u672c\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(os.path.dirname(script_path))<\/p>\n<h2><strong>\u73b0\u5728\u53ef\u4ee5\u5f15\u5165script.py\u4e2d\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>import script<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6b63\u786e\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u65e0\u8bba\u4ee3\u7801\u5728\u4ec0\u4e48\u5e73\u53f0\u4e0a\u8fd0\u884c\uff0c\u8def\u5f84\u95ee\u9898\u90fd\u80fd\u5f97\u5230\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f8\u5bf9\u8def\u5f84\u4e0e\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><h4>\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u662f\u57fa\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>..<\/code>\u8868\u793a\u4e0a\u4e00\u7ea7\u76ee\u5f55\uff0c\u901a\u8fc7<code>.<\/code>\u8868\u793a\u5f53\u524d\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &#39;.\/folder\/script.py&#39;<\/p>\n<h2><strong>\u4e0a\u4e00\u7ea7\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &#39;..\/script.py&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u9002\u7528\u4e8e\u6587\u4ef6\u4f4d\u7f6e\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u8d77\u6765\u8f83\u4e3a\u7b80\u4fbf\u3002\u4f46\u5982\u679c\u9879\u76ee\u7ed3\u6784\u53d1\u751f\u53d8\u5316\uff0c\u53ef\u80fd\u9700\u8981\u4fee\u6539\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u907f\u514d\u6587\u4ef6\u4f4d\u7f6e\u53d8\u5316\u5e26\u6765\u7684\u8def\u5f84\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/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>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6784\u5efa\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u66f4\u52a0\u7a33\u5b9a\uff0c\u4f46\u5982\u679c\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u4f7f\u7528\uff0c\u53ef\u80fd\u9700\u8981\u6ce8\u610f\u8def\u5f84\u683c\u5f0f\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u7528\u4e8e\u8def\u5f84\u64cd\u4f5c\u548c\u6587\u4ef6\u7ba1\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><h4>os.path.join<\/h4>\n<\/p>\n<p><p><code>os.path.join<\/code>\u7528\u4e8e\u8de8\u5e73\u53f0\u5730\u62fc\u63a5\u8def\u5f84\uff0c\u907f\u514d\u4e86\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u65f6\u7684\u9519\u8bef\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>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6784\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>os.path.abspath<\/h4>\n<\/p>\n<p><p><code>os.path.abspath<\/code>\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = os.path.abspath(&#39;.\/folder\/script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>os.path.dirname<\/h4>\n<\/p>\n<p><p><code>os.path.dirname<\/code>\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\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\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>os.path.exists<\/h4>\n<\/p>\n<p><p><code>os.path.exists<\/code>\u7528\u4e8e\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path_exists = os.path.exists(&#39;.\/folder\/script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001sys\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9Python\u89e3\u91ca\u5668\u4f7f\u7528\u6216\u7ef4\u62a4\u7684\u53d8\u91cf\u548c\u4e0e\u89e3\u91ca\u5668\u8fdb\u884c\u4ea4\u4e92\u7684\u51fd\u6570\u3002\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\uff0c\u53ef\u4ee5\u4e34\u65f6\u6dfb\u52a0\u8def\u5f84\u5230Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6784\u5efa\u9700\u8981\u5f15\u5165\u6587\u4ef6\u7684\u8def\u5f84<\/strong><\/h2>\n<p>script_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<h2><strong>\u5c06\u811a\u672c\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(os.path.dirname(script_path))<\/p>\n<h2><strong>\u73b0\u5728\u53ef\u4ee5\u5f15\u5165script.py\u4e2d\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>import script<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001Pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>Pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u7528\u4e8e\u8def\u5f84\u64cd\u4f5c\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u7b80\u5316\u4e86\u8def\u5f84\u5904\u7406\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_path = Path(__file__).resolve().parent<\/p>\n<h2><strong>\u6784\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>file_path = current_path \/ &#39;folder&#39; \/ &#39;script.py&#39;<\/p>\n<h2><strong>\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path_exists = file_path.exists()<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with file_path.open() as f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u95ee\u9898\u65f6\uff0c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u53ef\u80fd\u4e0d\u540c\uff0c\u4f8b\u5982Windows\u4f7f\u7528\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\uff0c\u800cLinux\u548cmacOS\u4f7f\u7528\u6b63\u659c\u6760\uff08<code>\/<\/code>\uff09\u3002\u4f7f\u7528<code>os.path<\/code>\u548c<code>Pathlib<\/code>\u6a21\u5757\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528os.path<\/h4>\n<\/p>\n<p><p><code>os.path<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u5904\u7406\u51fd\u6570\uff0c\u4f8b\u5982<code>os.path.join<\/code>\u548c<code>os.path.sep<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528os.path.join\u6784\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(&#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u8def\u5f84\u5206\u9694\u7b26<\/strong><\/h2>\n<p>path_sep = os.path.sep<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pathlib<\/h4>\n<\/p>\n<p><p><code>Pathlib<\/code>\u6a21\u5757\u5728\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406\u65b9\u9762\u66f4\u52a0\u65b9\u4fbf\uff0c\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6784\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>file_path = Path(&#39;folder&#39;) \/ &#39;script.py&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u8def\u5f84\u5206\u9694\u7b26<\/strong><\/h2>\n<p>path_sep = Path().anchor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u7edd\u5bf9\u8def\u5f84\u3001<code>os<\/code>\u6a21\u5757\u3001<code>sys<\/code>\u6a21\u5757\u548c<code>Pathlib<\/code>\u6a21\u5757\u6765\u5f15\u5165\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &#39;.\/folder\/script.py&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<p>absolute_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<h2><strong>\u4f7f\u7528os\u6a21\u5757<\/strong><\/h2>\n<p>script_path = os.path.join(current_dir, &#39;folder&#39;, &#39;script.py&#39;)<\/p>\n<h2><strong>\u4f7f\u7528sys\u6a21\u5757\u6dfb\u52a0\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(os.path.dirname(script_path))<\/p>\n<p>import script<\/p>\n<h2><strong>\u4f7f\u7528Pathlib\u6a21\u5757<\/strong><\/h2>\n<p>current_path = Path(__file__).resolve().parent<\/p>\n<p>pathlib_path = current_path \/ &#39;folder&#39; \/ &#39;script.py&#39;<\/p>\n<h2><strong>\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path_exists = pathlib_path.exists()<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with pathlib_path.open() as f:<\/p>\n<p>    content = f.read()<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&quot;Relative Path: {relative_path}&quot;)<\/p>\n<p>print(f&quot;Absolute Path: {absolute_path}&quot;)<\/p>\n<p>print(f&quot;Script Path: {script_path}&quot;)<\/p>\n<p>print(f&quot;Pathlib Path: {pathlib_path}&quot;)<\/p>\n<p>print(f&quot;Path Exists: {path_exists}&quot;)<\/p>\n<p>print(f&quot;File Content: {content}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u5904\u7406\u6587\u4ef6\u8def\u5f84\u95ee\u9898\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u65e0\u8bba\u662f\u76f8\u5bf9\u8def\u5f84\u8fd8\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u65e0\u8bba\u662f<code>os<\/code>\u6a21\u5757\u8fd8\u662f<code>Pathlib<\/code>\u6a21\u5757\uff0c\u90fd\u80fd\u63d0\u4f9b\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u8def\u5f84\u5904\u7406\u80fd\u529b\uff0c\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3\u6587\u4ef6\u5f15\u5165\u7684\u8def\u5f84\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6b63\u786e\u5f15\u5165\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u901a\u5e38\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6216<code>import<\/code>\u8bed\u53e5\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u90fd\u53ef\u4ee5\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\uff0c\u800c\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u786e\u4fdd\u8def\u5f84\u4e2d\u7684\u5206\u9694\u7b26\u4f7f\u7528\u6b63\u786e\uff0c\u4f8b\u5982\u5728Windows\u7cfb\u7edf\u4e2d\u4f7f\u7528\u53cd\u659c\u6760<code>\\<\/code>\uff0c\u800c\u5728Linux\u6216macOS\u4e2d\u4f7f\u7528\u6b63\u659c\u6760<code>\/<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>join()<\/code>\u548c<code>abspath()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5982\u4f55\u6392\u67e5\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u9996\u5148\u786e\u8ba4\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e8e\u6307\u5b9a\u8def\u5f84\u3002\u4f7f\u7528<code>os.path.exists()<\/code>\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5176\u6b21\uff0c\u68c0\u67e5\u8def\u5f84\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u62fc\u5199\u9519\u8bef\u6216\u4e0d\u5fc5\u8981\u7684\u7a7a\u683c\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<code>os.getcwd()<\/code>\u6765\u786e\u8ba4\u7a0b\u5e8f\u5728\u6267\u884c\u65f6\u7684\u8def\u5f84\uff0c\u786e\u4fdd\u5f15\u7528\u7684\u8def\u5f84\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u52a8\u6001\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u52a8\u6001\u6784\u5efa\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u3002\u901a\u8fc7<code>os.path.join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4ece\u800c\u907f\u514d\u786c\u7f16\u7801\u8def\u5f84\u3002\u53ef\u4ee5\u7ed3\u5408\u7528\u6237\u8f93\u5165\u3001\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u53d8\u91cf\u6765\u6784\u5efa\u52a8\u6001\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.environ<\/code>\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8def\u5f84\u4fe1\u606f\uff0c\u6216\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u8def\u5f84\u90e8\u5206\uff0c\u7ed3\u5408\u5176\u4ed6\u8def\u5f84\u7247\u6bb5\u6784\u5efa\u6700\u7ec8\u7684\u6587\u4ef6\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u79fb\u690d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f15\u5165\u6587\u4ef6\u8def\u5f84\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u7edd\u5bf9\u8def\u5f84\u3001os\u6a21\u5757\u3001sys\u6a21\u5757\u3001Pathlib\u6a21\u5757\u6765\u89e3\u51b3\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":1172825,"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\/1172821"}],"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=1172821"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172821\/revisions"}],"predecessor-version":[{"id":1172826,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172821\/revisions\/1172826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172825"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}