{"id":1076312,"date":"2025-01-08T11:52:12","date_gmt":"2025-01-08T03:52:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076312.html"},"modified":"2025-01-08T11:52:15","modified_gmt":"2025-01-08T03:52:15","slug":"python%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aa-txt%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076312.html","title":{"rendered":"python\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180942\/3f4684b8-d77e-4419-b1e5-4b5b693a7462.webp\" alt=\"python\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u65b0\u5efa\u5e76\u5199\u5165\u5185\u5bb9\u5230\u6587\u4ef6\u3001\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528open()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6307\u5b9a\u6a21\u5f0f\u6765\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u4f7f\u7528\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570\u65b0\u5efa\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u6765\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">open(filename, mode, encoding)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cfilename\u8868\u793a\u6587\u4ef6\u540d\uff0cmode\u8868\u793a\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\uff0cencoding\u8868\u793a\u6587\u4ef6\u7f16\u7801\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;w&#39;\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u4f1a\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li>&#39;a&#39;\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/li>\n<li>&#39;r&#39;\uff1a\u8bfb\u53d6\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u62a5\u9519\u3002<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528open()\u51fd\u6570\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>file.write(&#39;Hello, world!&#39;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528open()\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f(&#39;w&#39;)\u65b0\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4e86\u5b57\u7b26\u4e32&#39;Hello, world!&#39;\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528with\u8bed\u53e5\u6765\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\u3002with\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u56e0\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u9732\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cwith\u8bed\u53e5\u4f1a\u5728\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528close()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u591a\u884c\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528write()\u65b9\u6cd5\u591a\u6b21\uff0c\u6216\u8005\u4f7f\u7528writelines()\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8c03\u7528write()\u65b9\u6cd5\u591a\u6b21\u5199\u5165\u591a\u884c\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u591a\u884c\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a new line.\\n&#39;)<\/p>\n<p>    file.write(&#39;Python is great!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528writelines()\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6\uff0c\u5e76\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u5185\u5bb9<\/p>\n<p>lines = [&#39;Hello, world!\\n&#39;, &#39;This is a new line.\\n&#39;, &#39;Python is great!\\n&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5148\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684path.exists()\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u68c0\u67e5example.txt\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(&#39;example.txt&#39;):<\/p>\n<p>    print(&#39;File exists.&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File does not exist.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u5199\u5165\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\u539f\u6709\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00example.txt\u6587\u4ef6\uff0c\u5e76\u8ffd\u52a0\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;\\nThis is appended content.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5728\u65b0\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528\u8bfb\u53d6\u6a21\u5f0f(&#39;r&#39;)\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00example.txt\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0copen()\u51fd\u6570\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aencoding\u53c2\u6570\u6765\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aexample.txt\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528UTF-8\u7f16\u7801\u5199\u5165\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world! \u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;File not found.&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;Permission denied.&#39;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528with\u8bed\u53e5\u6765\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\uff0c\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002\u65e0\u8bba\u662f\u65b0\u5efa\u6587\u4ef6\u3001\u5199\u5165\u5185\u5bb9\u3001\u8ffd\u52a0\u5185\u5bb9\u8fd8\u662f\u8bfb\u53d6\u5185\u5bb9\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684.txt\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684.txt\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u4e3a&#39;w&#39;\uff08\u5199\u5165\u6a21\u5f0f\uff09\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0cPython\u5c06\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;new_file.txt&#39;, &#39;w&#39;) as file:\n    file.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>new_file.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4e00\u884c\u6587\u672c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u521b\u5efa.txt\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u521b\u5efa.txt\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u63d0\u4f9b\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8def\u5f84\uff0c\u6587\u4ef6\u5c06\u4f1a\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u521b\u5efa\u3002\u6b64\u5916\uff0c\u8981\u8003\u8651\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\uff0c\u786e\u4fdd\u7a0b\u5e8f\u6709\u6743\u9650\u5728\u6307\u5b9a\u76ee\u5f55\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u521b\u5efa.txt\u6587\u4ef6\u65f6\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u521b\u5efa.txt\u6587\u4ef6\u65f6\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\u5df2\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7684&#39;a&#39;\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;existing_file.txt&#39;, &#39;a&#39;) as file:\n    file.write(&quot;\u6dfb\u52a0\u7684\u65b0\u5185\u5bb9\u3002\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u65b0\u5185\u5bb9\u8ffd\u52a0\u5230<code>existing_file.txt<\/code>\u7684\u672b\u5c3e\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u539f\u6709\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5.txt\u6587\u4ef6\u662f\u5426\u6210\u529f\u521b\u5efa\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path.exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u521b\u5efa\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nif os.path.exists(&#39;new_file.txt&#39;):\n    print(&quot;\u6587\u4ef6\u521b\u5efa\u6210\u529f\uff01&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u521b\u5efa\u5931\u8d25\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u786e\u8ba4\u6587\u4ef6\u7684\u521b\u5efa\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6 \u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a.txt\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185 [&hellip;]","protected":false},"author":3,"featured_media":1076318,"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\/1076312"}],"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=1076312"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076312\/revisions"}],"predecessor-version":[{"id":1076322,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076312\/revisions\/1076322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076318"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}