{"id":1165100,"date":"2025-01-15T15:16:08","date_gmt":"2025-01-15T07:16:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165100.html"},"modified":"2025-01-15T15:16:11","modified_gmt":"2025-01-15T07:16:11","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%b0%83%e7%94%a8%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165100.html","title":{"rendered":"\u5982\u4f55\u7528python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205531\/1e0050eb-02c1-4790-98fd-5e1bba79fc06.webp\" alt=\"\u5982\u4f55\u7528python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>pandas<\/code>\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\u3001\u4f7f\u7528<code>os<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5f0f\uff08\u5982\u8bfb\u53d6\u6a21\u5f0f<code>r<\/code>\u3001\u5199\u5165\u6a21\u5f0f<code>w<\/code>\u7b49\uff09\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    content = file.read()<\/p>\n<p>    # \u6253\u5370\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/code> \u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a <code>example.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u8bfb\u53d6\u5230\u53d8\u91cf <code>content<\/code> \u4e2d\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002\u4f7f\u7528 <code>with<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>1.1 \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>read()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u5185\u5bb9\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#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><h3>1.2 \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>readlines()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u8fd4\u56de\u3002\u6bcf\u4e00\u884c\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><h3>1.3 \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff08\u8fed\u4ee3\u5668\u65b9\u5f0f\uff09<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6587\u4ef6\u5bf9\u8c61\u8fdb\u884c\u8fed\u4ee3\uff0c\u6bcf\u6b21\u8bfb\u53d6\u4e00\u884c\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u8bfb\u5165\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>2.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u6587\u4ef6\u3002\u4f7f\u7528 <code>read_csv()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\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><h3>2.2 \u8bfb\u53d6Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>read_excel()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;example.xlsx&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 \u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>read_json()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_json(&#39;example.json&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>os<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>3.1 \u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>os.listdir()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>files = os.listdir(&#39;.&#39;)<\/p>\n<p>print(files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>os.path.exists()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if os.path.exists(&#39;example.txt&#39;):<\/p>\n<p>    print(&#39;File exists&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File does not exist&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.3 \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>os.path.getsize()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_size = os.path.getsize(&#39;example.txt&#39;)<\/p>\n<p>print(f&#39;File size: {file_size} bytes&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>pathlib<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>4.1 \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code> \u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528 <code>Path<\/code> \u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>content = file_path.read_text()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>write_text()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>file_path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3 \u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>glob()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>files = Path(&#39;.&#39;).glob(&#39;*.txt&#39;)<\/p>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.4 \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>exists()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>if file_path.exists():<\/p>\n<p>    print(&#39;File exists&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File does not exist&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528<code>csv<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>5.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>csv<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u8bfb\u5199CSV\u6587\u4ef6\u3002\u4f7f\u7528 <code>csv.reader()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\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><h3>5.2 \u5199\u5165CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>csv.writer()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5411CSV\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;]<\/p>\n<p>]<\/p>\n<p>with open(&#39;example.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528<code>json<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>6.1 \u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>json<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406JSON\u6570\u636e\u3002\u4f7f\u7528 <code>json.load()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6JSON\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\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>6.2 \u5199\u5165JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>json.dump()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5411JSON\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 30,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;<\/p>\n<p>}<\/p>\n<p>with open(&#39;example.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528<code>pickle<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>7.1 \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pickle<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316Python\u5bf9\u8c61\u3002\u4f7f\u7528 <code>pickle.load()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>with open(&#39;example.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    data = pickle.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2 \u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>pickle.dump()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5411\u4e8c\u8fdb\u5236\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 30,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;<\/p>\n<p>}<\/p>\n<p>with open(&#39;example.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(data, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528<code>io<\/code>\u5e93\u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>8.1 \u8bfb\u53d6\u5185\u5b58\u4e2d\u7684\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>io<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u64cd\u4f5c\u5185\u5b58\u4e2d\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u4f7f\u7528 <code>io.StringIO<\/code> \u53ef\u4ee5\u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>file = io.StringIO(&#39;Hello, World!&#39;)<\/p>\n<p>content = file.read()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.2 \u5199\u5165\u5185\u5b58\u4e2d\u7684\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>io.StringIO<\/code> \u53ef\u4ee5\u5411\u5185\u5b58\u4e2d\u7684\u6587\u672c\u6587\u4ef6\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>file = io.StringIO()<\/p>\n<p>file.write(&#39;Hello, World!&#39;)<\/p>\n<p>content = file.getvalue()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.3 \u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>io.BytesIO<\/code> \u53ef\u4ee5\u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>file = io.BytesIO(b&#39;Hello, World!&#39;)<\/p>\n<p>content = file.read()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528<code>shutil<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>9.1 \u590d\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>shutil<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528 <code>shutil.copy()<\/code> \u53ef\u4ee5\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>shutil.copy(&#39;example.txt&#39;, &#39;copy_example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.2 \u79fb\u52a8\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>shutil.move()<\/code> \u53ef\u4ee5\u79fb\u52a8\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>shutil.move(&#39;example.txt&#39;, &#39;moved_example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.3 \u5220\u9664\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>os.remove()<\/code> \u53ef\u4ee5\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.remove(&#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>10.1 \u5206\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_in_chunks(file_object, chunk_size=1024):<\/p>\n<p>    while True:<\/p>\n<p>        data = file_object.read(chunk_size)<\/p>\n<p>        if not data:<\/p>\n<p>            break<\/p>\n<p>        yield data<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for chunk in read_in_chunks(file):<\/p>\n<p>        print(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>10.2 \u4f7f\u7528\u8fed\u4ee3\u5668\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u7279\u6027\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>10.3 \u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u7684 <code>chunksize<\/code> \u53c2\u6570\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>chunksize = 1000<\/p>\n<p>for chunk in pd.read_csv(&#39;large_file.csv&#39;, chunksize=chunksize):<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u591a\u79cd\u4f7f\u7528Python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>pandas<\/code>\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\u3001\u4f7f\u7528<code>os<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6\u3001\u4f7f\u7528<code>pathlib<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6\u3001\u4f7f\u7528<code>csv<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3001\u4f7f\u7528<code>json<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6\u3001\u4f7f\u7528<code>pickle<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\u3001\u4f7f\u7528<code>io<\/code>\u5e93\u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u6587\u4ef6\u3001\u4f7f\u7528<code>shutil<\/code>\u5e93\u64cd\u4f5c\u6587\u4ef6\u4ee5\u53ca\u5904\u7406\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6587\u4ef6\u7c7b\u578b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u4f8b\u5982&#39;r&#39;\u8868\u793a\u53ea\u8bfb\u6a21\u5f0f\uff09\uff0c\u60a8\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5168\u90e8\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1f<\/strong><br \/>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528<code>readline()<\/code>\u6216\u76f4\u63a5\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u904d\u5386\u6587\u4ef6\u5bf9\u8c61\u3002\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())  # strip()\u7528\u4e8e\u53bb\u6389\u884c\u672b\u7684\u6362\u884c\u7b26\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u60a8\u53ef\u4ee5\u9010\u884c\u5904\u7406\u6570\u636e\uff0c\u4fbf\u4e8e\u6570\u636e\u5206\u6790\u6216\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef\u975e\u5e38\u91cd\u8981\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u53ef\u80fd\u4e0d\u5b58\u5728\u6216\u8def\u5f84\u9519\u8bef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;yourfile.txt&#39;, &#39;r&#39;) as file:\n        content = file.read()\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u3002&quot;)\nexcept IOError:\n    print(&quot;\u8bfb\u53d6\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9 \u4f7f\u7528Python\u8c03\u7528\u6587\u4ef6\u5185\u5bb9\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u901a\u8fc7pand [&hellip;]","protected":false},"author":3,"featured_media":1165106,"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\/1165100"}],"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=1165100"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165100\/revisions"}],"predecessor-version":[{"id":1165107,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165100\/revisions\/1165107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165106"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}