{"id":995266,"date":"2024-12-27T09:03:25","date_gmt":"2024-12-27T01:03:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995266.html"},"modified":"2024-12-27T09:03:27","modified_gmt":"2024-12-27T01:03:27","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995266.html","title":{"rendered":"python\u5982\u4f55\u5199\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071931\/da8ca405-dfe7-426b-b8ca-ec8085b53307.webp\" alt=\"python\u5982\u4f55\u5199\u8def\u5f84\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5199\u8def\u5f84\u65f6\u9700\u8981\u6ce8\u610f\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<strong>\u4f7f\u7528raw\u5b57\u7b26\u4e32\u9632\u6b62\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528os.path\u6a21\u5757\u786e\u4fdd\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3001\u4f7f\u7528pathlib\u6a21\u5757\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528raw\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u8def\u5f84\u4e2d\u7684\u53cd\u659c\u6760\u88ab\u8bef\u8ba4\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528RAW\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8def\u5f84\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u53cd\u659c\u6760(<code><\/code>)\u65f6\uff0c\u5bb9\u6613\u88ab\u89e3\u91ca\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528raw\u5b57\u7b26\u4e32\u3002\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>r<\/code>\uff0c\u4f8b\u5982\uff1a<code>r&quot;C:\\Users\\Name\\Documents&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u4e2d\u7684\u53cd\u659c\u6760\u88ab\u6b63\u786e\u8bc6\u522b\u4e3a\u8def\u5f84\u5206\u9694\u7b26\uff0c\u800c\u4e0d\u662f\u8f6c\u4e49\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">path = r&quot;C:\\Users\\Name\\Documents\\file.txt&quot;<\/p>\n<p>print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528raw\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728Windows\u5e73\u53f0\u4e0a\uff0c\u56e0\u4e3aWindows\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os.path\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os.path<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>os.path.join()<\/code>: \u7528\u4e8e\u8fde\u63a5\u8def\u5f84\u7247\u6bb5\u3002<\/li>\n<li><code>os.path.abspath()<\/code>: \u7528\u4e8e\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u3002<\/li>\n<li><code>os.path.basename()<\/code>: \u83b7\u53d6\u8def\u5f84\u7684\u6587\u4ef6\u540d\u90e8\u5206\u3002<\/li>\n<li><code>os.path.dirname()<\/code>: \u83b7\u53d6\u8def\u5f84\u7684\u76ee\u5f55\u90e8\u5206\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8fde\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>path = os.path.join(&quot;C:&quot;, &quot;Users&quot;, &quot;Name&quot;, &quot;Documents&quot;, &quot;file.txt&quot;)<\/p>\n<p>print(path)<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = os.path.abspath(&quot;file.txt&quot;)<\/p>\n<p>print(abs_path)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>basename = os.path.basename(path)<\/p>\n<p>print(basename)<\/p>\n<h2><strong>\u83b7\u53d6\u76ee\u5f55\u540d<\/strong><\/h2>\n<p>dirname = os.path.dirname(path)<\/p>\n<p>print(dirname)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u7684\u597d\u5904\u662f\u5b83\u80fd\u591f\u81ea\u52a8\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u5728Python 3.4\u4e2d\u5f15\u5165\uff0c\u4e3a\u8def\u5f84\u64cd\u4f5c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u66ff\u4ee3<code>os.path<\/code>\u6a21\u5757\uff0c\u63d0\u4f9b\u66f4\u76f4\u89c2\u548c\u6613\u7528\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>path = Path(&quot;C:\/Users\/Name\/Documents\/file.txt&quot;)<\/p>\n<p>print(path)<\/p>\n<h2><strong>\u8fde\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>new_path = path \/ &quot;new_folder&quot; \/ &quot;new_file.txt&quot;<\/p>\n<p>print(new_path)<\/p>\n<h2><strong>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = path.resolve()<\/p>\n<p>print(abs_path)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u76ee\u5f55\u540d<\/strong><\/h2>\n<p>basename = path.name<\/p>\n<p>dirname = path.parent<\/p>\n<p>print(basename)<\/p>\n<p>print(dirname)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u81ea\u7136\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8def\u5f84\uff0c\u4f7f\u7528\u659c\u6760<code>\/<\/code>\u8fd0\u7b97\u7b26\u6765\u7ec4\u5408\u8def\u5f84\u7247\u6bb5\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8def\u5f84\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u5dee\u5f02\u3002<code>os.path<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u5904\u7406\u8de8\u5e73\u53f0\u8def\u5f84\u7684\u5de5\u5177\u3002\u7279\u522b\u662f<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>os.sep<\/code>\u548c<code>os.altsep<\/code>\u53d8\u91cf\uff0c\u5206\u522b\u8868\u793a\u8def\u5f84\u5206\u9694\u7b26\u548c\u5907\u7528\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u7528\u4e8e\u7f16\u5199\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528os.sep<\/strong><\/h2>\n<p>path = &quot;C:{}Users{}Name{}Documents&quot;.format(os.sep, os.sep, os.sep)<\/p>\n<p>print(path)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u7684\u65b9\u5f0f<\/strong><\/h2>\n<p>path = Path(&quot;C:\/Users\/Name\/Documents&quot;)<\/p>\n<p>print(path.as_posix())  # \u8f6c\u6362\u4e3aPOSIX\u683c\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u7406\u89e3\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u533a\u522b\u975e\u5e38\u91cd\u8981\u3002\u7edd\u5bf9\u8def\u5f84\u662f\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><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>cwd = os.getcwd()<\/p>\n<p>print(&quot;Current working directory:&quot;, cwd)<\/p>\n<h2><strong>\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = os.path.abspath(&quot;file.txt&quot;)<\/p>\n<p>print(&quot;Absolute path:&quot;, abs_path)<\/p>\n<h2><strong>\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>rel_path = os.path.relpath(abs_path, cwd)<\/p>\n<p>print(&quot;Relative path:&quot;, rel_path)<\/p>\n<h2><strong>\u4f7f\u7528pathlib<\/strong><\/h2>\n<p>path = Path(&quot;file.txt&quot;)<\/p>\n<p>print(&quot;Absolute path with pathlib:&quot;, path.resolve())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7406\u89e3\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u4f7f\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u7f51\u7edc\u8def\u5f84\u548cUNC\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u7f51\u7edc\u8def\u5f84\u6216UNC\uff08\u901a\u7528\u547d\u540d\u7ea6\u5b9a\uff09\u8def\u5f84\u3002UNC\u8def\u5f84\u7528\u4e8e\u8bbf\u95ee\u7f51\u7edc\u4e0a\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u901a\u5e38\u4ee5\u53cc\u53cd\u659c\u6760\u5f00\u5934\u3002\u4f8b\u5982\uff1a<code>\\\\ServerName\\ShareName\\Path\\To\\File.txt<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaUNC\u8def\u5f84<\/strong><\/h2>\n<p>unc_path = Path(r&quot;\\\\ServerName\\ShareName\\Path\\To\\File.txt&quot;)<\/p>\n<p>print(unc_path)<\/p>\n<h2><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>exists = unc_path.exists()<\/p>\n<p>print(&quot;Path exists:&quot;, exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5904\u7406\u7f51\u7edc\u8def\u5f84\u65f6\uff0c\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u5e76\u4e14\u5177\u6709\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8def\u5f84\u7684\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u8def\u5f84\u7684\u5904\u7406\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u6216\u5199\u5165\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\uff0c\u5305\u62ec\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u548c<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528open()\u51fd\u6570<\/strong><\/h2>\n<p>with open(&quot;file.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u4f7f\u7528pathlib<\/strong><\/h2>\n<p>path = Path(&quot;file.txt&quot;)<\/p>\n<p>content = path.read_text()<\/p>\n<p>print(content)<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;new_file.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u5199\u5165<\/strong><\/h2>\n<p>new_path = Path(&quot;new_file.txt&quot;)<\/p>\n<p>new_path.write_text(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u8def\u5f84\u65f6\uff0c\u5173\u952e\u662f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002<strong>\u4f7f\u7528raw\u5b57\u7b26\u4e32\u3001os.path\u6a21\u5757\u548cpathlib\u6a21\u5757<\/strong>\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u4e0d\u540c\u5e73\u53f0\u7684\u8def\u5f84\u95ee\u9898\u3002\u7406\u89e3\u8def\u5f84\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u5982\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\uff0c\u4ee5\u53ca\u5982\u4f55\u64cd\u4f5c\u6587\u4ef6\uff0c\u90fd\u662f\u7f16\u5199\u5065\u58eePython\u4ee3\u7801\u7684\u91cd\u8981\u6280\u80fd\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\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\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u6587\u4ef6\u7684\u5b8c\u6574\u4f4d\u7f6e\uff0c\u5982<code>\/home\/user\/documents\/file.txt<\/code>\u3002\u76f8\u5bf9\u8def\u5f84\u901a\u5e38\u4e0d\u5305\u542b\u6839\u76ee\u5f55\u90e8\u5206\uff0c\u4f8b\u5982<code>documents\/file.txt<\/code>\uff0c\u8868\u793a\u6587\u4ef6\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684documents\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8def\u5f84\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u826f\u597d\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u5e93\u3002<code>os.path.join()<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4f7f\u8def\u5f84\u62fc\u63a5\u66f4\u53ef\u9760\u3002\u4f7f\u7528<code>pathlib.Path<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4ee5\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u5904\u7406\u8def\u5f84\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u8def\u5f84\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\uff0cPython\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u6587\u4ef6\u8def\u5f84\u4e2d\uff0c\u5982\u679c\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\uff0c\u5efa\u8bae\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5728\u5b57\u7b26\u4e32\u524d\u52a0r\uff09\u6765\u907f\u514d\u8f6c\u4e49\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>r&quot;C:\\Program Files\\MyApp&quot;<\/code>\u3002\u53e6\u5916\uff0c\u786e\u4fdd\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u56e0\u5b57\u7b26\u96c6\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u5bf9\u4e8e\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>urllib.parse.quote()<\/code>\u51fd\u6570\u5bf9\u8def\u5f84\u8fdb\u884c\u7f16\u7801\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5199\u8def\u5f84\u65f6\u9700\u8981\u6ce8\u610f\u51e0\u4e2a\u5173\u952e\u70b9\uff1a\u4f7f\u7528raw\u5b57\u7b26\u4e32\u9632\u6b62\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528os.path\u6a21\u5757\u786e\u4fdd\u8de8\u5e73\u53f0\u517c [&hellip;]","protected":false},"author":3,"featured_media":995274,"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\/995266"}],"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=995266"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995266\/revisions"}],"predecessor-version":[{"id":995275,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995266\/revisions\/995275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}