{"id":1140912,"date":"2025-01-08T22:26:04","date_gmt":"2025-01-08T14:26:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140912.html"},"modified":"2025-01-08T22:26:06","modified_gmt":"2025-01-08T14:26:06","slug":"python%e4%bb%a3%e7%a0%81%e4%b8%ad%e8%b7%af%e5%be%84%e5%a6%82%e4%bd%95%e7%bb%9f%e4%b8%80%e5%8f%b0%e6%b9%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1140912.html","title":{"rendered":"python\u4ee3\u7801\u4e2d\u8def\u5f84\u5982\u4f55\u7edf\u4e00\u53f0\u6e7e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103805\/3ac003b9-0b94-49ca-8215-0821d70af04d.webp\" alt=\"python\u4ee3\u7801\u4e2d\u8def\u5f84\u5982\u4f55\u7edf\u4e00\u53f0\u6e7e\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4ee3\u7801\u4e2d\u7edf\u4e00\u8def\u5f84\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001os.path\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u548c\u73af\u5883\u53d8\u91cf\u3002<\/strong> \u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u8de8\u5e73\u53f0\u5e94\u7528\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u63a8\u8350\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u5e73\u53f0\u65e0\u5173\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7edf\u4e00\u8def\u5f84\uff0c\u5e76\u4e14\u8ba8\u8bba\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7edd\u5bf9\u8def\u5f84\u4e0e\u76f8\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u6307\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u800c\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\/home\/user\/documents\/file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u5e73\u53f0\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;C:\\\\Users\\\\user\\\\documents\\\\file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u66f4\u52a0\u7075\u6d3b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;documents\/file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u5e73\u53f0\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;documents\\\\file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001os.path\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>os.path<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u5904\u7406\u8def\u5f84\uff0c\u8fd9\u4e9b\u51fd\u6570\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8868\u73b0\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>2.1 os.path.join<\/h3>\n<\/p>\n<p><p><code>os.path.join<\/code>\u53ef\u4ee5\u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u5408\u6210\u4e00\u4e2a\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>base_path = &quot;\/home\/user&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>file_path = os.path.join(base_path, file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u5e73\u53f0\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">base_path = &quot;C:\\\\Users\\\\user&quot;<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>file_path = os.path.join(base_path, file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 os.path.abspath<\/h3>\n<\/p>\n<p><p><code>os.path.abspath<\/code>\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">relative_path = &quot;documents\/file.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 os.path.dirname \u548c os.path.basename<\/h3>\n<\/p>\n<p><p><code>os.path.dirname<\/code>\u8fd4\u56de\u8def\u5f84\u7684\u76ee\u5f55\u540d\uff0c<code>os.path.basename<\/code>\u8fd4\u56de\u8def\u5f84\u7684\u6587\u4ef6\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\/home\/user\/documents\/file.txt&quot;<\/p>\n<p>directory = os.path.dirname(file_path)<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001pathlib\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\u3002<\/p>\n<\/p>\n<p><h3>3.1 Path\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>pathlib.Path<\/code>\u5bf9\u8c61\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u8def\u5f84\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/home\/user\/documents\/file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u64cd\u4f5c\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>\/<\/code>\u64cd\u4f5c\u7b26\u6765\u7ec4\u5408\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">base_path = Path(&quot;\/home\/user&quot;)<\/p>\n<p>file_name = &quot;file.txt&quot;<\/p>\n<p>file_path = base_path \/ file_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.3 \u68c0\u67e5\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if file_path.exists():<\/p>\n<p>    print(&quot;Path exists&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Path does not exist&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e94\u7528\u66f4\u52a0\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u540c\u7684\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.environ<\/code>\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>db_host = os.environ.get(&quot;DB_HOST&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.environ<\/code>\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">os.environ[&quot;DB_HOST&quot;] = &quot;localhost&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h3>5.1 \u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e76\u4e14\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8868\u73b0\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>5.2 \u907f\u514d\u786c\u7f16\u7801\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u8def\u5f84\uff0c\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u53d8\u91cf\u6765\u7ba1\u7406\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>5.3 \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u79fb\u690d\u3002<\/p>\n<\/p>\n<p><h3>5.4 \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u4e4b\u524d\uff0c\u59cb\u7ec8\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u610f\u5916\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if file_path.exists():<\/p>\n<p>    # Perform file operations<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Path does not exist&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684Python\u4ee3\u7801\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u5177\u6709\u4e00\u81f4\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\u7684\u7edf\u4e00\u6027\u3002\u901a\u8fc7\u4f7f\u7528<code>os.path.join()<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pathlib<\/code>\u5e93\u4e5f\u662f\u4e00\u4e2a\u73b0\u4ee3\u800c\u65b9\u4fbf\u7684\u9009\u62e9\uff0c\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u8f7b\u677e\u5730\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u786e\u4fdd\u8def\u5f84\u7684\u517c\u5bb9\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u8def\u5f84\u517c\u5bb9\u6027\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528<code>os.path.normpath()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06\u8def\u5f84\u89c4\u8303\u5316\uff0c\u6e05\u7406\u591a\u4f59\u7684\u5206\u9694\u7b26\u548c\u5f53\u524d\u76ee\u5f55\u6807\u8bc6\u7b26\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u800c\u4e0d\u662f\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7684\u4ee3\u7801\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u66f4\u7075\u6d3b\u5730\u8fd0\u884c\u3002\u786e\u4fdd\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u6d4b\u8bd5\u4f60\u7684\u4ee3\u7801\uff0c\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u5728Python\u4e2d\u9047\u5230\u8def\u5f84\u9519\u8bef\uff0c\u5e94\u8be5\u5982\u4f55\u6392\u67e5\uff1f<\/strong><br \/>\u9047\u5230\u8def\u5f84\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u8def\u5f84\u548c\u4f7f\u7528<code>os.path.exists()<\/code>\u6765\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u786e\u4fdd\u8def\u5f84\u7684\u683c\u5f0f\u6b63\u786e\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0cWindows\u548cLinux\u7684\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u4e0d\u540c\u3002\u901a\u8fc7\u8c03\u8bd5\u5de5\u5177\u9010\u6b65\u68c0\u67e5\u8def\u5f84\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a \u5728Python\u4ee3\u7801\u4e2d\u7edf\u4e00\u8def\u5f84\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001os.path\u6a21\u5757\u3001pathlib\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1140918,"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\/1140912"}],"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=1140912"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140912\/revisions"}],"predecessor-version":[{"id":1140919,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140912\/revisions\/1140919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140918"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}