{"id":1036218,"date":"2024-12-31T12:05:09","date_gmt":"2024-12-31T04:05:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036218.html"},"modified":"2024-12-31T12:05:11","modified_gmt":"2024-12-31T04:05:11","slug":"python3%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e4%b8%ad%e6%96%87%e8%b7%af%e5%be%84%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1036218.html","title":{"rendered":"python3\u5982\u4f55\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2734332e-2e4a-4cfd-b10c-4f85737c78c0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python3\u5982\u4f55\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\" \/><\/p>\n<p><p> Python3\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c<strong>\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u3001\u5229\u7528os\u6a21\u5757\u3001\u901a\u8fc7open\u51fd\u6570\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f<\/strong>\uff0c\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f<\/strong>\u662f\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u56e0\u4e3a\u5982\u679c\u7f16\u7801\u65b9\u5f0f\u4e0d\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u6587\u4ef6\u65f6\u51fa\u73b0\u4e71\u7801\u6216\u65e0\u6cd5\u8bc6\u522b\u8def\u5f84\u3002Python3\u9ed8\u8ba4\u4f7f\u7528Unicode\u7f16\u7801\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\uff0c\u53ea\u9700\u8981\u5728\u5904\u7406\u6587\u672c\u5185\u5bb9\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f<\/h2>\n<\/p>\n<p><p>\u5728Python3\u4e2d\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aencoding\u53c2\u6570\u6765\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86encoding=&quot;utf-8&quot;\uff0c\u8fd9\u786e\u4fdd\u4e86\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u8fd9\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u6307\u5b9a\u7f16\u7801\uff0cPython\u5c06\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u4e2d\u6587\u6587\u4ef6\u5185\u5bb9\u65f6\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5229\u7528os\u6a21\u5757<\/h2>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u5185\u7f6e\u6a21\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u6765\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86os\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528open()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u91cc\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528os.path.join()\u65b9\u6cd5\u6765\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u8def\u5f84\u683c\u5f0f\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &quot;\u8def\u5f84\/\u5230&quot;<\/p>\n<p>file_name = &quot;\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>file_path = os.path.join(directory, file_name)<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528os.path.join()\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u90fd\u80fd\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7open\u51fd\u6570\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aopen()\u51fd\u6570\u7684encoding\u53c2\u6570\u6765\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;gbk&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86encoding=&quot;gbk&quot;\uff0c\u8fd9\u786e\u4fdd\u4e86\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\u4f7f\u7528GBK\u7f16\u7801\u3002GBK\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4e2d\u6587\u7f16\u7801\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u65e7\u7684\u4e2d\u6587\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e2d\u6587\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\uff0c\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u7684\u65b9\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>Windows\u64cd\u4f5c\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\uff0c\u53ea\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;C:\\\\\u8def\u5f84\\\\\u5230\\\\\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Windows\u98ce\u683c\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u6307\u5b9a\u4e86UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>Linux\u64cd\u4f5c\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\/\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Linux\u98ce\u683c\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u6307\u5b9a\u4e86UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u4e2d\u6587<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u65f6\uff0c\u4e0d\u4ec5\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u8fd8\u9700\u8981\u6b63\u786e\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u4e2d\u6587\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>with open(file_path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6570\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>content = &quot;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9\u3002&quot;<\/p>\n<p>with open(file_path, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5199\u5165\u4e86\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u4e2d\u6587\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u53bb\u9664\u6362\u884c\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528strip()\u65b9\u6cd5\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content = &quot;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9\u3002\\n&quot;<\/p>\n<p>cleaned_content = content.strip()<\/p>\n<p>print(cleaned_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528strip()\u65b9\u6cd5\u53bb\u9664\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u66ff\u6362\u5236\u8868\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528replace()\u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5236\u8868\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content = &quot;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9\u3002\\t&quot;<\/p>\n<p>cleaned_content = content.replace(&quot;\\t&quot;, &quot; &quot;)<\/p>\n<p>print(cleaned_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528replace()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5236\u8868\u7b26\u66ff\u6362\u4e3a\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u53cd\u659c\u6760\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u53bb\u9664\u8def\u5f84\u4e2d\u7684\u7a7a\u683c<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528strip()\u65b9\u6cd5\u53bb\u9664\u8def\u5f84\u4e2d\u7684\u7a7a\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot; \u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt &quot;<\/p>\n<p>cleaned_file_path = file_path.strip()<\/p>\n<p>print(cleaned_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528strip()\u65b9\u6cd5\u53bb\u9664\u4e86\u8def\u5f84\u4e2d\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u66ff\u6362\u8def\u5f84\u4e2d\u7684\u53cd\u659c\u6760<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u53cd\u659c\u6760\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528replace()\u65b9\u6cd5\u5c06\u53cd\u659c\u6760\u66ff\u6362\u4e3a\u6b63\u659c\u6760\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;\u8def\u5f84\\\\\u5230\\\\\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>cleaned_file_path = file_path.replace(&quot;\\\\&quot;, &quot;\/&quot;)<\/p>\n<p>print(cleaned_file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528replace()\u65b9\u6cd5\u5c06\u8def\u5f84\u4e2d\u7684\u53cd\u659c\u6760\u66ff\u6362\u4e3a\u6b63\u659c\u6760\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u73af\u5883\u53d8\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = os.path.expandvars(&quot;%USERPROFILE%\\\\\u8def\u5f84\\\\\u5230\\\\\u4e2d\u6587\u6587\u4ef6.txt&quot;)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.expandvars()\u65b9\u6cd5\u5c06\u8def\u5f84\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.abspath()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>relative_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.abspath()\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.relpath()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>absolute_path = &quot;C:\/\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>relative_path = os.path.relpath(absolute_path)<\/p>\n<p>print(relative_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.relpath()\u65b9\u6cd5\u5c06\u7edd\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u6587\u4ef6\u540d<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.basename()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p>print(file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.basename()\u65b9\u6cd5\u83b7\u53d6\u4e86\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.splitext()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\u8def\u5f84\/\u5230\/\u4e2d\u6587\u6587\u4ef6.txt&quot;<\/p>\n<p>file_name, file_extension = os.path.splitext(file_path)<\/p>\n<p>print(file_extension)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.path.splitext()\u65b9\u6cd5\u83b7\u53d6\u4e86\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u4e00\u4e0b\uff0cPython3\u4e2d\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f<strong>\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f<\/strong>\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5229\u7528os\u6a21\u5757\u3001\u901a\u8fc7open\u51fd\u6570\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\u7b49\u65b9\u6cd5\u6765\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002\u5728\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u548c\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u3001\u73af\u5883\u53d8\u91cf\u3001\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u3001\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7b49\u4fe1\u606f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u6b63\u786e\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u5904\u7406\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u540d\u7684\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u7684\u7f16\u7801\u901a\u5e38\u4f7f\u7528UTF-8\u3002\u4e3a\u4e86\u786e\u4fdd\u6b63\u786e\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\uff0c\u5e76\u786e\u4fdd\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\u6307\u5b9a\u7f16\u7801\u4e3a<code>&#39;utf-8&#39;<\/code>\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u3002<\/p>\n<p><strong>\u5728Windows\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u4e2d\u6587\u8def\u5f84\u7684\u5dee\u5f02\uff1f<\/strong><br \/>Windows\u548cLinux\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\u7684\u7f16\u7801\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002Windows\u901a\u5e38\u4f7f\u7528GBK\u7f16\u7801\uff0c\u800cLinux\u5219\u4f7f\u7528UTF-8\u3002\u4e3a\u4e86\u5728\u4e0d\u540c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path.join()<\/code>\u548c<code>pathlib<\/code>\u6a21\u5757\u6765\u6784\u5efa\u8def\u5f84\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u62fc\u63a5\u5e26\u6765\u7684\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u679c\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u65f6\u51fa\u73b0\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u9519\u8bef\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u51fa\u73b0\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u9519\u8bef\u53ef\u80fd\u662f\u56e0\u4e3a\u8def\u5f84\u4e0d\u6b63\u786e\u6216\u7f16\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6392\u67e5\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u8def\u5f84\u7684\u62fc\u5199\u548c\u683c\u5f0f\u6b63\u786e\u3002\u5176\u6b21\uff0c\u4f7f\u7528<code>os.listdir()<\/code>\u67e5\u770b\u76ee\u6807\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u540d\uff0c\u786e\u8ba4\u4e2d\u6587\u6587\u4ef6\u540d\u662f\u5426\u6b63\u786e\u663e\u793a\u3002\u5982\u679c\u4ecd\u7136\u65e0\u6cd5\u89e3\u51b3\uff0c\u5c1d\u8bd5\u4f7f\u7528<code>os.chdir()<\/code>\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f7f\u5176\u4e0e\u4e2d\u6587\u8def\u5f84\u4e00\u81f4\uff0c\u7136\u540e\u518d\u5c1d\u8bd5\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3\u8bfb\u53d6\u4e2d\u6587\u8def\u5f84\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u3001\u5229\u7528os\u6a21\u5757\u3001\u901a\u8fc7open\u51fd\u6570\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1036224,"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\/1036218"}],"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=1036218"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036218\/revisions"}],"predecessor-version":[{"id":1036228,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036218\/revisions\/1036228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}