{"id":10021228,"date":"2026-02-01T02:40:01","date_gmt":"2026-01-31T18:40:01","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021228"},"modified":"2026-02-01T02:40:01","modified_gmt":"2026-01-31T18:40:01","slug":"python%e4%b8%ad%e7%9a%84%e6%96%87%e4%bb%b6i-o%e6%93%8d%e4%bd%9c%e4%b8%8e%e7%bc%93%e5%86%b2%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021228","title":{"rendered":"Python\u4e2d\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0e\u7f13\u51b2\u7b56\u7565"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0e\u7f13\u51b2\u7b56\u7565<\/h2>\n<h3>1. \u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6587\u4ef6I\/O\uff08\u8f93\u5165\/\u8f93\u51fa\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u7406\u89e3\u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5\u662f\u638c\u63e1Python\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u7840\u3002<\/p>\n<h4>1.1 \u6587\u4ef6\u7684\u5b9a\u4e49<\/h4>\n<p>\u6587\u4ef6\u662f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5b58\u50a8\u4ecb\u8d28\u4e0a\u7684\u4e00\u7ec4\u76f8\u5173\u6570\u636e\u7684\u96c6\u5408\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u5f81\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u540d<\/strong>\uff1a\u7528\u4e8e\u6807\u8bc6\u6587\u4ef6\u7684\u540d\u79f0<\/li>\n<li><strong>\u8def\u5f84<\/strong>\uff1a\u6587\u4ef6\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4f4d\u7f6e<\/li>\n<li><strong>\u5185\u5bb9<\/strong>\uff1a\u6587\u4ef6\u4e2d\u5b58\u50a8\u7684\u6570\u636e<\/li>\n<li><strong>\u5c5e\u6027<\/strong>\uff1a\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u5982\u5927\u5c0f\u3001\u521b\u5efa\u65f6\u95f4\u3001\u4fee\u6539\u65f6\u95f4\u7b49<\/li>\n<\/ul>\n<h4>1.2 \u6587\u4ef6\u7684\u7c7b\u578b<\/h4>\n<p>\u6839\u636e\u6587\u4ef6\u7684\u5185\u5bb9\u548c\u7f16\u7801\u65b9\u5f0f\uff0c\u6587\u4ef6\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u672c\u6587\u4ef6<\/strong>\uff1a\u5b58\u50a8\u6587\u672c\u6570\u636e\uff0c\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\uff08\u5982UTF-8\u3001ASCII\u7b49\uff09<\/li>\n<li><strong>\u4e8c\u8fdb\u5236\u6587\u4ef6<\/strong>\uff1a\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5982\u56fe\u7247\u3001\u97f3\u9891\u3001\u89c6\u9891\u7b49<\/li>\n<li><strong>\u7279\u6b8a\u6587\u4ef6<\/strong>\uff1a\u5982\u8bbe\u5907\u6587\u4ef6\u3001\u7ba1\u9053\u6587\u4ef6\u7b49<\/li>\n<\/ul>\n<h4>1.3 \u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u672c\u6a21\u5f0f<\/h4>\n<p>Python\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u652f\u6301\u4ee5\u4e0b\u57fa\u672c\u6a21\u5f0f\uff1a<\/p>\n<ul>\n<li><strong>\u8bfb\u6a21\u5f0f<\/strong>\uff08<code>r<\/code>\uff09\uff1a\u53ea\u8bfb\u6a21\u5f0f\uff0c\u6253\u5f00\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6<\/li>\n<li><strong>\u5199\u6a21\u5f0f<\/strong>\uff08<code>w<\/code>\uff09\uff1a\u5199\u5165\u6a21\u5f0f\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u6216\u622a\u65ad\u73b0\u6709\u6587\u4ef6<\/li>\n<li><strong>\u8ffd\u52a0\u6a21\u5f0f<\/strong>\uff08<code>a<\/code>\uff09\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff0c\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u6570\u636e<\/li>\n<li><strong>\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/strong>\uff08<code>b<\/code>\uff09\uff1a\u4e0e\u4e0a\u8ff0\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u4e8c\u8fdb\u5236\u65b9\u5f0f\u64cd\u4f5c\u6587\u4ef6<\/li>\n<li><strong>\u8bfb\u5199\u6a21\u5f0f<\/strong>\uff08<code>+<\/code>\uff09\uff1a\u4e0e\u4e0a\u8ff0\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u540c\u65f6\u652f\u6301\u8bfb\u5199\u64cd\u4f5c<\/li>\n<\/ul>\n<h4>1.4 \u6587\u4ef6\u5bf9\u8c61<\/h4>\n<p>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u901a\u8fc7\u6587\u4ef6\u5bf9\u8c61\u6765\u5b9e\u73b0\u7684\u3002\u6587\u4ef6\u5bf9\u8c61\u662f\u7531<code>open()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"python\"># \u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5\u793a\u4f8b\n\n# 1. \u6253\u5f00\u6587\u4ef6\nprint(&quot;\u6253\u5f00\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\n# \u6587\u672c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\ntry:\n    # \u8bfb\u6a21\u5f0f\n    f = open(&quot;test.txt&quot;, &quot;r&quot;)\n    print(&quot;\u6210\u529f\u6253\u5f00\u6587\u4ef6\uff08\u8bfb\u6a21\u5f0f\uff09&quot;)\n    f.close()\n    \n    # \u5199\u6a21\u5f0f\n    f = open(&quot;test.txt&quot;, &quot;w&quot;)\n    print(&quot;\u6210\u529f\u6253\u5f00\u6587\u4ef6\uff08\u5199\u6a21\u5f0f\uff09&quot;)\n    f.close()\n    \n    # \u8ffd\u52a0\u6a21\u5f0f\n    f = open(&quot;test.txt&quot;, &quot;a&quot;)\n    print(&quot;\u6210\u529f\u6253\u5f00\u6587\u4ef6\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09&quot;)\n    f.close()\n    \n    # \u4e8c\u8fdb\u5236\u6a21\u5f0f\n    f = open(&quot;test.bin&quot;, &quot;wb&quot;)\n    print(&quot;\u6210\u529f\u6253\u5f00\u6587\u4ef6\uff08\u4e8c\u8fdb\u5236\u5199\u6a21\u5f0f\uff09&quot;)\n    f.close()\n    \n    # \u8bfb\u5199\u6a21\u5f0f\n    f = open(&quot;test.txt&quot;, &quot;r+&quot;)\n    print(&quot;\u6210\u529f\u6253\u5f00\u6587\u4ef6\uff08\u8bfb\u5199\u6a21\u5f0f\uff09&quot;)\n    f.close()\nexcept Exception as e:\n    print(f&quot;\u6253\u5f00\u6587\u4ef6\u5931\u8d25: {e}&quot;)\n\n# 2. \u6587\u4ef6\u5bf9\u8c61\u7684\u5c5e\u6027\nprint(&quot;\/n\u6587\u4ef6\u5bf9\u8c61\u7684\u5c5e\u6027\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    f = open(&quot;test.txt&quot;, &quot;w+&quot;)\n    print(f&quot;\u6587\u4ef6\u540d: {f.name}&quot;)\n    print(f&quot;\u6a21\u5f0f: {f.mode}&quot;)\n    print(f&quot;\u662f\u5426\u5173\u95ed: {f.closed}&quot;)\n    print(f&quot;\u7f16\u7801: {f.encoding}&quot;)\n    print(f&quot;\u6362\u884c\u7b26: {f.newlines}&quot;)\n    print(f&quot;\u662f\u5426\u53ef\u8bfb\u5199: {f.readable()}, {f.writable()}&quot;)\n    f.close()\nexcept Exception as e:\n    print(f&quot;\u64cd\u4f5c\u5931\u8d25: {e}&quot;)\n\n# 3. \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nprint(&quot;\/n\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u793a\u4f8b\uff1a&quot;)\n\n# \u4f7f\u7528with\u8bed\u53e5\uff08\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff09\u6253\u5f00\u6587\u4ef6\nwith open(&quot;test.txt&quot;, &quot;w&quot;) as f:\n    f.write(&quot;Hello, World!\/n&quot;)\n    print(&quot;\u5199\u5165\u6570\u636e&quot;)\nprint(&quot;\u6587\u4ef6\u5df2\u81ea\u52a8\u5173\u95ed&quot;)\n\n# 4. \u67e5\u770b\u6587\u4ef6\u5185\u5bb9\nwith open(&quot;test.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# 5. \u5220\u9664\u6d4b\u8bd5\u6587\u4ef6\nimport os\nif os.path.exists(&quot;test.txt&quot;):\n    os.remove(&quot;test.txt&quot;)\n    print(&quot;\u5220\u9664test.txt\u6587\u4ef6&quot;)\nif os.path.exists(&quot;test.bin&quot;):\n    os.remove(&quot;test.bin&quot;)\n    print(&quot;\u5220\u9664test.bin\u6587\u4ef6&quot;)<\/code><\/pre>\n<h3>2. \u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u57fa\u672c\u65b9\u6cd5<\/h3>\n<p>Python\u7684\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u662f\u638c\u63e1Python\u6587\u4ef6\u64cd\u4f5c\u7684\u5173\u952e\u3002<\/p>\n<h4>2.1 \u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u6cd5<\/h4>\n<p>\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong><code>read(size=-1)<\/code><\/strong>\uff1a\u8bfb\u53d6\u6307\u5b9a\u5927\u5c0f\u7684\u5b57\u8282\u6216\u5b57\u7b26\uff0c\u9ed8\u8ba4\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6<\/li>\n<li><strong><code>readline(size=-1)<\/code><\/strong>\uff1a\u8bfb\u53d6\u4e00\u884c\u6570\u636e\uff0c\u9ed8\u8ba4\u8bfb\u53d6\u6574\u4e2a\u884c<\/li>\n<li><strong><code>readlines(hint=-1)<\/code><\/strong>\uff1a\u8bfb\u53d6\u6240\u6709\u884c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u9ed8\u8ba4\u8bfb\u53d6\u6240\u6709\u884c<\/li>\n<li><strong><code>__iter__()<\/code><\/strong>\uff1a\u652f\u6301\u8fed\u4ee3\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u6587\u4ef6\u7684\u6bcf\u4e00\u884c<\/li>\n<\/ul>\n<h4>2.2 \u5199\u5165\u6587\u4ef6\u7684\u65b9\u6cd5<\/h4>\n<p>\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5199\u5165\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong><code>write(string)<\/code><\/strong>\uff1a\u5199\u5165\u5b57\u7b26\u4e32\u6216\u5b57\u8282\u4e32<\/li>\n<li><strong><code>writelines(lines)<\/code><\/strong>\uff1a\u5199\u5165\u591a\u884c\u6570\u636e<\/li>\n<li><strong><code>flush()<\/code><\/strong>\uff1a\u5237\u65b0\u7f13\u51b2\u533a\uff0c\u5c06\u6570\u636e\u7acb\u5373\u5199\u5165\u6587\u4ef6<\/li>\n<li><strong><code>close()<\/code><\/strong>\uff1a\u5173\u95ed\u6587\u4ef6\uff0c\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<\/ul>\n<h4>2.3 \u6587\u4ef6\u6307\u9488\u64cd\u4f5c\u7684\u65b9\u6cd5<\/h4>\n<p>\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u6587\u4ef6\u6307\u9488\u64cd\u4f5c\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong><code>tell()<\/code><\/strong>\uff1a\u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e<\/li>\n<li>\n<p><strong><code>seek(offset, whence=0)<\/code><\/strong>\uff1a\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6307\u5b9a\u4f4d\u7f6e<\/p>\n<ul>\n<li><code>offset<\/code>\uff1a\u504f\u79fb\u91cf<\/li>\n<li><code>whence<\/code>\uff1a\u53c2\u8003\u4f4d\u7f6e\uff080\uff1a\u6587\u4ef6\u5f00\u5934\uff0c1\uff1a\u5f53\u524d\u4f4d\u7f6e\uff0c2\uff1a\u6587\u4ef6\u672b\u5c3e\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2.4 \u6587\u4ef6\u64cd\u4f5c\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u57fa\u672c\u65b9\u6cd5\u793a\u4f8b\n\n# 1. \u5199\u5165\u6587\u4ef6\nprint(&quot;\u5199\u5165\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\n# \u5199\u5165\u6587\u672c\u6587\u4ef6\nwith open(&quot;example.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:\n    f.write(&quot;Hello, World!\/n&quot;)\n    f.write(&quot;Python\u6587\u4ef6\u64cd\u4f5c\u793a\u4f8b\/n&quot;)\n    f.write(&quot;\u8fd9\u662f\u7b2c\u4e09\u884c\/n&quot;)\nprint(&quot;\u5199\u5165\u6587\u672c\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\nwith open(&quot;example.bin&quot;, &quot;wb&quot;) as f:\n    f.write(b&quot;Hello, Binary!\/n&quot;)\n    f.write(b&quot;Python\u4e8c\u8fdb\u5236\u6587\u4ef6\u64cd\u4f5c\u793a\u4f8b\/n&quot;)\nprint(&quot;\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\u6210\u529f&quot;)\n\n# 2. \u8bfb\u53d6\u6587\u4ef6\nprint(&quot;\/n\u8bfb\u53d6\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\n# \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read()\n    print(&quot;\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff1a&quot;)\n    print(content)\n\n# \u8bfb\u53d6\u6307\u5b9a\u5927\u5c0f\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read(10)\n    print(&quot;\/n\u8bfb\u53d6\u524d10\u4e2a\u5b57\u7b26\uff1a&quot;)\n    print(content)\n\n# \u9010\u884c\u8bfb\u53d6\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    print(&quot;\/n\u9010\u884c\u8bfb\u53d6\uff1a&quot;)\n    line1 = f.readline()\n    line2 = f.readline()\n    print(f&quot;\u7b2c\u4e00\u884c: {line1.rstrip()}&quot;)\n    print(f&quot;\u7b2c\u4e8c\u884c: {line2.rstrip()}&quot;)\n\n# \u8bfb\u53d6\u6240\u6709\u884c\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    lines = f.readlines()\n    print(&quot;\/n\u8bfb\u53d6\u6240\u6709\u884c\uff1a&quot;)\n    for i, line in enumerate(lines):\n        print(f&quot;\u7b2c{i+1}\u884c: {line.rstrip()}&quot;)\n\n# \u4f7f\u7528for\u5faa\u73af\u904d\u5386\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    print(&quot;\/n\u4f7f\u7528for\u5faa\u73af\u904d\u5386\uff1a&quot;)\n    for i, line in enumerate(f):\n        print(f&quot;\u7b2c{i+1}\u884c: {line.rstrip()}&quot;)\n\n# \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\nwith open(&quot;example.bin&quot;, &quot;rb&quot;) as f:\n    content = f.read()\n    print(&quot;\/n\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a&quot;)\n    print(content)\n\n# 3. \u6587\u4ef6\u6307\u9488\u64cd\u4f5c\nprint(&quot;\/n\u6587\u4ef6\u6307\u9488\u64cd\u4f5c\u793a\u4f8b\uff1a&quot;)\n\nwith open(&quot;example.txt&quot;, &quot;r+&quot;, encoding=&quot;utf-8&quot;) as f:\n    # \u67e5\u770b\u521d\u59cb\u4f4d\u7f6e\n    print(f&quot;\u521d\u59cb\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e: {f.tell()}&quot;)\n    \n    # \u8bfb\u53d6\u4e00\u4e9b\u6570\u636e\n    content = f.read(10)\n    print(f&quot;\u8bfb\u53d6\u7684\u5185\u5bb9: {content}&quot;)\n    print(f&quot;\u8bfb\u53d6\u540e\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e: {f.tell()}&quot;)\n    \n    # \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u5f00\u5934\n    f.seek(0)\n    print(f&quot;\u79fb\u52a8\u5230\u6587\u4ef6\u5f00\u5934\u540e\u6307\u9488\u4f4d\u7f6e: {f.tell()}&quot;)\n    \n    # \u8bfb\u53d6\u7b2c\u4e00\u884c\n    line = f.readline()\n    print(f&quot;\u7b2c\u4e00\u884c\u5185\u5bb9: {line.rstrip()}&quot;)\n    \n    # \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u672b\u5c3e\n    f.seek(0, 2)\n    print(f&quot;\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e\u540e\u6307\u9488\u4f4d\u7f6e: {f.tell()}&quot;)\n    \n    # \u5728\u6587\u4ef6\u672b\u5c3e\u5199\u5165\u6570\u636e\n    f.write(&quot;\u8fd9\u662f\u8ffd\u52a0\u7684\u5185\u5bb9\/n&quot;)\n    print(&quot;\u5728\u6587\u4ef6\u672b\u5c3e\u5199\u5165\u6570\u636e&quot;)\n\n# \u67e5\u770b\u4fee\u6539\u540e\u7684\u6587\u4ef6\u5185\u5bb9\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read()\n    print(&quot;\/n\u4fee\u6539\u540e\u7684\u6587\u4ef6\u5185\u5bb9\uff1a&quot;)\n    print(content)\n\n# 4. \u8ffd\u52a0\u5185\u5bb9\nprint(&quot;\/n\u8ffd\u52a0\u5185\u5bb9\u793a\u4f8b\uff1a&quot;)\n\nwith open(&quot;example.txt&quot;, &quot;a&quot;, encoding=&quot;utf-8&quot;) as f:\n    f.write(&quot;\u8fd9\u662f\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6dfb\u52a0\u7684\u5185\u5bb9\/n&quot;)\nprint(&quot;\u8ffd\u52a0\u5185\u5bb9\u6210\u529f&quot;)\n\n# \u67e5\u770b\u8ffd\u52a0\u540e\u7684\u6587\u4ef6\u5185\u5bb9\nwith open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read()\n    print(&quot;\/n\u8ffd\u52a0\u540e\u7684\u6587\u4ef6\u5185\u5bb9\uff1a&quot;)\n    print(content)\n\n# 5. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\nimport os\nif os.path.exists(&quot;example.txt&quot;):\n    os.remove(&quot;example.txt&quot;)\n    print(&quot;\u5220\u9664example.txt\u6587\u4ef6&quot;)\nif os.path.exists(&quot;example.bin&quot;):\n    os.remove(&quot;example.bin&quot;)\n    print(&quot;\u5220\u9664example.bin\u6587\u4ef6&quot;)<\/code><\/pre>\n<h3>3. \u7f13\u51b2\u7b56\u7565<\/h3>\n<p>\u7f13\u51b2\u662f\u6587\u4ef6I\/O\u64cd\u4f5c\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u64cd\u4f5c\u7684\u6027\u80fd\u3002\u7406\u89e3\u7f13\u51b2\u7b56\u7565\u5bf9\u4e8e\u4f18\u5316\u6587\u4ef6I\/O\u64cd\u4f5c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>3.1 \u7f13\u51b2\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<p>\u7f13\u51b2\u662f\u6307\u5728\u5185\u5b58\u4e2d\u4e34\u65f6\u5b58\u50a8\u6570\u636e\uff0c\u7136\u540e\u6279\u91cf\u5199\u5165\u6216\u8bfb\u53d6\u6587\u4ef6\u7684\u8fc7\u7a0b\u3002\u7f13\u51b2\u7684\u4e3b\u8981\u76ee\u7684\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd<\/strong>\uff1a\u51cf\u5c11\u78c1\u76d8I\/O\u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u56e0\u4e3a\u5185\u5b58\u64cd\u4f5c\u6bd4\u78c1\u76d8\u64cd\u4f5c\u5feb\u5f97\u591a<\/li>\n<li><strong>\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528<\/strong>\uff1a\u7cfb\u7edf\u8c03\u7528\u7684\u5f00\u9500\u8f83\u5927\uff0c\u7f13\u51b2\u53ef\u4ee5\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570<\/li>\n<li><strong>\u63d0\u9ad8\u53ef\u9760\u6027<\/strong>\uff1a\u5728\u610f\u5916\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f13\u51b2\u6062\u590d\u6570\u636e<\/li>\n<\/ul>\n<h4>3.2 Python\u4e2d\u7684\u7f13\u51b2\u6a21\u5f0f<\/h4>\n<p>Python\u4e2d\u7684\u6587\u4ef6\u5bf9\u8c61\u652f\u6301\u4ee5\u4e0b\u7f13\u51b2\u6a21\u5f0f\uff1a<\/p>\n<ul>\n<li><strong>\u65e0\u7f13\u51b2<\/strong>\uff08<code>0<\/code>\uff09\uff1a\u4e0d\u4f7f\u7528\u7f13\u51b2\uff0c\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u4f1a\u76f4\u63a5\u64cd\u4f5c\u78c1\u76d8<\/li>\n<li><strong>\u884c\u7f13\u51b2<\/strong>\uff08<code>1<\/code>\uff09\uff1a\u6309\u884c\u7f13\u51b2\uff0c\u5f53\u9047\u5230\u6362\u884c\u7b26\u65f6\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<li><strong>\u5757\u7f13\u51b2<\/strong>\uff08<code>&gt;1<\/code>\uff09\uff1a\u6309\u5757\u7f13\u51b2\uff0c\u5f53\u7f13\u51b2\u533a\u6ee1\u65f6\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<li>\n<p><strong>\u9ed8\u8ba4\u7f13\u51b2<\/strong>\uff1a\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u548c\u64cd\u4f5c\u6a21\u5f0f\u81ea\u52a8\u9009\u62e9\u7f13\u51b2\u6a21\u5f0f<\/p>\n<ul>\n<li>\u6587\u672c\u6587\u4ef6\uff1a\u9ed8\u8ba4\u4f7f\u7528\u884c\u7f13\u51b2<\/li>\n<li>\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a\u9ed8\u8ba4\u4f7f\u7528\u5757\u7f13\u51b2<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>3.3 \u7f13\u51b2\u533a\u7684\u5927\u5c0f<\/h4>\n<p>\u7f13\u51b2\u533a\u7684\u5927\u5c0f\u4f1a\u5f71\u54cd\u6587\u4ef6\u64cd\u4f5c\u7684\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u8f83\u5c0f\u7684\u7f13\u51b2\u533a<\/strong>\uff1a\u5185\u5b58\u4f7f\u7528\u8f83\u5c11\uff0c\u4f46\u53ef\u80fd\u4f1a\u589e\u52a0\u78c1\u76d8I\/O\u64cd\u4f5c\u7684\u6b21\u6570<\/li>\n<li><strong>\u8f83\u5927\u7684\u7f13\u51b2\u533a<\/strong>\uff1a\u53ef\u4ee5\u51cf\u5c11\u78c1\u76d8I\/O\u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u4f46\u4f1a\u589e\u52a0\u5185\u5b58\u4f7f\u7528<\/li>\n<\/ul>\n<h4>3.4 \u7f13\u51b2\u7684\u63a7\u5236<\/h4>\n<p>Python\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u6765\u63a7\u5236\u7f13\u51b2\uff1a<\/p>\n<ul>\n<li><strong><code>flush()<\/code><\/strong>\uff1a\u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a\uff0c\u5c06\u6570\u636e\u5199\u5165\u78c1\u76d8<\/li>\n<li><strong><code>close()<\/code><\/strong>\uff1a\u5173\u95ed\u6587\u4ef6\u65f6\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<li><strong><code>with<\/code>\u8bed\u53e5<\/strong>\uff1a\u9000\u51fa\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u65f6\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u4ece\u800c\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<\/ul>\n<h4>3.5 \u7f13\u51b2\u7b56\u7565\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u7f13\u51b2\u7b56\u7565\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u7f13\u51b2\u7b56\u7565\u793a\u4f8b\n\nimport os\nimport time\n\n# 1. \u7f13\u51b2\u6a21\u5f0f\u793a\u4f8b\nprint(&quot;\u7f13\u51b2\u6a21\u5f0f\u793a\u4f8b\uff1a&quot;)\n\n# \u65e0\u7f13\u51b2\nprint(&quot;\/n\u65e0\u7f13\u51b2\u6a21\u5f0f\uff1a&quot;)\ntry:\n    # \u6ce8\u610f\uff1a\u5728\u6587\u672c\u6a21\u5f0f\u4e0b\uff0c\u7f13\u51b2\u6a21\u5f0f0\u53ef\u80fd\u4e0d\u652f\u6301\n    f = open(&quot;buffer_test.txt&quot;, &quot;wb&quot;, buffering=0)\n    print(f&quot;\u6253\u5f00\u6587\u4ef6\u6210\u529f\uff0c\u7f13\u51b2\u6a21\u5f0f: \u65e0\u7f13\u51b2&quot;)\n    f.write(b&quot;Hello, Buffer!\/n&quot;)\n    f.close()\nexcept Exception as e:\n    print(f&quot;\u64cd\u4f5c\u5931\u8d25: {e}&quot;)\n\n# \u884c\u7f13\u51b2\nprint(&quot;\/n\u884c\u7f13\u51b2\u6a21\u5f0f\uff1a&quot;)\ntry:\n    f = open(&quot;buffer_test.txt&quot;, &quot;w&quot;, buffering=1)\n    print(f&quot;\u6253\u5f00\u6587\u4ef6\u6210\u529f\uff0c\u7f13\u51b2\u6a21\u5f0f: \u884c\u7f13\u51b2&quot;)\n    f.write(&quot;Hello, Line Buffer!\/n&quot;)\n    # \u5199\u5165\u6362\u884c\u7b26\u540e\u4f1a\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a\n    print(&quot;\u5199\u5165\u6362\u884c\u7b26\u540e\uff0c\u7f13\u51b2\u533a\u5df2\u5237\u65b0&quot;)\n    f.close()\nexcept Exception as e:\n    print(f&quot;\u64cd\u4f5c\u5931\u8d25: {e}&quot;)\n\n# \u5757\u7f13\u51b2\nprint(&quot;\/n\u5757\u7f13\u51b2\u6a21\u5f0f\uff1a&quot;)\ntry:\n    f = open(&quot;buffer_test.txt&quot;, &quot;w&quot;, buffering=1024)\n    print(f&quot;\u6253\u5f00\u6587\u4ef6\u6210\u529f\uff0c\u7f13\u51b2\u6a21\u5f0f: \u5757\u7f13\u51b2\uff0c\u7f13\u51b2\u533a\u5927\u5c0f: 1024&quot;)\n    f.write(&quot;Hello, Block Buffer!\/n&quot;)\n    print(&quot;\u5199\u5165\u6570\u636e\u540e\uff0c\u7f13\u51b2\u533a\u672a\u5237\u65b0\uff08\u6570\u636e\u91cf\u5c0f\uff09&quot;)\n    f.flush()\n    print(&quot;\u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a&quot;)\n    f.close()\nexcept Exception as e:\n    print(f&quot;\u64cd\u4f5c\u5931\u8d25: {e}&quot;)\n\n# 2. \u7f13\u51b2\u6027\u80fd\u6d4b\u8bd5\nprint(&quot;\/n\u7f13\u51b2\u6027\u80fd\u6d4b\u8bd5\uff1a&quot;)\n\ndef test_write_performance(buffering):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u5199\u5165\u6027\u80fd&quot;&quot;&quot;\n    start_time = time.time()\n    with open(&quot;performance_test.txt&quot;, &quot;w&quot;, buffering=buffering) as f:\n        for i in range(10000):\n            f.write(f&quot;Line {i}: This is a test line.\/n&quot;)\n    end_time = time.time()\n    return end_time - start_time\n\n# \u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u6a21\u5f0f\u7684\u6027\u80fd\nprint(&quot;\u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u6a21\u5f0f\u7684\u5199\u5165\u6027\u80fd\uff1a&quot;)\n\n# \u65e0\u7f13\u51b2\uff08\u5982\u679c\u652f\u6301\uff09\ntry:\n    time_no_buffer = test_write_performance(0)\n    print(f&quot;\u65e0\u7f13\u51b2\u6a21\u5f0f\u8017\u65f6: {time_no_buffer:.4f}\u79d2&quot;)\nexcept Exception as e:\n    print(f&quot;\u65e0\u7f13\u51b2\u6a21\u5f0f\u6d4b\u8bd5\u5931\u8d25: {e}&quot;)\n\n# \u884c\u7f13\u51b2\ntime_line_buffer = test_write_performance(1)\nprint(f&quot;\u884c\u7f13\u51b2\u6a21\u5f0f\u8017\u65f6: {time_line_buffer:.4f}\u79d2&quot;)\n\n# \u5757\u7f13\u51b2\uff081024\u5b57\u8282\uff09\ntime_block_buffer_1k = test_write_performance(1024)\nprint(f&quot;1024\u5b57\u8282\u5757\u7f13\u51b2\u6a21\u5f0f\u8017\u65f6: {time_block_buffer_1k:.4f}\u79d2&quot;)\n\n# \u5757\u7f13\u51b2\uff084096\u5b57\u8282\uff09\ntime_block_buffer_4k = test_write_performance(4096)\nprint(f&quot;4096\u5b57\u8282\u5757\u7f13\u51b2\u6a21\u5f0f\u8017\u65f6: {time_block_buffer_4k:.4f}\u79d2&quot;)\n\n# \u5757\u7f13\u51b2\uff088192\u5b57\u8282\uff09\ntime_block_buffer_8k = test_write_performance(8192)\nprint(f&quot;8192\u5b57\u8282\u5757\u7f13\u51b2\u6a21\u5f0f\u8017\u65f6: {time_block_buffer_8k:.4f}\u79d2&quot;)\n\n# 3. \u7f13\u51b2\u533a\u5237\u65b0\u793a\u4f8b\nprint(&quot;\/n\u7f13\u51b2\u533a\u5237\u65b0\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\u6d4b\u8bd5\u7f13\u51b2\u533a\u5237\u65b0\u884c\u4e3a\uff1a&quot;)\nwith open(&quot;flush_test.txt&quot;, &quot;w&quot;) as f:\n    print(&quot;\u5199\u5165\u7b2c\u4e00\u884c\u6570\u636e...&quot;)\n    f.write(&quot;\u7b2c\u4e00\u884c\u6570\u636e\/n&quot;)\n    # \u5199\u5165\u6362\u884c\u7b26\u540e\uff0c\u884c\u7f13\u51b2\u4f1a\u81ea\u52a8\u5237\u65b0\n    print(&quot;\u5199\u5165\u7b2c\u4e00\u884c\u540e\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6709\u5185\u5bb9...&quot;)\n    \n    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    with open(&quot;flush_test.txt&quot;, &quot;r&quot;) as f_read:\n        content = f_read.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9: '{content}'&quot;)\n    \n    print(&quot;\/n\u5199\u5165\u7b2c\u4e8c\u884c\u6570\u636e\uff08\u4e0d\u5305\u542b\u6362\u884c\u7b26\uff09...&quot;)\n    f.write(&quot;\u7b2c\u4e8c\u884c\u6570\u636e&quot;)\n    # \u6ca1\u6709\u6362\u884c\u7b26\uff0c\u884c\u7f13\u51b2\u4e0d\u4f1a\u81ea\u52a8\u5237\u65b0\n    print(&quot;\u5199\u5165\u7b2c\u4e8c\u884c\u540e\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6709\u5185\u5bb9...&quot;)\n    \n    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    with open(&quot;flush_test.txt&quot;, &quot;r&quot;) as f_read:\n        content = f_read.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9: '{content}'&quot;)\n    \n    print(&quot;\/n\u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a...&quot;)\n    f.flush()\n    print(&quot;\u5237\u65b0\u540e\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6709\u5185\u5bb9...&quot;)\n    \n    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    with open(&quot;flush_test.txt&quot;, &quot;r&quot;) as f_read:\n        content = f_read.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9: '{content}'&quot;)\n\nprint(&quot;\/n\u9000\u51fawith\u8bed\u53e5\u540e\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5e76\u5237\u65b0\u7f13\u51b2\u533a&quot;)\n\n# \u68c0\u67e5\u6587\u4ef6\u6700\u7ec8\u5185\u5bb9\nwith open(&quot;flush_test.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()\n    print(f&quot;\u6587\u4ef6\u6700\u7ec8\u5185\u5bb9: '{content}'&quot;)\n\n# 4. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\uff1a&quot;)\n\nfor file in [&quot;buffer_test.txt&quot;, &quot;performance_test.txt&quot;, &quot;flush_test.txt&quot;]:\n    if os.path.exists(file):\n        os.remove(file)\n        print(f&quot;\u5220\u9664{file}\u6587\u4ef6&quot;)<\/code><\/pre>\n<h3>4. \u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c<\/h3>\n<p>\u9664\u4e86\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<h4>4.1 os\u6a21\u5757<\/h4>\n<p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684<code>os<\/code>\u6a21\u5757\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\n<p><strong><code>os.path<\/code><\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u8def\u5f84\u76f8\u5173\u7684\u64cd\u4f5c<\/p>\n<ul>\n<li><code>os.path.exists(path)<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/li>\n<li><code>os.path.isfile(path)<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u6587\u4ef6<\/li>\n<li><code>os.path.isdir(path)<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u76ee\u5f55<\/li>\n<li><code>os.path.join(path1, path2, ...)<\/code>\uff1a\u8fde\u63a5\u591a\u4e2a\u8def\u5f84<\/li>\n<li><code>os.path.abspath(path)<\/code>\uff1a\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/li>\n<li><code>os.path.basename(path)<\/code>\uff1a\u83b7\u53d6\u6587\u4ef6\u540d<\/li>\n<li><code>os.path.dirname(path)<\/code>\uff1a\u83b7\u53d6\u76ee\u5f55\u540d<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>os.remove(path)<\/code>\uff1a\u5220\u9664\u6587\u4ef6<\/li>\n<li><code>os.rename(src, dst)<\/code>\uff1a\u91cd\u547d\u540d\u6587\u4ef6\u6216\u76ee\u5f55<\/li>\n<li><code>os.replace(src, dst)<\/code>\uff1a\u66ff\u6362\u6587\u4ef6<\/li>\n<li><code>os.chmod(path, mode)<\/code>\uff1a\u4fee\u6539\u6587\u4ef6\u6743\u9650<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u76ee\u5f55\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>os.mkdir(path)<\/code>\uff1a\u521b\u5efa\u76ee\u5f55<\/li>\n<li><code>os.makedirs(path)<\/code>\uff1a\u9012\u5f52\u521b\u5efa\u76ee\u5f55<\/li>\n<li><code>os.rmdir(path)<\/code>\uff1a\u5220\u9664\u76ee\u5f55<\/li>\n<li><code>os.removedirs(path)<\/code>\uff1a\u9012\u5f52\u5220\u9664\u76ee\u5f55<\/li>\n<li><code>os.listdir(path)<\/code>\uff1a\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.2 shutil\u6a21\u5757<\/h4>\n<p><code>shutil<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u590d\u5236\u3001\u79fb\u52a8\u3001\u5f52\u6863\u7b49\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u6587\u4ef6\u590d\u5236<\/strong>\uff1a<\/p>\n<ul>\n<li><code>shutil.copy(src, dst)<\/code>\uff1a\u590d\u5236\u6587\u4ef6<\/li>\n<li><code>shutil.copy2(src, dst)<\/code>\uff1a\u590d\u5236\u6587\u4ef6\u548c\u5143\u6570\u636e<\/li>\n<li><code>shutil.copyfile(src, dst)<\/code>\uff1a\u590d\u5236\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u76ee\u5f55\u590d\u5236<\/strong>\uff1a<\/p>\n<ul>\n<li><code>shutil.copytree(src, dst)<\/code>\uff1a\u9012\u5f52\u590d\u5236\u76ee\u5f55<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u548c\u76ee\u5f55\u79fb\u52a8<\/strong>\uff1a<\/p>\n<ul>\n<li><code>shutil.move(src, dst)<\/code>\uff1a\u79fb\u52a8\u6587\u4ef6\u6216\u76ee\u5f55<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u5220\u9664<\/strong>\uff1a<\/p>\n<ul>\n<li><code>shutil.rmtree(path)<\/code>\uff1a\u9012\u5f52\u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5f52\u6863\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>shutil.make_archive(base_name, format, root_dir)<\/code>\uff1a\u521b\u5efa\u5f52\u6863\u6587\u4ef6<\/li>\n<li><code>shutil.unpack_archive(filename, extract_dir)<\/code>\uff1a\u89e3\u538b\u5f52\u6863\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.3 pathlib\u6a21\u5757<\/h4>\n<p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4+\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u63a5\u53e3\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u8def\u5f84\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<ul>\n<li><code>Path(path)<\/code>\uff1a\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/li>\n<li><code>PurePath(path)<\/code>\uff1a\u521b\u5efa\u7eaf\u8def\u5f84\u5bf9\u8c61\uff08\u4e0d\u6d89\u53ca\u5b9e\u9645\u6587\u4ef6\u7cfb\u7edf\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8def\u5f84\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path.exists()<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/li>\n<li><code>path.is_file()<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u6587\u4ef6<\/li>\n<li><code>path.is_dir()<\/code>\uff1a\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u76ee\u5f55<\/li>\n<li><code>path.iterdir()<\/code>\uff1a\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/li>\n<li><code>path.glob(pattern)<\/code>\uff1a\u67e5\u627e\u5339\u914d\u6a21\u5f0f\u7684\u6587\u4ef6<\/li>\n<li><code>path.rglob(pattern)<\/code>\uff1a\u9012\u5f52\u67e5\u627e\u5339\u914d\u6a21\u5f0f\u7684\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path.read_text(encoding=None)<\/code>\uff1a\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/li>\n<li><code>path.write_text(data, encoding=None)<\/code>\uff1a\u5199\u5165\u6587\u672c\u6587\u4ef6<\/li>\n<li><code>path.read_bytes()<\/code>\uff1a\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/li>\n<li><code>path.write_bytes(data)<\/code>\uff1a\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/li>\n<li><code>path.unlink()<\/code>\uff1a\u5220\u9664\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u76ee\u5f55\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path.mkdir(exist_ok=False)<\/code>\uff1a\u521b\u5efa\u76ee\u5f55<\/li>\n<li><code>path.rmdir()<\/code>\uff1a\u5220\u9664\u76ee\u5f55<\/li>\n<li><code>path.mkdir(parents=True, exist_ok=False)<\/code>\uff1a\u9012\u5f52\u521b\u5efa\u76ee\u5f55<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.4 \u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u793a\u4f8b\n\nimport os\nimport shutil\nfrom pathlib import Path\n\n# 1. os\u6a21\u5757\u793a\u4f8b\nprint(&quot;os\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\n# \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\nfile_path = &quot;test_file.txt&quot;\nprint(f&quot;\/n\u68c0\u67e5\u6587\u4ef6 {file_path} \u662f\u5426\u5b58\u5728\uff1a&quot;)\nif os.path.exists(file_path):\n    print(f&quot;\u6587\u4ef6 {file_path} \u5b58\u5728&quot;)\nelse:\n    print(f&quot;\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728&quot;)\n    # \u521b\u5efa\u6587\u4ef6\n    with open(file_path, &quot;w&quot;) as f:\n        f.write(&quot;\u6d4b\u8bd5\u6587\u4ef6\u5185\u5bb9\/n&quot;)\n    print(f&quot;\u521b\u5efa\u6587\u4ef6 {file_path} \u6210\u529f&quot;)\n\n# \u68c0\u67e5\u8def\u5f84\u7c7b\u578b\nprint(f&quot;\/n\u68c0\u67e5\u8def\u5f84 {file_path} \u7684\u7c7b\u578b\uff1a&quot;)\nprint(f&quot;\u662f\u5426\u662f\u6587\u4ef6: {os.path.isfile(file_path)}&quot;)\nprint(f&quot;\u662f\u5426\u662f\u76ee\u5f55: {os.path.isdir(file_path)}&quot;)\n\n# \u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\nprint(f&quot;\/n\u6587\u4ef6 {file_path} \u7684\u4fe1\u606f\uff1a&quot;)\nprint(f&quot;\u7edd\u5bf9\u8def\u5f84: {os.path.abspath(file_path)}&quot;)\nprint(f&quot;\u6587\u4ef6\u540d: {os.path.basename(file_path)}&quot;)\nprint(f&quot;\u76ee\u5f55\u540d: {os.path.dirname(file_path)}&quot;)\nprint(f&quot;\u6587\u4ef6\u5927\u5c0f: {os.path.getsize(file_path)} \u5b57\u8282&quot;)\nprint(f&quot;\u521b\u5efa\u65f6\u95f4: {os.path.getctime(file_path)}&quot;)\nprint(f&quot;\u4fee\u6539\u65f6\u95f4: {os.path.getmtime(file_path)}&quot;)\n\n# \u76ee\u5f55\u64cd\u4f5c\nprint(&quot;\/n\u76ee\u5f55\u64cd\u4f5c\uff1a&quot;)\ndir_path = &quot;test_dir&quot;\nprint(f&quot;\u68c0\u67e5\u76ee\u5f55 {dir_path} \u662f\u5426\u5b58\u5728\uff1a&quot;)\nif not os.path.exists(dir_path):\n    os.mkdir(dir_path)\n    print(f&quot;\u521b\u5efa\u76ee\u5f55 {dir_path} \u6210\u529f&quot;)\nelse:\n    print(f&quot;\u76ee\u5f55 {dir_path} \u5df2\u5b58\u5728&quot;)\n\n# \u5217\u51fa\u76ee\u5f55\u5185\u5bb9\nprint(f&quot;\/n\u76ee\u5f55 {dir_path} \u4e2d\u7684\u5185\u5bb9\uff1a&quot;)\nif os.path.exists(dir_path):\n    contents = os.listdir(dir_path)\n    print(f&quot;\u76ee\u5f55\u5185\u5bb9: {contents}&quot;)\n\n# 2. shutil\u6a21\u5757\u793a\u4f8b\nprint(&quot;\/nshutil\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\n# \u590d\u5236\u6587\u4ef6\nsrc_file = &quot;test_file.txt&quot;\ndst_file = os.path.join(dir_path, &quot;copied_file.txt&quot;)\nprint(f&quot;\/n\u590d\u5236\u6587\u4ef6 {src_file} \u5230 {dst_file}\uff1a&quot;)\nif os.path.exists(src_file):\n    shutil.copy(src_file, dst_file)\n    print(&quot;\u590d\u5236\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u68c0\u67e5\u590d\u5236\u7684\u6587\u4ef6\nprint(f&quot;\u68c0\u67e5\u590d\u5236\u7684\u6587\u4ef6 {dst_file}\uff1a&quot;)\nif os.path.exists(dst_file):\n    with open(dst_file, &quot;r&quot;) as f:\n        content = f.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# 3. pathlib\u6a21\u5757\u793a\u4f8b\nprint(&quot;\/npathlib\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\n# \u521b\u5efaPath\u5bf9\u8c61\npath = Path(&quot;test_file.txt&quot;)\nprint(f&quot;\/nPath\u5bf9\u8c61\u64cd\u4f5c\uff1a&quot;)\nprint(f&quot;\u8def\u5f84: {path}&quot;)\nprint(f&quot;\u7edd\u5bf9\u8def\u5f84: {path.absolute()}&quot;)\nprint(f&quot;\u662f\u5426\u5b58\u5728: {path.exists()}&quot;)\nprint(f&quot;\u662f\u5426\u662f\u6587\u4ef6: {path.is_file()}&quot;)\nprint(f&quot;\u662f\u5426\u662f\u76ee\u5f55: {path.is_dir()}&quot;)\nprint(f&quot;\u6587\u4ef6\u540d: {path.name}&quot;)\nprint(f&quot;\u540e\u7f00: {path.suffix}&quot;)\nprint(f&quot;stem: {path.stem}&quot;)\nprint(f&quot;\u7236\u76ee\u5f55: {path.parent}&quot;)\n\n# \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\nprint(&quot;\/n\u4f7f\u7528Path\u5bf9\u8c61\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a&quot;)\nif path.exists() and path.is_file():\n    content = path.read_text()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# \u5199\u5165\u6587\u4ef6\u5185\u5bb9\nprint(&quot;\/n\u4f7f\u7528Path\u5bf9\u8c61\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff1a&quot;)\nnew_path = Path(dir_path) \/ &quot;new_file.txt&quot;\nnew_path.write_text(&quot;\u4f7f\u7528Path\u5bf9\u8c61\u5199\u5165\u7684\u5185\u5bb9\/n&quot;)\nprint(f&quot;\u5199\u5165\u6587\u4ef6 {new_path} \u6210\u529f&quot;)\n\n# \u68c0\u67e5\u5199\u5165\u7684\u6587\u4ef6\nif new_path.exists():\n    content = new_path.read_text()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# \u904d\u5386\u76ee\u5f55\nprint(&quot;\/n\u4f7f\u7528Path\u5bf9\u8c61\u904d\u5386\u76ee\u5f55\uff1a&quot;)\ndir_path_obj = Path(dir_path)\nif dir_path_obj.exists() and dir_path_obj.is_dir():\n    print(f&quot;\u76ee\u5f55 {dir_path} \u4e2d\u7684\u6587\u4ef6\uff1a&quot;)\n    for item in dir_path_obj.iterdir():\n        print(f&quot;  {item.name} - {'\u6587\u4ef6' if item.is_file() else '\u76ee\u5f55'}&quot;)\n\n# 4. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\uff1a&quot;)\n\n# \u5220\u9664\u6587\u4ef6\nif os.path.exists(file_path):\n    os.remove(file_path)\n    print(f&quot;\u5220\u9664\u6587\u4ef6 {file_path}&quot;)\n\n# \u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\nif os.path.exists(dir_path):\n    shutil.rmtree(dir_path)\n    print(f&quot;\u5220\u9664\u76ee\u5f55 {dir_path} \u53ca\u5176\u5185\u5bb9&quot;)<\/code><\/pre>\n<h3>5. \u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p>\u6587\u4ef6I\/O\u64cd\u4f5c\u662fPython\u7f16\u7a0b\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\uff0c\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002<\/p>\n<h4>5.1 \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff08\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff09\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\uff1a<\/p>\n<pre><code class=\"python\"># \u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nwith open(&quot;file.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()\n# \u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\n\n# \u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\nf = open(&quot;file.txt&quot;, &quot;r&quot;)\ncontent = f.read()\nf.close()  # \u9700\u8981\u624b\u52a8\u5173\u95ed<\/code><\/pre>\n<h4>5.2 \u6307\u5b9a\u7f16\u7801<\/h4>\n<p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5e94\u8be5\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\uff0c\u4ee5\u907f\u514d\u7f16\u7801\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"python\"># \u63a8\u8350\u6307\u5b9a\u7f16\u7801\nwith open(&quot;file.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read()\n\n# \u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\uff08\u4f9d\u8d56\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\uff09\nwith open(&quot;file.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()<\/code><\/pre>\n<h4>5.3 \u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<p>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u5e94\u8be5\u9010\u884c\u8bfb\u53d6\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u4e0d\u8db3\uff1a<\/p>\n<pre><code class=\"python\"># \u63a8\u8350\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\nwith open(&quot;large_file.txt&quot;, &quot;r&quot;) as f:\n    for line in f:\n        # \u5904\u7406\u6bcf\u4e00\u884c\n        process_line(line)\n\n# \u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\uff08\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\uff09\nwith open(&quot;large_file.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()  # \u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\n    # \u5904\u7406\u5185\u5bb9<\/code><\/pre>\n<h4>5.4 \u9519\u8bef\u5904\u7406<\/h4>\n<p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5e94\u8be5\u6dfb\u52a0\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff1a<\/p>\n<pre><code class=\"python\"># \u63a8\u8350\u6dfb\u52a0\u9519\u8bef\u5904\u7406\ntry:\n    with open(&quot;file.txt&quot;, &quot;r&quot;) as f:\n        content = f.read()\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\nexcept PermissionError:\n    print(&quot;\u6ca1\u6709\u6743\u9650\u8bfb\u53d6\u6587\u4ef6&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)\n\n# \u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\uff08\u6ca1\u6709\u9519\u8bef\u5904\u7406\uff09\nwith open(&quot;file.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()<\/code><\/pre>\n<h4>5.5 \u7f13\u51b2\u533a\u7ba1\u7406<\/h4>\n<p>\u6839\u636e\u6587\u4ef6\u64cd\u4f5c\u7684\u7279\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u6a21\u5f0f\u548c\u7f13\u51b2\u533a\u5927\u5c0f\uff1a<\/p>\n<ul>\n<li><strong>\u5c0f\u6587\u4ef6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u7f13\u51b2\u6a21\u5f0f<\/li>\n<li><strong>\u5927\u6587\u4ef6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u8f83\u5927\u7684\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u64cd\u4f5c<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u8f83\u5c0f\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u6216\u65e0\u7f13\u51b2<\/li>\n<\/ul>\n<h4>5.6 \u6587\u4ef6\u8def\u5f84\u5904\u7406<\/h4>\n<p>\u4f7f\u7528<code>os.path<\/code>\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff1a<\/p>\n<pre><code class=\"python\"># \u63a8\u8350\u4f7f\u7528os.path\nimport os\nfile_path = os.path.join(&quot;dir&quot;, &quot;file.txt&quot;)\n\n# \u63a8\u8350\u4f7f\u7528pathlib\nfrom pathlib import Path\nfile_path = Path(&quot;dir&quot;) \/ &quot;file.txt&quot;\n\n# \u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\uff08\u786c\u7f16\u7801\u8def\u5f84\u5206\u9694\u7b26\uff09\nfile_path = &quot;dir\/file.txt&quot;  # \u5728Windows\u4e0a\u53ef\u80fd\u6709\u95ee\u9898<\/code><\/pre>\n<h4>5.7 \u6587\u4ef6\u64cd\u4f5c\u7684\u6027\u80fd\u4f18\u5316<\/h4>\n<p>\u6587\u4ef6\u64cd\u4f5c\u7684\u6027\u80fd\u4f18\u5316\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u6a21\u5f0f<\/strong>\uff1a\u6839\u636e\u6587\u4ef6\u5927\u5c0f\u548c\u64cd\u4f5c\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u6a21\u5f0f<\/li>\n<li><strong>\u51cf\u5c11\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6b21\u6570<\/strong>\uff1a\u6279\u91cf\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e<\/li>\n<li><strong>\u4f7f\u7528\u5185\u5b58\u6620\u5c04<\/strong>\uff1a\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mmap<\/code>\u6a21\u5757\u8fdb\u884c\u5185\u5b58\u6620\u5c04<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u6b65I\/O<\/strong>\uff1a\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65I\/O<\/li>\n<li><strong>\u907f\u514d\u9891\u7e41\u7684\u6587\u4ef6\u6253\u5f00\u548c\u5173\u95ed<\/strong>\uff1a\u5c3d\u91cf\u51cf\u5c11\u6587\u4ef6\u6253\u5f00\u548c\u5173\u95ed\u7684\u6b21\u6570<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\n\nimport os\nfrom pathlib import Path\nimport mmap\n\n# 1. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nprint(&quot;\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u63a8\u8350\u7684\u65b9\u5f0f\uff1a&quot;)\nwith open(&quot;best_practice.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:\n    f.write(&quot;Hello, Best Practice!\/n&quot;)\nprint(&quot;\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\uff0c\u6587\u4ef6\u5df2\u81ea\u52a8\u5173\u95ed&quot;)\n\n# 2. \u6307\u5b9a\u7f16\u7801\nprint(&quot;\/n\u6307\u5b9a\u7f16\u7801\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u63a8\u8350\u7684\u65b9\u5f0f\uff1a&quot;)\nwith open(&quot;encoding.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:\n    f.write(&quot;\u4f60\u597d\uff0cPython\uff01\/n&quot;)\nprint(&quot;\u5199\u5165UTF-8\u7f16\u7801\u7684\u6587\u672c\u6587\u4ef6\u6210\u529f&quot;)\n\nwith open(&quot;encoding.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n    content = f.read()\n    print(f&quot;\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# 3. \u5904\u7406\u5927\u6587\u4ef6\nprint(&quot;\/n\u5904\u7406\u5927\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\n# \u521b\u5efa\u4e00\u4e2a\u5927\u6587\u4ef6\nprint(&quot;\u521b\u5efa\u5927\u6587\u4ef6...&quot;)\nwith open(&quot;large_file.txt&quot;, &quot;w&quot;) as f:\n    for i in range(10000):\n        f.write(f&quot;Line {i}: This is a test line for large file.\/n&quot;)\nprint(&quot;\u521b\u5efa\u5927\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\nprint(&quot;\/n\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\uff1a&quot;)\nline_count = 0\nwith open(&quot;large_file.txt&quot;, &quot;r&quot;) as f:\n    for line in f:\n        line_count += 1\n        # \u6bcf1000\u884c\u6253\u5370\u4e00\u6b21\n        if line_count % 1000 == 0:\n            print(f&quot;\u5df2\u8bfb\u53d6 {line_count} \u884c&quot;)\nprint(f&quot;\u6587\u4ef6\u603b\u884c\u6570: {line_count}&quot;)\n\n# 4. \u9519\u8bef\u5904\u7406\nprint(&quot;\/n\u9519\u8bef\u5904\u7406\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u63a8\u8350\u7684\u65b9\u5f0f\uff1a&quot;)\ntry:\n    with open(&quot;non_existent_file.txt&quot;, &quot;r&quot;) as f:\n        content = f.read()\nexcept FileNotFoundError:\n    print(&quot;\u9519\u8bef\uff1a\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\nexcept PermissionError:\n    print(&quot;\u9519\u8bef\uff1a\u6ca1\u6709\u6743\u9650\u8bfb\u53d6\u6587\u4ef6&quot;)\nexcept Exception as e:\n    print(f&quot;\u9519\u8bef\uff1a{e}&quot;)\n\n# 5. \u6587\u4ef6\u8def\u5f84\u5904\u7406\nprint(&quot;\/n\u6587\u4ef6\u8def\u5f84\u5904\u7406\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u4f7f\u7528os.path\uff1a&quot;)\ndir_name = &quot;data&quot;\nfile_name = &quot;results.txt&quot;\nfile_path = os.path.join(dir_name, file_name)\nprint(f&quot;\u62fc\u63a5\u7684\u8def\u5f84: {file_path}&quot;)\n\nprint(&quot;\/n\u4f7f\u7528pathlib\uff1a&quot;)\ndir_path = Path(&quot;data&quot;)\nfile_path = dir_path \/ &quot;results.txt&quot;\nprint(f&quot;\u62fc\u63a5\u7684\u8def\u5f84: {file_path}&quot;)\nprint(f&quot;\u7edd\u5bf9\u8def\u5f84: {file_path.absolute()}&quot;)\n\n# \u521b\u5efa\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\ndir_path.mkdir(exist_ok=True)\nprint(f&quot;\u521b\u5efa\u76ee\u5f55 {dir_path} \u6210\u529f&quot;)\n\n# 6. \u5185\u5b58\u6620\u5c04\u793a\u4f8b\nprint(&quot;\/n\u5185\u5b58\u6620\u5c04\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u8bfb\u53d6\u6587\u4ef6\uff1a&quot;)\nwith open(&quot;large_file.txt&quot;, &quot;r+&quot;) as f:\n    # \u521b\u5efa\u5185\u5b58\u6620\u5c04\n    with mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) as mm:\n        # \u8bfb\u53d6\u5185\u5b58\u6620\u5c04\u7684\u5185\u5bb9\n        content = mm.read(100)\n        print(f&quot;\u5185\u5b58\u6620\u5c04\u8bfb\u53d6\u7684\u524d100\u4e2a\u5b57\u7b26: '{content.decode('utf-8')}'&quot;)\n        \n        # \u67e5\u627e\u5185\u5bb9\n        position = mm.find(b&quot;Line 1000&quot;)\n        if position != -1:\n            mm.seek(position)\n            line = mm.readline()\n            print(f&quot;\u627e\u5230\u7684\u884c: '{line.decode('utf-8').rstrip()}'&quot;)\n\n# 7. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\uff1a&quot;)\n\nfor file in [&quot;best_practice.txt&quot;, &quot;encoding.txt&quot;, &quot;large_file.txt&quot;]:\n    if os.path.exists(file):\n        os.remove(file)\n        print(f&quot;\u5220\u9664\u6587\u4ef6 {file}&quot;)\n\nif os.path.exists(dir_name):\n    import shutil\n    shutil.rmtree(dir_name)\n    print(f&quot;\u5220\u9664\u76ee\u5f55 {dir_name}&quot;)<\/code><\/pre>\n<h3>6. \u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c<\/h3>\n<p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c\uff0c\u7528\u4e8e\u5904\u7406\u7279\u6b8a\u7684\u6587\u4ef6\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<h4>6.1 \u4e34\u65f6\u6587\u4ef6<\/h4>\n<p>\u4e34\u65f6\u6587\u4ef6\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u521b\u5efa\u7684\u4e34\u65f6\u5b58\u50a8\u6587\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u4e2d\u95f4\u6570\u636e\u3002Python\u7684<code>tempfile<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong><code>tempfile.TemporaryFile()<\/code><\/strong>\uff1a\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\uff0c\u5173\u95ed\u540e\u81ea\u52a8\u5220\u9664<\/li>\n<li><strong><code>tempfile.NamedTemporaryFile()<\/code><\/strong>\uff1a\u521b\u5efa\u547d\u540d\u4e34\u65f6\u6587\u4ef6<\/li>\n<li><strong><code>tempfile.TemporaryDirectory()<\/code><\/strong>\uff1a\u521b\u5efa\u4e34\u65f6\u76ee\u5f55<\/li>\n<\/ul>\n<h4>6.2 \u6587\u4ef6\u9501<\/h4>\n<p>\u6587\u4ef6\u9501\u7528\u4e8e\u5728\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\u540c\u6b65\u5bf9\u6587\u4ef6\u7684\u8bbf\u95ee\uff0c\u907f\u514d\u5e76\u53d1\u8bbf\u95ee\u5bfc\u81f4\u7684\u6570\u636e\u4e0d\u4e00\u81f4\u3002Python\u7684<code>fcntl<\/code>\u6a21\u5757\uff08\u5728Unix\u7cfb\u7edf\u4e0a\uff09\u548c<code>msvcrt<\/code>\u6a21\u5757\uff08\u5728Windows\u7cfb\u7edf\u4e0a\uff09\u63d0\u4f9b\u4e86\u6587\u4ef6\u9501\u529f\u80fd\u3002<\/p>\n<h4>6.3 \u5185\u5b58\u6587\u4ef6\u5bf9\u8c61<\/h4>\n<p>\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61\u662f\u5728\u5185\u5b58\u4e2d\u6a21\u62df\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u4e0d\u4f7f\u7528\u5b9e\u9645\u6587\u4ef6\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002Python\u7684<code>io<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61\u7684\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong><code>io.StringIO()<\/code><\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61<\/li>\n<li><strong><code>io.BytesIO()<\/code><\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61<\/li>\n<\/ul>\n<h4>6.4 \u538b\u7f29\u6587\u4ef6<\/h4>\n<p>Python\u7684<code>gzip<\/code>\u3001<code>bz2<\/code>\u3001<code>lzma<\/code>\u7b49\u6a21\u5757\u63d0\u4f9b\u4e86\u538b\u7f29\u6587\u4ef6\u7684\u8bfb\u5199\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong><code>gzip.open()<\/code><\/strong>\uff1a\u8bfb\u5199gzip\u538b\u7f29\u6587\u4ef6<\/li>\n<li><strong><code>bz2.open()<\/code><\/strong>\uff1a\u8bfb\u5199bz2\u538b\u7f29\u6587\u4ef6<\/li>\n<li><strong><code>lzma.open()<\/code><\/strong>\uff1a\u8bfb\u5199lzma\u538b\u7f29\u6587\u4ef6<\/li>\n<\/ul>\n<h4>6.5 \u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c\u793a\u4f8b\n\nimport tempfile\nimport io\nimport gzip\nimport os\n\n# 1. \u4e34\u65f6\u6587\u4ef6\u793a\u4f8b\nprint(&quot;\u4e34\u65f6\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u4f7f\u7528TemporaryFile\uff1a&quot;)\nwith tempfile.TemporaryFile(mode='w+') as f:\n    # \u5199\u5165\u6570\u636e\n    f.write(&quot;\u8fd9\u662f\u4e34\u65f6\u6587\u4ef6\u7684\u5185\u5bb9\/n&quot;)\n    f.write(&quot;\u4e34\u65f6\u6587\u4ef6\u4f1a\u5728\u5173\u95ed\u540e\u81ea\u52a8\u5220\u9664\/n&quot;)\n    \n    # \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u5f00\u5934\n    f.seek(0)\n    \n    # \u8bfb\u53d6\u6570\u636e\n    content = f.read()\n    print(f&quot;\u4e34\u65f6\u6587\u4ef6\u5185\u5bb9\uff1a\/n{content}&quot;)\nprint(&quot;\u4e34\u65f6\u6587\u4ef6\u5df2\u5173\u95ed\u5e76\u81ea\u52a8\u5220\u9664&quot;)\n\nprint(&quot;\/n\u4f7f\u7528NamedTemporaryFile\uff1a&quot;)\nwith tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:\n    # \u5199\u5165\u6570\u636e\n    f.write(&quot;\u8fd9\u662f\u547d\u540d\u4e34\u65f6\u6587\u4ef6\u7684\u5185\u5bb9\/n&quot;)\n    temp_file_name = f.name\n    print(f&quot;\u4e34\u65f6\u6587\u4ef6\u540d\u79f0\uff1a{temp_file_name}&quot;)\n\n# \u68c0\u67e5\u4e34\u65f6\u6587\u4ef6\u662f\u5426\u5b58\u5728\nprint(f&quot;\u4e34\u65f6\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a{os.path.exists(temp_file_name)}&quot;)\n\n# \u8bfb\u53d6\u4e34\u65f6\u6587\u4ef6\nwith open(temp_file_name, &quot;r&quot;) as f:\n    content = f.read()\n    print(f&quot;\u4e34\u65f6\u6587\u4ef6\u5185\u5bb9\uff1a\/n{content}&quot;)\n\n# \u5220\u9664\u4e34\u65f6\u6587\u4ef6\nos.unlink(temp_file_name)\nprint(f&quot;\u5220\u9664\u4e34\u65f6\u6587\u4ef6\uff1a{temp_file_name}&quot;)\nprint(f&quot;\u4e34\u65f6\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a{os.path.exists(temp_file_name)}&quot;)\n\nprint(&quot;\/n\u4f7f\u7528TemporaryDirectory\uff1a&quot;)\nwith tempfile.TemporaryDirectory() as temp_dir:\n    print(f&quot;\u4e34\u65f6\u76ee\u5f55\uff1a{temp_dir}&quot;)\n    \n    # \u5728\u4e34\u65f6\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6\n    temp_file = os.path.join(temp_dir, &quot;test.txt&quot;)\n    with open(temp_file, &quot;w&quot;) as f:\n        f.write(&quot;\u4e34\u65f6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\/n&quot;)\n    \n    # \u8bfb\u53d6\u6587\u4ef6\n    with open(temp_file, &quot;r&quot;) as f:\n        content = f.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nprint(&quot;\u4e34\u65f6\u76ee\u5f55\u5df2\u5173\u95ed\u5e76\u81ea\u52a8\u5220\u9664&quot;)\n\n# 2. \u5185\u5b58\u6587\u4ef6\u5bf9\u8c61\u793a\u4f8b\nprint(&quot;\/n\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\/n\u4f7f\u7528StringIO\uff1a&quot;)\n# \u521b\u5efaStringIO\u5bf9\u8c61\nstring_io = io.StringIO()\n\n# \u5199\u5165\u6570\u636e\nstring_io.write(&quot;\u8fd9\u662fStringIO\u7684\u5185\u5bb9\/n&quot;)\nstring_io.write(&quot;StringIO\u662f\u5728\u5185\u5b58\u4e2d\u6a21\u62df\u7684\u6587\u4ef6\u5bf9\u8c61\/n&quot;)\n\n# \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u5f00\u5934\nstring_io.seek(0)\n\n# \u8bfb\u53d6\u6570\u636e\ncontent = string_io.read()\nprint(f&quot;StringIO\u5185\u5bb9\uff1a\/n{content}&quot;)\n\n# \u5173\u95edStringIO\nstring_io.close()\n\nprint(&quot;\/n\u4f7f\u7528BytesIO\uff1a&quot;)\n# \u521b\u5efaBytesIO\u5bf9\u8c61\nbytes_io = io.BytesIO()\n\n# \u5199\u5165\u6570\u636e\nbytes_io.write(b&quot;\u8fd9\u662fBytesIO\u7684\u5185\u5bb9\/n&quot;)\nbytes_io.write(b&quot;BytesIO\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\/n&quot;)\n\n# \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u5f00\u5934\nbytes_io.seek(0)\n\n# \u8bfb\u53d6\u6570\u636e\ncontent = bytes_io.read()\nprint(f&quot;BytesIO\u5185\u5bb9\uff1a\/n{content.decode('utf-8')}&quot;)\n\n# \u5173\u95edBytesIO\nbytes_io.close()\n\n# 3. \u538b\u7f29\u6587\u4ef6\u793a\u4f8b\nprint(&quot;\/n\u538b\u7f29\u6587\u4ef6\u793a\u4f8b\uff1a&quot;)\n\n# \u521b\u5efagzip\u538b\u7f29\u6587\u4ef6\nprint(&quot;\/n\u521b\u5efagzip\u538b\u7f29\u6587\u4ef6\uff1a&quot;)\nwith gzip.open(&quot;compressed_file.txt.gz&quot;, &quot;wb&quot;) as f:\n    f.write(b&quot;\u8fd9\u662f\u538b\u7f29\u6587\u4ef6\u7684\u5185\u5bb9\/n&quot;)\n    f.write(b&quot;gzip\u6a21\u5757\u7528\u4e8e\u5904\u7406gzip\u538b\u7f29\u6587\u4ef6\/n&quot;)\nprint(&quot;\u521b\u5efagzip\u538b\u7f29\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u8bfb\u53d6gzip\u538b\u7f29\u6587\u4ef6\nprint(&quot;\/n\u8bfb\u53d6gzip\u538b\u7f29\u6587\u4ef6\uff1a&quot;)\nwith gzip.open(&quot;compressed_file.txt.gz&quot;, &quot;rb&quot;) as f:\n    content = f.read()\n    print(f&quot;\u538b\u7f29\u6587\u4ef6\u5185\u5bb9\uff1a\/n{content.decode('utf-8')}&quot;)\n\n# 4. \u6587\u4ef6\u9501\u793a\u4f8b\uff08Unix\u7cfb\u7edf\uff09\nprint(&quot;\/n\u6587\u4ef6\u9501\u793a\u4f8b\uff1a&quot;)\n\nprint(&quot;\u6ce8\u610f\uff1a\u6587\u4ef6\u9501\u793a\u4f8b\u5728Windows\u7cfb\u7edf\u4e0a\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u5b9e\u73b0&quot;)\ntry:\n    import fcntl\n    \n    # \u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\n    with open(&quot;locked_file.txt&quot;, &quot;w&quot;) as f:\n        f.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u9700\u8981\u52a0\u9501\u7684\u6587\u4ef6\/n&quot;)\n    \n    # \u6253\u5f00\u6587\u4ef6\u5e76\u52a0\u9501\n    print(&quot;\/n\u6253\u5f00\u6587\u4ef6\u5e76\u52a0\u9501\uff1a&quot;)\n    with open(&quot;locked_file.txt&quot;, &quot;r+&quot;) as f:\n        # \u83b7\u53d6\u6587\u4ef6\u9501\n        print(&quot;\u83b7\u53d6\u6587\u4ef6\u9501...&quot;)\n        fcntl.flock(f, fcntl.LOCK_EX)  # \u6392\u4ed6\u9501\n        print(&quot;\u83b7\u53d6\u6587\u4ef6\u9501\u6210\u529f&quot;)\n        \n        # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n        content = f.read()\n        print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\n        \n        # \u5199\u5165\u6570\u636e\n        f.seek(0)\n        f.write(&quot;\u8fd9\u662f\u52a0\u9501\u540e\u4fee\u6539\u7684\u5185\u5bb9\/n&quot;)\n        f.truncate()\n        print(&quot;\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\u6210\u529f&quot;)\n        \n        # \u91ca\u653e\u6587\u4ef6\u9501\n        print(&quot;\u91ca\u653e\u6587\u4ef6\u9501...&quot;)\n        fcntl.flock(f, fcntl.LOCK_UN)\n        print(&quot;\u91ca\u653e\u6587\u4ef6\u9501\u6210\u529f&quot;)\n        \nexcept ImportError:\n    print(&quot;fcntl\u6a21\u5757\u5728Windows\u7cfb\u7edf\u4e0a\u4e0d\u53ef\u7528&quot;)\nexcept Exception as e:\n    print(f&quot;\u6587\u4ef6\u9501\u64cd\u4f5c\u5931\u8d25\uff1a{e}&quot;)\n\n# 5. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\uff1a&quot;)\n\nfor file in [&quot;compressed_file.txt.gz&quot;, &quot;locked_file.txt&quot;]:\n    if os.path.exists(file):\n        os.remove(file)\n        print(f&quot;\u5220\u9664\u6587\u4ef6 {file}&quot;)<\/code><\/pre>\n<h3>7. \u5e38\u89c1\u6587\u4ef6I\/O\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u5728Python\u6587\u4ef6I\/O\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u3002\u7406\u89e3\u8fd9\u4e9b\u95ee\u9898\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u5bf9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>7.1 \u5e38\u89c1\u95ee\u9898<\/h4>\n<ul>\n<li><strong>\u6587\u4ef6\u4e0d\u5b58\u5728<\/strong>\uff1a\u5c1d\u8bd5\u6253\u5f00\u4e0d\u5b58\u5728\u7684\u6587\u4ef6<\/li>\n<li><strong>\u6743\u9650\u9519\u8bef<\/strong>\uff1a\u6ca1\u6709\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u7684\u6743\u9650<\/li>\n<li><strong>\u7f16\u7801\u9519\u8bef<\/strong>\uff1a\u6587\u4ef6\u7f16\u7801\u4e0e\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u5339\u914d<\/li>\n<li><strong>\u5185\u5b58\u4e0d\u8db3<\/strong>\uff1a\u5c1d\u8bd5\u4e00\u6b21\u6027\u8bfb\u53d6\u5927\u6587\u4ef6\u5230\u5185\u5b58<\/li>\n<li><strong>\u6587\u4ef6\u88ab\u5360\u7528<\/strong>\uff1a\u6587\u4ef6\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528<\/li>\n<li><strong>\u8def\u5f84\u9519\u8bef<\/strong>\uff1a\u6587\u4ef6\u8def\u5f84\u4e0d\u6b63\u786e<\/li>\n<li><strong>\u7f13\u51b2\u533a\u672a\u5237\u65b0<\/strong>\uff1a\u6570\u636e\u672a\u53ca\u65f6\u5199\u5165\u6587\u4ef6<\/li>\n<li><strong>\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u9519\u8bef<\/strong>\uff1a\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u4e0d\u6b63\u786e\u5bfc\u81f4\u8bfb\u5199\u9519\u8bef<\/li>\n<\/ul>\n<h4>7.2 \u89e3\u51b3\u65b9\u6848<\/h4>\n<h5>7.2.1 \u6587\u4ef6\u4e0d\u5b58\u5728<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u5c1d\u8bd5\u6253\u5f00\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>os.path.exists()<\/code>\u6216<code>Path.exists()<\/code>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/li>\n<li>\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7<code>FileNotFoundError<\/code>\u5f02\u5e38<\/li>\n<li>\u5728\u5199\u5165\u6a21\u5f0f\u4e0b\uff0c\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u81ea\u52a8\u521b\u5efa<\/li>\n<\/ul>\n<h5>7.2.2 \u6743\u9650\u9519\u8bef<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6ca1\u6709\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u7684\u6743\u9650\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e<\/li>\n<li>\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7<code>PermissionError<\/code>\u5f02\u5e38<\/li>\n<li>\u786e\u4fdd\u4ee5\u6b63\u786e\u7684\u7528\u6237\u8eab\u4efd\u8fd0\u884c\u7a0b\u5e8f<\/li>\n<\/ul>\n<h5>7.2.3 \u7f16\u7801\u9519\u8bef<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6587\u4ef6\u7f16\u7801\u4e0e\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u5339\u914d\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u663e\u5f0f\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801<\/li>\n<li>\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7<code>UnicodeDecodeError<\/code>\u6216<code>UnicodeEncodeError<\/code>\u5f02\u5e38<\/li>\n<li>\u4f7f\u7528<code>chardet<\/code>\u5e93\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801<\/li>\n<\/ul>\n<h5>7.2.4 \u5185\u5b58\u4e0d\u8db3<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u5c1d\u8bd5\u4e00\u6b21\u6027\u8bfb\u53d6\u5927\u6587\u4ef6\u5230\u5185\u5b58\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/li>\n<li>\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6587\u4ef6<\/li>\n<li>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\uff08<code>mmap<\/code>\uff09\u5904\u7406\u5927\u6587\u4ef6<\/li>\n<\/ul>\n<h5>7.2.5 \u6587\u4ef6\u88ab\u5360\u7528<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6587\u4ef6\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u5176\u4ed6\u8fdb\u7a0b\u5df2\u91ca\u653e\u6587\u4ef6<\/li>\n<li>\u4f7f\u7528\u6587\u4ef6\u9501\u673a\u5236<\/li>\n<li>\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u91cd\u8bd5<\/li>\n<\/ul>\n<h5>7.2.6 \u8def\u5f84\u9519\u8bef<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6587\u4ef6\u8def\u5f84\u4e0d\u6b63\u786e\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>os.path<\/code>\u6216<code>pathlib<\/code>\u6a21\u5757\u5904\u7406\u8def\u5f84<\/li>\n<li>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/li>\n<li>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u800c\u4e0d\u662f\u76f8\u5bf9\u8def\u5f84<\/li>\n<\/ul>\n<h5>7.2.7 \u7f13\u51b2\u533a\u672a\u5237\u65b0<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6570\u636e\u672a\u53ca\u65f6\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>flush()<\/code>\u65b9\u6cd5\u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<li>\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a<\/li>\n<li>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff0c\u9000\u51fa\u65f6\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<\/li>\n<\/ul>\n<h5>7.2.8 \u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u9519\u8bef<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u4e0d\u6b63\u786e\u5bfc\u81f4\u8bfb\u5199\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>tell()<\/code>\u65b9\u6cd5\u67e5\u770b\u5f53\u524d\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e<\/li>\n<li>\u4f7f\u7528<code>seek()<\/code>\u65b9\u6cd5\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6b63\u786e\u4f4d\u7f6e<\/li>\n<li>\u6ce8\u610f\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u5bf9\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u7684\u5f71\u54cd<\/li>\n<\/ul>\n<h4>7.3 \u793a\u4f8b\uff1a\u89e3\u51b3\u5e38\u89c1\u6587\u4ef6I\/O\u95ee\u9898<\/h4>\n<pre><code class=\"python\"># \u5e38\u89c1\u6587\u4ef6I\/O\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\n\nimport os\nfrom pathlib import Path\nimport chardet\n\n# 1. \u6587\u4ef6\u4e0d\u5b58\u5728\nprint(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\nprint(&quot;\/n\u65b9\u6cd51\uff1a\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728&quot;)\nfile_path = &quot;non_existent_file.txt&quot;\nif os.path.exists(file_path):\n    with open(file_path, &quot;r&quot;) as f:\n        content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nelse:\n    print(f&quot;\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728&quot;)\n\nprint(&quot;\/n\u65b9\u6cd52\uff1a\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38&quot;)\ntry:\n    with open(file_path, &quot;r&quot;) as f:\n        content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nexcept FileNotFoundError:\n    print(f&quot;\u9519\u8bef\uff1a\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728&quot;)\n\n# 2. \u7f16\u7801\u9519\u8bef\nprint(&quot;\/n\u7f16\u7801\u9519\u8bef\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\n# \u521b\u5efa\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u6587\u4ef6\nwith open(&quot;utf8_file.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:\n    f.write(&quot;\u4f60\u597d\uff0cPython\uff01\/n&quot;)\nprint(&quot;\u521b\u5efaUTF-8\u7f16\u7801\u7684\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u5c1d\u8bd5\u4f7f\u7528\u9519\u8bef\u7684\u7f16\u7801\u8bfb\u53d6\nprint(&quot;\/n\u5c1d\u8bd5\u4f7f\u7528\u9519\u8bef\u7684\u7f16\u7801\u8bfb\u53d6\uff1a&quot;)\ntry:\n    with open(&quot;utf8_file.txt&quot;, &quot;r&quot;, encoding=&quot;ascii&quot;) as f:\n        content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nexcept UnicodeDecodeError as e:\n    print(f&quot;\u7f16\u7801\u9519\u8bef\uff1a{e}&quot;)\n\n# \u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u8bfb\u53d6\nprint(&quot;\/n\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u8bfb\u53d6\uff1a&quot;)\ntry:\n    with open(&quot;utf8_file.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n        content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nexcept Exception as e:\n    print(f&quot;\u9519\u8bef\uff1a{e}&quot;)\n\n# \u4f7f\u7528chardet\u68c0\u6d4b\u7f16\u7801\nprint(&quot;\/n\u4f7f\u7528chardet\u68c0\u6d4b\u7f16\u7801\uff1a&quot;)\ntry:\n    with open(&quot;utf8_file.txt&quot;, &quot;rb&quot;) as f:\n        raw_data = f.read()\n    \n    # \u68c0\u6d4b\u7f16\u7801\n    result = chardet.detect(raw_data)\n    encoding = result[&quot;encoding&quot;]\n    confidence = result[&quot;confidence&quot;]\n    \n    print(f&quot;\u68c0\u6d4b\u5230\u7684\u7f16\u7801\uff1a{encoding}\uff08\u7f6e\u4fe1\u5ea6\uff1a{confidence:.2f}&quot;)\n    \n    # \u4f7f\u7528\u68c0\u6d4b\u5230\u7684\u7f16\u7801\u8bfb\u53d6\n    content = raw_data.decode(encoding)\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9\uff1a{content}&quot;)\nexcept Exception as e:\n    print(f&quot;\u9519\u8bef\uff1a{e}&quot;)\n\n# 3. \u5185\u5b58\u4e0d\u8db3\nprint(&quot;\/n\u5185\u5b58\u4e0d\u8db3\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\n# \u521b\u5efa\u4e00\u4e2a\u5927\u6587\u4ef6\nprint(&quot;\u521b\u5efa\u5927\u6587\u4ef6...&quot;)\nwith open(&quot;large_file.txt&quot;, &quot;w&quot;) as f:\n    for i in range(50000):\n        f.write(f&quot;Line {i}: This is a test line for memory issue.\/n&quot;)\nprint(&quot;\u521b\u5efa\u5927\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\nprint(&quot;\/n\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\uff1a&quot;)\nline_count = 0\nwith open(&quot;large_file.txt&quot;, &quot;r&quot;) as f:\n    for line in f:\n        line_count += 1\n        if line_count % 10000 == 0:\n            print(f&quot;\u5df2\u8bfb\u53d6 {line_count} \u884c&quot;)\nprint(f&quot;\u6587\u4ef6\u603b\u884c\u6570: {line_count}&quot;)\n\n# \u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6587\u4ef6\nprint(&quot;\/n\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6587\u4ef6\uff1a&quot;)\ndef read_large_file(file_path, chunk_size=1024):\n    &quot;&quot;&quot;\u4f7f\u7528\u751f\u6210\u5668\u8bfb\u53d6\u5927\u6587\u4ef6&quot;&quot;&quot;\n    with open(file_path, &quot;r&quot;) as f:\n        while True:\n            chunk = f.read(chunk_size)\n            if not chunk:\n                break\n            yield chunk\n\n# \u4f7f\u7528\u751f\u6210\u5668\u8bfb\u53d6\u6587\u4ef6\nchar_count = 0\nfor chunk in read_large_file(&quot;large_file.txt&quot;):\n    char_count += len(chunk)\nprint(f&quot;\u6587\u4ef6\u603b\u5b57\u7b26\u6570: {char_count}&quot;)\n\n# 4. \u8def\u5f84\u9519\u8bef\nprint(&quot;\/n\u8def\u5f84\u9519\u8bef\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\n# \u4f7f\u7528os.path\u5904\u7406\u8def\u5f84\nprint(&quot;\/n\u4f7f\u7528os.path\u5904\u7406\u8def\u5f84\uff1a&quot;)\ndir_name = &quot;data&quot;\nfile_name = &quot;results.txt&quot;\n\n# \u521b\u5efa\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\nif not os.path.exists(dir_name):\n    os.makedirs(dir_name)\n    print(f&quot;\u521b\u5efa\u76ee\u5f55 {dir_name} \u6210\u529f&quot;)\n\n# \u62fc\u63a5\u8def\u5f84\nfile_path = os.path.join(dir_name, file_name)\nprint(f&quot;\u62fc\u63a5\u7684\u8def\u5f84: {file_path}&quot;)\nprint(f&quot;\u7edd\u5bf9\u8def\u5f84: {os.path.abspath(file_path)}&quot;)\n\n# \u5199\u5165\u6587\u4ef6\nwith open(file_path, &quot;w&quot;) as f:\n    f.write(&quot;\u6d4b\u8bd5\u8def\u5f84\u5904\u7406\/n&quot;)\nprint(f&quot;\u5199\u5165\u6587\u4ef6 {file_path} \u6210\u529f&quot;)\n\n# \u4f7f\u7528pathlib\u5904\u7406\u8def\u5f84\nprint(&quot;\/n\u4f7f\u7528pathlib\u5904\u7406\u8def\u5f84\uff1a&quot;)\ndir_path = Path(&quot;data2&quot;)\nfile_path = dir_path \/ &quot;results.txt&quot;\n\n# \u521b\u5efa\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\ndir_path.mkdir(exist_ok=True)\nprint(f&quot;\u521b\u5efa\u76ee\u5f55 {dir_path} \u6210\u529f&quot;)\n\nprint(f&quot;\u62fc\u63a5\u7684\u8def\u5f84: {file_path}&quot;)\nprint(f&quot;\u7edd\u5bf9\u8def\u5f84: {file_path.absolute()}&quot;)\n\n# \u5199\u5165\u6587\u4ef6\nfile_path.write_text(&quot;\u6d4b\u8bd5pathlib\u8def\u5f84\u5904\u7406\/n&quot;)\nprint(f&quot;\u5199\u5165\u6587\u4ef6 {file_path} \u6210\u529f&quot;)\n\n# 5. \u7f13\u51b2\u533a\u672a\u5237\u65b0\nprint(&quot;\/n\u7f13\u51b2\u533a\u672a\u5237\u65b0\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\nprint(&quot;\/n\u6d4b\u8bd5\u7f13\u51b2\u533a\u5237\u65b0\uff1a&quot;)\nwith open(&quot;buffer_test.txt&quot;, &quot;w&quot;) as f:\n    print(&quot;\u5199\u5165\u6570\u636e...&quot;)\n    f.write(&quot;\u9700\u8981\u5237\u65b0\u7f13\u51b2\u533a\u7684\u6570\u636e\/n&quot;)\n    print(&quot;\u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a...&quot;)\n    f.flush()  # \u624b\u52a8\u5237\u65b0\u7f13\u51b2\u533a\n    print(&quot;\u7f13\u51b2\u533a\u5df2\u5237\u65b0&quot;)\n\n# \u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\nwith open(&quot;buffer_test.txt&quot;, &quot;r&quot;) as f:\n    content = f.read()\n    print(f&quot;\u6587\u4ef6\u5185\u5bb9: '{content}'&quot;)\n\n# 6. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55\uff1a&quot;)\n\n# \u5220\u9664\u6587\u4ef6\nfor file in [&quot;utf8_file.txt&quot;, &quot;large_file.txt&quot;, &quot;buffer_test.txt&quot;]:\n    if os.path.exists(file):\n        os.remove(file)\n        print(f&quot;\u5220\u9664\u6587\u4ef6 {file}&quot;)\n\n# \u5220\u9664\u76ee\u5f55\nimport shutil\nfor dir_name in [&quot;data&quot;, &quot;data2&quot;]:\n    if os.path.exists(dir_name):\n        shutil.rmtree(dir_name)\n        print(f&quot;\u5220\u9664\u76ee\u5f55 {dir_name}&quot;)<\/code><\/pre>\n<h3>8. \u6587\u4ef6I\/O\u6027\u80fd\u4f18\u5316<\/h3>\n<p>\u6587\u4ef6I\/O\u64cd\u4f5c\u662f\u7a0b\u5e8f\u6027\u80fd\u7684\u5e38\u89c1\u74f6\u9888\u4e4b\u4e00\u3002\u7406\u89e3\u6587\u4ef6I\/O\u6027\u80fd\u4f18\u5316\u7684\u65b9\u6cd5\u5bf9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>8.1 \u5f71\u54cd\u6587\u4ef6I\/O\u6027\u80fd\u7684\u56e0\u7d20<\/h4>\n<ul>\n<li><strong>\u78c1\u76d8\u901f\u5ea6<\/strong>\uff1a\u673a\u68b0\u786c\u76d8\uff08HDD\uff09\u548c\u56fa\u6001\u786c\u76d8\uff08SSD\uff09\u7684\u901f\u5ea6\u5dee\u5f02\u5f88\u5927<\/li>\n<li><strong>\u6587\u4ef6\u5927\u5c0f<\/strong>\uff1a\u5927\u6587\u4ef6\u7684I\/O\u64cd\u4f5c\u901a\u5e38\u6bd4\u5c0f\u6587\u4ef6\u6162<\/li>\n<li><strong>\u7f13\u51b2\u7b56\u7565<\/strong>\uff1a\u7f13\u51b2\u533a\u7684\u5927\u5c0f\u548c\u6a21\u5f0f\u4f1a\u5f71\u54cdI\/O\u6027\u80fd<\/li>\n<li><strong>I\/O\u6a21\u5f0f<\/strong>\uff1a\u987a\u5e8fI\/O\u901a\u5e38\u6bd4\u968f\u673aI\/O\u5feb<\/li>\n<li><strong>\u6587\u4ef6\u7cfb\u7edf<\/strong>\uff1a\u4e0d\u540c\u7684\u6587\u4ef6\u7cfb\u7edf\u6709\u4e0d\u540c\u7684\u6027\u80fd\u7279\u6027<\/li>\n<li><strong>\u64cd\u4f5c\u7cfb\u7edf<\/strong>\uff1a\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u6709\u4e0d\u540c\u7684I\/O\u5904\u7406\u673a\u5236<\/li>\n<li><strong>\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1<\/strong>\uff1a\u7a0b\u5e8f\u7684I\/O\u64cd\u4f5c\u65b9\u5f0f\u4f1a\u5f71\u54cd\u6027\u80fd<\/li>\n<\/ul>\n<h4>8.2 \u6027\u80fd\u4f18\u5316\u7684\u65b9\u6cd5<\/h4>\n<h5>8.2.1 \u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u7b56\u7565<\/h5>\n<ul>\n<li><strong>\u5927\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u8f83\u5927\u7684\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>\u5c0f\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u64cd\u4f5c<\/strong>\uff1a\u4f7f\u7528\u8f83\u5c0f\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u6216\u65e0\u7f13\u51b2<\/li>\n<\/ul>\n<h5>8.2.2 \u51cf\u5c11I\/O\u64cd\u4f5c\u7684\u6b21\u6570<\/h5>\n<ul>\n<li><strong>\u6279\u91cf\u8bfb\u5199<\/strong>\uff1a\u5c3d\u91cf\u51cf\u5c11\u8bfb\u5199\u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u6279\u91cf\u5904\u7406\u6570\u636e<\/li>\n<li><strong>\u5408\u5e76\u5c0f\u6587\u4ef6<\/strong>\uff1a\u5c06\u591a\u4e2a\u5c0f\u6587\u4ef6\u5408\u5e76\u4e3a\u4e00\u4e2a\u5927\u6587\u4ef6\uff0c\u51cf\u5c11\u6587\u4ef6\u6253\u5f00\u548c\u5173\u95ed\u7684\u6b21\u6570<\/li>\n<li><strong>\u4f7f\u7528\u5185\u5b58\u7f13\u5b58<\/strong>\uff1a\u5bf9\u4e8e\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u4f7f\u7528\u5185\u5b58\u7f13\u5b58<\/li>\n<\/ul>\n<h5>8.2.3 \u4f18\u5316\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f<\/h5>\n<ul>\n<li><strong>\u987a\u5e8f\u8bbf\u95ee<\/strong>\uff1a\u5c3d\u91cf\u4f7f\u7528\u987a\u5e8f\u8bbf\u95ee\u800c\u4e0d\u662f\u968f\u673a\u8bbf\u95ee<\/li>\n<li><strong>\u9884\u8bfb<\/strong>\uff1a\u5bf9\u4e8e\u987a\u5e8f\u8bbf\u95ee\u7684\u6587\u4ef6\uff0c\u4f7f\u7528\u9884\u8bfb\u673a\u5236<\/li>\n<li><strong>\u5ef6\u8fdf\u5199\u5165<\/strong>\uff1a\u5bf9\u4e8e\u5199\u5165\u64cd\u4f5c\uff0c\u4f7f\u7528\u5ef6\u8fdf\u5199\u5165\u673a\u5236<\/li>\n<\/ul>\n<h5>8.2.4 \u4f7f\u7528\u9ad8\u7ea7I\/O\u6280\u672f<\/h5>\n<ul>\n<li><strong>\u5185\u5b58\u6620\u5c04<\/strong>\uff1a\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u4f7f\u7528\u5185\u5b58\u6620\u5c04\uff08<code>mmap<\/code>\uff09<\/li>\n<li><strong>\u5f02\u6b65I\/O<\/strong>\uff1a\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u64cd\u4f5c\uff0c\u4f7f\u7528\u5f02\u6b65I\/O<\/li>\n<li><strong>\u76f4\u63a5I\/O<\/strong>\uff1a\u5bf9\u4e8e\u67d0\u4e9b\u573a\u666f\uff0c\u4f7f\u7528\u76f4\u63a5I\/O\u7ed5\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7f13\u51b2\u533a<\/li>\n<li><strong>\u5e76\u884cI\/O<\/strong>\uff1a\u5bf9\u4e8e\u591a\u4e2a\u6587\u4ef6\uff0c\u4f7f\u7528\u5e76\u884cI\/O\u64cd\u4f5c<\/li>\n<\/ul>\n<h5>8.2.5 \u6587\u4ef6\u7cfb\u7edf\u4f18\u5316<\/h5>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u7cfb\u7edf<\/strong>\uff1a\u6839\u636e\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u7cfb\u7edf<\/li>\n<li><strong>\u4f18\u5316\u6587\u4ef6\u7cfb\u7edf\u53c2\u6570<\/strong>\uff1a\u8c03\u6574\u6587\u4ef6\u7cfb\u7edf\u7684\u53c2\u6570\u4ee5\u63d0\u9ad8\u6027\u80fd<\/li>\n<li><strong>\u4f7f\u7528RAID<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u7684\u573a\u666f\uff0c\u4f7f\u7528RAID\u6280\u672f<\/li>\n<\/ul>\n<h4>8.3 \u6027\u80fd\u4f18\u5316\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u6587\u4ef6I\/O\u6027\u80fd\u4f18\u5316\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u6587\u4ef6I\/O\u6027\u80fd\u4f18\u5316\u793a\u4f8b\n\nimport os\nimport time\nimport mmap\nimport concurrent.futures\n\n# 1. \u7f13\u51b2\u533a\u5927\u5c0f\u4f18\u5316\nprint(&quot;\u7f13\u51b2\u533a\u5927\u5c0f\u4f18\u5316\u793a\u4f8b\uff1a&quot;)\n\n# \u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6\nprint(&quot;\/n\u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6...&quot;)\ntest_file = &quot;performance_test.txt&quot;\nwith open(test_file, &quot;w&quot;) as f:\n    for i in range(100000):\n        f.write(f&quot;Line {i}: This is a test line for performance optimization.\/n&quot;)\nprint(&quot;\u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6\u6210\u529f&quot;)\n\n# \u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u8bfb\u53d6\u6027\u80fd\ndef test_read_performance(buffer_size):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u8bfb\u53d6\u6027\u80fd&quot;&quot;&quot;\n    start_time = time.time()\n    with open(test_file, &quot;r&quot;, buffering=buffer_size) as f:\n        content = f.read()\n    end_time = time.time()\n    return end_time - start_time\n\nprint(&quot;\/n\u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u8bfb\u53d6\u6027\u80fd\uff1a&quot;)\nbuffer_sizes = [1, 4096, 8192, 16384, 32768, 65536]\nfor size in buffer_sizes:\n    try:\n        elapsed_time = test_read_performance(size)\n        print(f&quot;\u7f13\u51b2\u533a\u5927\u5c0f {size} \u5b57\u8282: {elapsed_time:.4f} \u79d2&quot;)\n    except Exception as e:\n        print(f&quot;\u7f13\u51b2\u533a\u5927\u5c0f {size} \u5b57\u8282: \u6d4b\u8bd5\u5931\u8d25 - {e}&quot;)\n\n# 2. \u6279\u91cf\u8bfb\u5199\u4f18\u5316\nprint(&quot;\/n\u6279\u91cf\u8bfb\u5199\u4f18\u5316\u793a\u4f8b\uff1a&quot;)\n\n# \u6d4b\u8bd5\u9010\u884c\u5199\u5165\u4e0e\u6279\u91cf\u5199\u5165\u7684\u6027\u80fd\ndef test_write_methods():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u4e0d\u540c\u5199\u5165\u65b9\u6cd5\u7684\u6027\u80fd&quot;&quot;&quot;\n    # \u6d4b\u8bd5\u6570\u636e\n    lines = [f&quot;Line {i}: This is a test line.\/n&quot; for i in range(100000)]\n    \n    # \u9010\u884c\u5199\u5165\n    start_time = time.time()\n    with open(&quot;line_write.txt&quot;, &quot;w&quot;) as f:\n        for line in lines:\n            f.write(line)\n    line_write_time = time.time() - start_time\n    print(f&quot;\u9010\u884c\u5199\u5165\u8017\u65f6: {line_write_time:.4f} \u79d2&quot;)\n    \n    # \u6279\u91cf\u5199\u5165\n    start_time = time.time()\n    with open(&quot;batch_write.txt&quot;, &quot;w&quot;) as f:\n        f.writelines(lines)\n    batch_write_time = time.time() - start_time\n    print(f&quot;\u6279\u91cf\u5199\u5165\u8017\u65f6: {batch_write_time:.4f} \u79d2&quot;)\n    \n    # \u4e00\u6b21\u5199\u5165\n    start_time = time.time()\n    with open(&quot;single_write.txt&quot;, &quot;w&quot;) as f:\n        f.write(&quot;&quot;.join(lines))\n    single_write_time = time.time() - start_time\n    print(f&quot;\u4e00\u6b21\u5199\u5165\u8017\u65f6: {single_write_time:.4f} \u79d2&quot;)\n\ntest_write_methods()\n\n# 3. \u5185\u5b58\u6620\u5c04\u4f18\u5316\nprint(&quot;\/n\u5185\u5b58\u6620\u5c04\u4f18\u5316\u793a\u4f8b\uff1a&quot;)\n\n# \u6d4b\u8bd5\u5185\u5b58\u6620\u5c04\u4e0e\u666e\u901a\u8bfb\u53d6\u7684\u6027\u80fd\ndef test_mmap_performance():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u5185\u5b58\u6620\u5c04\u7684\u6027\u80fd&quot;&quot;&quot;\n    # \u666e\u901a\u8bfb\u53d6\n    start_time = time.time()\n    with open(test_file, &quot;r&quot;) as f:\n        content = f.read()\n    normal_read_time = time.time() - start_time\n    print(f&quot;\u666e\u901a\u8bfb\u53d6\u8017\u65f6: {normal_read_time:.4f} \u79d2&quot;)\n    \n    # \u5185\u5b58\u6620\u5c04\u8bfb\u53d6\n    start_time = time.time()\n    with open(test_file, &quot;r&quot;) as f:\n        with mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) as mm:\n            content = mm.read()\n    mmap_read_time = time.time() - start_time\n    print(f&quot;\u5185\u5b58\u6620\u5c04\u8bfb\u53d6\u8017\u65f6: {mmap_read_time:.4f} \u79d2&quot;)\n\ntest_mmap_performance()\n\n# 4. \u5e76\u884cI\/O\u4f18\u5316\nprint(&quot;\/n\u5e76\u884cI\/O\u4f18\u5316\u793a\u4f8b\uff1a&quot;)\n\n# \u521b\u5efa\u591a\u4e2a\u6d4b\u8bd5\u6587\u4ef6\ndef create_test_files():\n    &quot;&quot;&quot;\u521b\u5efa\u591a\u4e2a\u6d4b\u8bd5\u6587\u4ef6&quot;&quot;&quot;\n    for i in range(5):\n        file_name = f&quot;test_file_{i}.txt&quot;\n        with open(file_name, &quot;w&quot;) as f:\n            for j in range(20000):\n                f.write(f&quot;File {i}, Line {j}: This is a test line.\/n&quot;)\n    print(&quot;\u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6\u6210\u529f&quot;)\n\ncreate_test_files()\n\n# \u6d4b\u8bd5\u4e32\u884c\u8bfb\u53d6\u4e0e\u5e76\u884c\u8bfb\u53d6\u7684\u6027\u80fd\ndef read_file(file_name):\n    &quot;&quot;&quot;\u8bfb\u53d6\u6587\u4ef6&quot;&quot;&quot;\n    with open(file_name, &quot;r&quot;) as f:\n        content = f.read()\n    return len(content)\n\ndef test_parallel_read():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u5e76\u884c\u8bfb\u53d6\u7684\u6027\u80fd&quot;&quot;&quot;\n    file_names = [f&quot;test_file_{i}.txt&quot; for i in range(5)]\n    \n    # \u4e32\u884c\u8bfb\u53d6\n    start_time = time.time()\n    for file_name in file_names:\n        read_file(file_name)\n    serial_time = time.time() - start_time\n    print(f&quot;\u4e32\u884c\u8bfb\u53d6\u8017\u65f6: {serial_time:.4f} \u79d2&quot;)\n    \n    # \u5e76\u884c\u8bfb\u53d6\n    start_time = time.time()\n    with concurrent.futures.ThreadPoolExecutor() as executor:\n        executor.map(read_file, file_names)\n    parallel_time = time.time() - start_time\n    print(f&quot;\u5e76\u884c\u8bfb\u53d6\u8017\u65f6: {parallel_time:.4f} \u79d2&quot;)\n\ntest_parallel_read()\n\n# 5. \u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\nprint(&quot;\/n\u6e05\u7406\u6d4b\u8bd5\u6587\u4ef6\uff1a&quot;)\n\nfor file in [&quot;performance_test.txt&quot;, &quot;line_write.txt&quot;, &quot;batch_write.txt&quot;, &quot;single_write.txt&quot;]:\n    if os.path.exists(file):\n        os.remove(file)\n        print(f&quot;\u5220\u9664\u6587\u4ef6 {file}&quot;)\n\nfor i in range(5):\n    file_name = f&quot;test_file_{i}.txt&quot;\n    if os.path.exists(file_name):\n        os.remove(file_name)\n        print(f&quot;\u5220\u9664\u6587\u4ef6 {file_name}&quot;)<\/code><\/pre>\n<h3>8. \u603b\u7ed3<\/h3>\n<p>\u672c\u6587\u8be6\u7ec6\u5206\u6790\u4e86Python\u4e2d\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0e\u7f13\u51b2\u7b56\u7565\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5<\/strong>\uff1a\u6587\u4ef6\u7684\u5b9a\u4e49\u3001\u7c7b\u578b\u3001\u64cd\u4f5c\u6a21\u5f0f<\/li>\n<li><strong>\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u57fa\u672c\u65b9\u6cd5<\/strong>\uff1a\u8bfb\u53d6\u3001\u5199\u5165\u3001\u6587\u4ef6\u6307\u9488\u64cd\u4f5c<\/li>\n<li><strong>\u7f13\u51b2\u7b56\u7565<\/strong>\uff1a\u7f13\u51b2\u7684\u6982\u5ff5\u3001\u6a21\u5f0f\u3001\u5927\u5c0f\u3001\u63a7\u5236<\/li>\n<li><strong>\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c<\/strong>\uff1aos\u6a21\u5757\u3001shutil\u6a21\u5757\u3001pathlib\u6a21\u5757<\/li>\n<li><strong>\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u6307\u5b9a\u7f16\u7801\u3001\u5904\u7406\u5927\u6587\u4ef6\u3001\u9519\u8bef\u5904\u7406\u3001\u6587\u4ef6\u8def\u5f84\u5904\u7406\u3001\u7f13\u51b2\u533a\u7ba1\u7406<\/li>\n<li><strong>\u9ad8\u7ea7\u6587\u4ef6I\/O\u64cd\u4f5c<\/strong>\uff1a\u4e34\u65f6\u6587\u4ef6\u3001\u5185\u5b58\u6587\u4ef6\u5bf9\u8c61\u3001\u538b\u7f29\u6587\u4ef6\u3001\u6587\u4ef6\u9501<\/li>\n<li><strong>\u5e38\u89c1\u6587\u4ef6I\/O\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u9519\u8bef\u3001\u7f16\u7801\u9519\u8bef\u3001\u5185\u5b58\u4e0d\u8db3\u3001\u8def\u5f84\u9519\u8bef\u3001\u7f13\u51b2\u533a\u672a\u5237\u65b0<\/li>\n<li><strong>\u6587\u4ef6I\/O\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u5f71\u54cd\u56e0\u7d20\u3001\u4f18\u5316\u65b9\u6cd5\u3001\u6027\u80fd\u6d4b\u8bd5<\/li>\n<\/ul>\n<p>Python\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u5904\u7406\u5404\u79cd\u6587\u4ef6\u7c7b\u578b\u548c\u683c\u5f0f\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u80fd\u591f\uff1a<\/p>\n<ol>\n<li>\u7406\u89e3\u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5\u548c\u64cd\u4f5c\u6a21\u5f0f<\/li>\n<li>\u638c\u63e1\u6587\u4ef6\u8bfb\u5199\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7<\/li>\n<li>\u7406\u89e3\u7f13\u51b2\u7b56\u7565\u7684\u5de5\u4f5c\u539f\u7406\u548c\u5e94\u7528<\/li>\n<li>\u719f\u7ec3\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u7684\u5404\u79cd\u5de5\u5177<\/li>\n<li>\u9075\u5faa\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>\u89e3\u51b3\u5e38\u89c1\u7684\u6587\u4ef6I\/O\u95ee\u9898<\/li>\n<li>\u4f18\u5316\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6027\u80fd<\/li>\n<\/ol>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u548c\u7f13\u51b2\u7b56\u7565\uff0c\u9075\u5faaPython\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002\u540c\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u4fdd\u6301\u5b66\u4e60\u7684\u6001\u5ea6\uff0c\u5173\u6ce8Python\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<h3>9. \u53c2\u8003\u6587\u732e<\/h3>\n<ol>\n<li>Python Documentation: Reading and Writing Files<\/li>\n<li>Python Documentation: os \u2014 Miscellaneous operating system interfaces<\/li>\n<li>Python Documentation: shutil \u2014 High-level file operations<\/li>\n<li>Python Documentation: pathlib \u2014 Object-oriented filesystem paths<\/li>\n<li>Python Documentation: tempfile \u2014 Generate temporary files and directories<\/li>\n<li>Python Documentation: io \u2014 Core tools for working with streams<\/li>\n<li>Python Documentation: gzip \u2014 Support for gzip files<\/li>\n<li>Real Python: Reading and Writing Files in Python<\/li>\n<li>Real Python: Working With Files in Python<\/li>\n<li>Real Python: Python&#8217;s tempfile Module<\/li>\n<\/ol>\n<h3>10. \u7ed3\u8bed<\/h3>\n<p>Python\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u662fPython\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86Python\u6587\u4ef6I\/O\u64cd\u4f5c\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u3002<\/p>\n<p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\uff1a<\/p>\n<ul>\n<li><strong>\u6b63\u786e\u7406\u89e3\u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5<\/strong>\uff1a\u4e86\u89e3\u6587\u4ef6\u7684\u7c7b\u578b\u3001\u64cd\u4f5c\u6a21\u5f0f\u548c\u57fa\u672c\u65b9\u6cd5<\/li>\n<li><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\uff1a\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6\u7684\u6b63\u786e\u5173\u95ed<\/li>\n<li><strong>\u6307\u5b9a\u7f16\u7801<\/strong>\uff1a\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\u663e\u5f0f\u6307\u5b9a\u7f16\u7801<\/li>\n<li><strong>\u5904\u7406\u5927\u6587\u4ef6\u65f6\u6ce8\u610f\u5185\u5b58\u4f7f\u7528<\/strong>\uff1a\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6<\/li>\n<li><strong>\u6dfb\u52a0\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7\u548c\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38<\/li>\n<li><strong>\u4f7f\u7528\u5408\u9002\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>os.path<\/code>\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/li>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u7b56\u7565<\/strong>\uff1a\u6839\u636e\u6587\u4ef6\u64cd\u4f5c\u7684\u7279\u70b9\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u6a21\u5f0f\u548c\u5927\u5c0f<\/li>\n<li><strong>\u4f18\u5316\u6587\u4ef6I\/O\u6027\u80fd<\/strong>\uff1a\u6839\u636e\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u4f18\u5316\u65b9\u6cd5<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u539f\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u6587\u4ef6I\/O\u529f\u80fd\uff0c\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u3001\u9ad8\u6548\u548c\u53ef\u7ef4\u62a4\u7684Python\u4ee3\u7801\u3002\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0d\u4ec5\u662f\u4e00\u79cd\u57fa\u672c\u7684\u7f16\u7a0b\u6280\u80fd\uff0c\u66f4\u662f\u4e00\u79cd\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5728\u6570\u636e\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u914d\u7f6e\u7ba1\u7406\u7b49\u65b9\u9762\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3Python\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0e\u7f13\u51b2\u7b56\u7565\uff0c\u638c\u63e1\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7f16\u5199\u51fa\u66f4\u9ad8\u8d28\u91cf\u7684Python\u4ee3\u7801\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585261\">https:\/\/segmentfault.com\/a\/1190000047585261<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u6587\u4ef6I\/O\u64cd\u4f5c\u4e0e\u7f13\u51b2\u7b56\u7565 1. \u6587\u4ef6I\/O\u7684\u57fa\u672c\u6982\u5ff5 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6587\u4ef6I\/O\uff08\u8f93\u5165\/ [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021228","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021228"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021228\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}