{"id":995727,"date":"2024-12-27T09:07:20","date_gmt":"2024-12-27T01:07:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995727.html"},"modified":"2024-12-27T09:07:23","modified_gmt":"2024-12-27T01:07:23","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/995727.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072221\/754d6e2b-2e8e-4565-8e87-5503e7e64331.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528Pathlib\u5e93\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>open()\u51fd\u6570\u662f\u6700\u57fa\u7840\u7684\uff0c\u7528\u4e8e\u76f4\u63a5\u6253\u5f00\u6587\u4ef6\uff1bos\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u8def\u5f84\u3001\u6587\u4ef6\u7b49\uff1bPathlib\u662fPython 3.4\u5f15\u5165\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u3002<\/strong>\u5728\u8fd9\u4e4b\u4e2d\uff0cPathlib\u56e0\u4e3a\u5176\u9762\u5411\u5bf9\u8c61\u7684\u8bbe\u8ba1\u800c\u9010\u6e10\u6210\u4e3a\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u4e3b\u6d41\u9009\u62e9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OPEN()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u4f7f\u7528open()\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6253\u5f00\u548c\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u6216\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528open()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528open()\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u53ea\u8bfb\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528read()\u3001readline()\u6216readlines()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OS\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u51fd\u6570\u3002\u901a\u8fc7os\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u76ee\u5f55<\/strong>\uff1a\u4f7f\u7528os.getcwd()\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u8def\u5f84<\/strong>\uff1a\u4f7f\u7528os.path.join()\u6765\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8def\u5f84\u683c\u5f0f\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u5b58\u5728<\/strong>\uff1a\u4f7f\u7528os.path.exists()\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_directory, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;File does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pathlib\u5e93\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84<\/p>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u5b83\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528Path\u7c7b\u521b\u5efa\u4e00\u4e2a\u8def\u5f84\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u5b58\u5728<\/strong>\uff1a\u4f7f\u7528Path\u5bf9\u8c61\u7684exists()\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528read_text()\u6216open()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong><\/h2>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if file_path.exists():<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(content)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;File does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6587\u4ef6\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u7406\u89e3\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84\u7684\u6982\u5ff5\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7edd\u5bf9\u8def\u5f84<\/strong>\uff1a\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5b83\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6709\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u4f8b\u5982\u5728Windows\u4e2d\u4ee5\u201cC:\\\u201d\u5f00\u5934\uff0c\u800c\u5728Unix\/Linux\u4e2d\u4ee5\u201c\/\u201d\u5f00\u5934\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f8\u5bf9\u8def\u5f84<\/strong>\uff1a\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u79fb\u690d\u6027\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7684\u6587\u4ef6\u7cfb\u7edf\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528os.path.abspath()\u6765\u83b7\u53d6\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>relative_path = &#39;example.txt&#39;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;Absolute Path:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u6709\u65f6\u4f1a\u9047\u5230\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u53cd\u659c\u6760\u7b49\uff09\u3002\u8fd9\u4e9b\u5b57\u7b26\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\uff0c\u56e0\u6b64\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u53ef\u4ee5\u907f\u514d\u8f6c\u4e49\u5b57\u7b26\u7684\u95ee\u9898\u3002\u539f\u59cb\u5b57\u7b26\u4e32\u4ee5\u201cr\u201d\u5f00\u5934\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528os.path.join()<\/strong>\uff1a\u907f\u514d\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u5b57\u7b26\u4e32\uff0c\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u5904\u7406\u8def\u5f84\u4e2d\u7684\u53cd\u659c\u6760<\/strong><\/h2>\n<p>file_path = r&#39;C:\\path\\to\\file.txt&#39;<\/p>\n<h2><strong>\u4f7f\u7528os.path.join()\u6784\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;C:\\\\path\\\\to&#39;<\/p>\n<p>file_name = &#39;file.txt&#39;<\/p>\n<p>file_path = os.path.join(directory, file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8003\u8651\u5185\u5b58\u4f7f\u7528\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5927\u6587\u4ef6\uff0c\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9010\u884c\u8bfb\u53d6<\/strong>\uff1a\u4f7f\u7528\u5faa\u73af\u914d\u5408readline()\u6216\u9010\u884c\u8fed\u4ee3\u6765\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d41\u5f0f\u5904\u7406<\/strong>\uff1a\u5728\u8bfb\u53d6\u6587\u4ef6\u7684\u540c\u65f6\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6<\/p>\n<p>file_path = &#39;large_file.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)  # \u5047\u8bbeprocess\u662f\u5bf9\u6bcf\u884c\u6570\u636e\u7684\u5904\u7406\u51fd\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u786c\u7f16\u7801\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4f7f\u7528os\u6a21\u5757\u6216Pathlib\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u8def\u5f84\u5dee\u5f02\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528os.path.join()\u6216Pathlib<\/strong>\uff1a\u786e\u4fdd\u8def\u5f84\u62fc\u63a5\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528\u786c\u7f16\u7801\u7684\u8def\u5f84\u5206\u9694\u7b26<\/strong>\uff1a\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8def\u5f84\u5206\u9694\u7b26\u4e0d\u540c\u3002\u5728Windows\u4e2d\u662f\u201c\\\u201d\uff0c\u800c\u5728Unix\/Linux\u4e2d\u662f\u201c\/\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528os.path.join()\u6784\u5efa\u8de8\u5e73\u53f0\u8def\u5f84<\/strong><\/h2>\n<p>directory = os.path.expanduser(&#39;~&#39;)  # \u83b7\u53d6\u7528\u6237\u4e3b\u76ee\u5f55<\/p>\n<p>file_name = &#39;example.txt&#39;<\/p>\n<p>file_path = os.path.join(directory, file_name)<\/p>\n<h2><strong>\u4f7f\u7528Pathlib\u6784\u5efa\u8de8\u5e73\u53f0\u8def\u5f84<\/strong><\/h2>\n<p>file_path = Path.home() \/ &#39;example.txt&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u5305\u62ecopen()\u51fd\u6570\u3001os\u6a21\u5757\u548cPathlib\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>open()\u51fd\u6570<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u64cd\u4f5c\u3002<\/li>\n<li><strong>os\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u8def\u5f84\u6216\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u7684\u573a\u666f\u3002<\/li>\n<li><strong>Pathlib\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u63a8\u8350\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\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\u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u6b63\u786e\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u6587\u4ef6\u8def\u5f84.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u6253\u5f00\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u5e76\u6253\u5370\u5176\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u6216\u7a7a\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u4e2d\u5982\u679c\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\uff0c\u901a\u5e38\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5728\u5b57\u7b26\u4e32\u524d\u52a0<code>r<\/code>\uff09\u6765\u907f\u514d\u8f6c\u4e49\u95ee\u9898\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">file_path = r&#39;C:\\Users\\Name\\My Documents\\\u6587\u4ef6.txt&#39;\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u6216\u5355\u5f15\u53f7\u90fd\u53ef\u4ee5\u6709\u6548\u907f\u514d\u95ee\u9898\u3002\u5728Windows\u73af\u5883\u4e0b\uff0c\u4f7f\u7528\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\u65f6\u4e5f\u8981\u6ce8\u610f\u8f6c\u4e49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>exists()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;\u6587\u4ef6\u8def\u5f84.txt&#39;\nif os.path.exists(file_path):\n    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5c1d\u8bd5\u8bfb\u53d6\u6587\u4ef6\u4e4b\u524d\u786e\u8ba4\u6587\u4ef6\u7684\u5b58\u5728\u6027\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528Pathlib\u5e93\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":995733,"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\/995727"}],"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=995727"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995727\/revisions"}],"predecessor-version":[{"id":995740,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995727\/revisions\/995740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995733"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}