{"id":1149834,"date":"2025-01-13T16:54:18","date_gmt":"2025-01-13T08:54:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149834.html"},"modified":"2025-01-13T16:54:20","modified_gmt":"2025-01-13T08:54:20","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149834.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8c03\u7528\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180438\/ce73d45f-cecb-4f63-b1fe-ed126b588f86.webp\" alt=\"python\u4e2d\u5982\u4f55\u8c03\u7528\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5916\u90e8\u5e93\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u8def\u5f84\u6a21\u5757os\u548cpathlib\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c<strong>open()\u51fd\u6570\u662f\u6700\u57fa\u7840\u4e14\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u53ef\u4ee5\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528open()\u51fd\u6570\u6765\u8c03\u7528\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528open()\u51fd\u6570\uff1a<\/strong><\/p>\n<\/p>\n<p><p>open()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4e3b\u8981\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u3002\u5b83\u6709\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f\u53ef\u4ee5\u662f&#39;r&#39;\uff08\u8bfb\u53d6\uff09\u3001&#39;w&#39;\uff08\u5199\u5165\uff09\u3001&#39;a&#39;\uff08\u8ffd\u52a0\uff09\u7b49\u3002\u4f7f\u7528open()\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u6216\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code># \u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528open()\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408with\u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u4ece\u800c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u548c\u6587\u4ef6\u9501\u5b9a\u95ee\u9898\u3002\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4f8b\u5982read()\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3001readline()\u6309\u884c\u8bfb\u53d6\u3001readlines()\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a sample text.&#39;)<\/p>\n<h2><strong>\u8ffd\u52a0\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an appended text.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u8c03\u7528\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>open()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f\u53ef\u4ee5\u662f&#39;r&#39;\uff08\u8bfb\u53d6\uff09\u3001&#39;w&#39;\uff08\u5199\u5165\uff09\u3001&#39;a&#39;\uff08\u8ffd\u52a0\uff09\u3001&#39;b&#39;\uff08\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/strong><\/h2>\n<p>file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8ffd\u52a0\u5199\u5165<\/strong><\/h2>\n<p>file = open(&#39;example.txt&#39;, &#39;a&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u4e8c\u8fdb\u5236\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6<\/strong><\/h2>\n<p>file = open(&#39;example.bin&#39;, &#39;rb&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528read()\u3001readline()\u548creadlines()\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u6309\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<h2><strong>\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    for line in lines:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a sample text.&#39;)<\/p>\n<h2><strong>\u8ffd\u52a0\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an appended text.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5173\u95ed\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u4f1a\u5728\u6587\u4ef6\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u4f46\u663e\u5f0f\u5730\u5173\u95ed\u6587\u4ef6\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u5f0f\u5173\u95ed\u6587\u4ef6<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>content = file.read()<\/p>\n<p>file.close()<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u548cpathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u548cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u8def\u5f84\u5904\u7406\u3001\u6587\u4ef6\u91cd\u547d\u540d\u3001\u6587\u4ef6\u5220\u9664\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os\u6a21\u5757<\/h4>\n<\/p>\n<p><p>os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(&#39;example.txt&#39;):<\/p>\n<p>    print(&#39;File exists&#39;)<\/p>\n<h2><strong>\u91cd\u547d\u540d\u6587\u4ef6<\/strong><\/h2>\n<p>os.rename(&#39;example.txt&#39;, &#39;new_example.txt&#39;)<\/p>\n<h2><strong>\u5220\u9664\u6587\u4ef6<\/strong><\/h2>\n<p>os.remove(&#39;new_example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u6a21\u5757<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\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>    print(&#39;File exists&#39;)<\/p>\n<h2><strong>\u91cd\u547d\u540d\u6587\u4ef6<\/strong><\/h2>\n<p>file_path.rename(&#39;new_example.txt&#39;)<\/p>\n<h2><strong>\u5220\u9664\u6587\u4ef6<\/strong><\/h2>\n<p>file_path.unlink()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e38\u7528\u4e8e\u8bfb\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u6587\u4ef6\uff0c\u5982CSV\u3001Excel\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;example.csv&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;example.xlsx&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_json(&#39;example.json&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u5916\u90e8\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86pandas\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5916\u90e8\u5e93\u53ef\u4ee5\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982openpyxl\u3001csv\u3001json\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528openpyxl\u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = load_workbook(&#39;example.xlsx&#39;)<\/p>\n<p>sheet = workbook.active<\/p>\n<p>for row in sheet.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528csv\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528json\u6a21\u5757\u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6587\u4ef6\u8def\u5f84\u6a21\u5757\u64cd\u4f5c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u8c03\u7528\u6587\u4ef6\u5916\uff0c\u6587\u4ef6\u8def\u5f84\u6a21\u5757\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os.path\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/h4>\n<\/p>\n<p><p>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u529f\u80fd\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;example.txt&#39;)<\/p>\n<p>print(abs_path)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u76ee\u5f55\u540d<\/strong><\/h2>\n<p>dir_name = os.path.dirname(&#39;example.txt&#39;)<\/p>\n<p>print(dir_name)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u57fa\u540d<\/strong><\/h2>\n<p>base_name = os.path.basename(&#39;example.txt&#39;)<\/p>\n<p>print(base_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\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>\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>abs_path = file_path.resolve()<\/p>\n<p>print(abs_path)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u76ee\u5f55\u540d<\/strong><\/h2>\n<p>dir_name = file_path.parent<\/p>\n<p>print(dir_name)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u57fa\u540d<\/strong><\/h2>\n<p>base_name = file_path.name<\/p>\n<p>print(base_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(b&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u6216\u5206\u5757\u8bfb\u53d6\u7684\u65b9\u5f0f\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u5757\u8bfb\u53d6\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5206\u5757\u8bfb\u53d6\u5927\u6587\u4ef6<\/p>\n<p>chunk_size = 1024  # \u6bcf\u6b21\u8bfb\u53d61KB<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(chunk_size)<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        process(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6587\u4ef6\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>1. \u6355\u83b7\u6587\u4ef6\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;File not found&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;Permission denied&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class FileError(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se FileError(&#39;File not found&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    raise FileError(&#39;Permission denied&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528open()\u51fd\u6570\u3001os\u548cpathlib\u6a21\u5757\u3001pandas\u5e93\u4ee5\u53ca\u5176\u4ed6\u5916\u90e8\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\u9700\u6c42\u3002\u65e0\u8bba\u662f\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u8fd8\u662f\u64cd\u4f5c\u5927\u6587\u4ef6\u548c\u5904\u7406\u6587\u4ef6\u5f02\u5e38\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u64cd\u4f5c\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u4e2d\u8c03\u7528\u6587\u4ef6\u7684\u65b9\u6cd5\u7075\u6d3b\u591a\u6837\uff0c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u4efb\u52a1\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\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\u4ef6\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u8bfb\u53d6\uff09\uff0c\u4f60\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u5e76\u4f7f\u7528<code>read()<\/code>, <code>readline()<\/code>\u6216<code>readlines()<\/code>\u7b49\u65b9\u6cd5\u83b7\u53d6\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:<\/code>\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\uff1f<\/strong><br \/>Python\u7684<code>open()<\/code>\u51fd\u6570\u652f\u6301\u591a\u79cd\u6a21\u5f0f\uff0c\u5305\u62ec&#39;r&#39;\uff08\u53ea\u8bfb\uff09\u3001&#39;w&#39;\uff08\u5199\u5165\uff0c\u8986\u76d6\u6587\u4ef6\uff09\u3001&#39;a&#39;\uff08\u8ffd\u52a0\u5185\u5bb9\uff09\u3001&#39;b&#39;\uff08\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u548c&#39;t&#39;\uff08\u6587\u672c\u6a21\u5f0f\uff09\u3002\u9009\u62e9\u5408\u9002\u7684\u6a21\u5f0f\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u6765\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5199\u5165\u6587\u4ef6\u540c\u6837\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u6307\u5b9a&#39;w&#39;\u6216&#39;a&#39;\u6a21\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u6587\u4ef6\u6216\u8ffd\u52a0\u5185\u5bb9\u5230\u73b0\u6709\u6587\u4ef6\u3002\u4f7f\u7528<code>file.write()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u4e2d\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u79cd\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u672a\u627e\u5230\u6216\u6743\u9650\u4e0d\u8db3\u3002\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002\u4f8b\u5982\uff0c<code>try: with open(&#39;filename.txt&#39;, &#39;r&#39;) as file: ... except FileNotFoundError: print(&#39;\u6587\u4ef6\u672a\u627e\u5230&#39;)<\/code>\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5916\u90e8\u5e93\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u8def [&hellip;]","protected":false},"author":3,"featured_media":1149837,"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\/1149834"}],"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=1149834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149834\/revisions"}],"predecessor-version":[{"id":1149838,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149834\/revisions\/1149838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149837"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}