{"id":1126280,"date":"2025-01-08T19:59:38","date_gmt":"2025-01-08T11:59:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126280.html"},"modified":"2025-01-08T19:59:40","modified_gmt":"2025-01-08T11:59:40","slug":"python%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126280.html","title":{"rendered":"python\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090636\/e65a7761-5354-4246-939d-28bf7a048e32.webp\" alt=\"python\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u65b0\u5efa\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3001<code>Path<\/code>\u5bf9\u8c61\u6216\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>pathlib<\/code>\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>open<\/code>\u51fd\u6570<\/strong>\u3002\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528<code>open<\/code>\u51fd\u6570\uff1a<\/strong><\/p>\n<p><code>open<\/code>\u51fd\u6570\u662fPython\u4e2d\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002\u8be5\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u6a21\u5f0f\uff0c\u82e5\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u82e5\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u7a7a\u5185\u5bb9\uff1b<\/li>\n<li><code>&#39;x&#39;<\/code>\uff1a\u6392\u4ed6\u6027\u521b\u5efa\u6a21\u5f0f\uff0c\u82e5\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u82e5\u6587\u4ef6\u5b58\u5728\u5219\u5f15\u53d1\u5f02\u5e38\uff1b<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff0c\u82e5\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u82e5\u6587\u4ef6\u5b58\u5728\u5219\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><p>\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 &#39;w&#39; \u6a21\u5f0f\u521b\u5efa\u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 &#39;x&#39; \u6a21\u5f0f\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;x&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 &#39;a&#39; \u6a21\u5f0f\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\uff1a<\/strong><\/p>\n<p><code>Path<\/code>\u5bf9\u8c61\u662f<code>pathlib<\/code>\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002<code>pathlib<\/code>\u5e93\u5728Python 3.4\u4e2d\u5f15\u5165\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>path = Path(&#39;example.txt&#39;)<\/p>\n<p>path.write_text(&#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if not path.exists():<\/p>\n<p>    path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1a<\/strong><\/p>\n<p>\u9664\u4e86\u5185\u7f6e\u51fd\u6570\u548c\u6807\u51c6\u5e93\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u521b\u5efa\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>os<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>filename = &#39;example.txt&#39;<\/p>\n<p>if not os.path.exists(filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u65b0\u5efa\u6587\u672c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>open<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u4e14\u5e38\u7528\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3002\u901a\u8fc7\u6307\u5b9a\u4e0d\u540c\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u6587\u4ef6\u7684\u8bfb\u5199\u3001\u521b\u5efa\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528\u5199\u6a21\u5f0f <code>&#39;w&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u6a21\u5f0f\u4e3a <code>&#39;w&#39;<\/code> \u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c<code>&#39;w&#39;<\/code> \u6a21\u5f0f\u4f1a\u521b\u5efa\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5b83\u4f1a\u8986\u76d6\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>&#39;example.txt&#39;<\/code> \u662f\u6587\u4ef6\u540d\uff0c<code>&#39;w&#39;<\/code> \u662f\u5199\u6a21\u5f0f\u3002<code>with<\/code> \u8bed\u53e5\u7528\u4e8e\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u6392\u4ed6\u6027\u521b\u5efa\u6a21\u5f0f <code>&#39;x&#39;<\/code><\/h4>\n<\/p>\n<p><p><code>&#39;x&#39;<\/code> \u6a21\u5f0f\u7528\u4e8e\u5728\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\u521b\u5efa\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>FileExistsError<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;x&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>except FileExistsError:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u6a21\u5f0f\u5728\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u4e0d\u88ab\u8986\u76d6\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f <code>&#39;a&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f <code>&#39;a&#39;<\/code> \u7528\u4e8e\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u521b\u5efa\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5b83\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u8986\u76d6\u539f\u6709\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, again!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u6a21\u5f0f\u5728\u9700\u8981\u4e0d\u65ad\u5411\u6587\u4ef6\u4e2d\u8ffd\u52a0\u6570\u636e\u65f6\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u65b0\u5efa\u6587\u672c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u4e14\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u5f0f\u3002<code>Path<\/code>\u5bf9\u8c61\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&#39;example.txt&#39;)<\/p>\n<p>path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Path<\/code>\u5bf9\u8c61\u8868\u793a\u6587\u4ef6\u8def\u5f84\uff0c<code>write_text<\/code>\u65b9\u6cd5\u7528\u4e8e\u5199\u5165\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>exists<\/code>\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&#39;example.txt&#39;)<\/p>\n<p>if not path.exists():<\/p>\n<p>    path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u610f\u5916\u8986\u76d6\u5df2\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u65b0\u5efa\u6587\u672c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u51fd\u6570\u548c\u6807\u51c6\u5e93\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>os<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4f4e\u7ea7\u522b\u7684\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528<code>os<\/code>\u5e93\u521b\u5efa\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5e76\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>filename = &#39;example.txt&#39;<\/p>\n<p>if not os.path.exists(filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7ed3\u5408\u4e86<code>os<\/code>\u5e93\u7684\u6587\u4ef6\u5b58\u5728\u68c0\u67e5\u529f\u80fd\u548c<code>open<\/code>\u51fd\u6570\u7684\u6587\u4ef6\u521b\u5efa\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5b9e\u4f8b\uff1a\u521b\u5efa\u5e76\u5199\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e\u53ef\u80fd\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u5185\u5bb9\uff0c\u7136\u540e\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u521b\u5efa\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u5185\u5bb9\uff0c\u5e76\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filename = input(&#39;Enter the filename: &#39;)<\/p>\n<p>content = input(&#39;Enter the content: &#39;)<\/p>\n<p>if not os.path.exists(filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(content)<\/p>\n<p>    print(f&#39;File &quot;{filename}&quot; created successfully.&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;File already exists.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u6587\u4ef6\u540d\u548c\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u8986\u76d6\u5df2\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u64cd\u4f5c\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_file(filename, content):<\/p>\n<p>    if not os.path.exists(filename):<\/p>\n<p>        with open(filename, &#39;w&#39;) as file:<\/p>\n<p>            file.write(content)<\/p>\n<p>        print(f&#39;File &quot;{filename}&quot; created successfully.&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(&#39;File already exists.&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>filename = input(&#39;Enter the filename: &#39;)<\/p>\n<p>content = input(&#39;Enter the content: &#39;)<\/p>\n<p>create_file(filename, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u590d\u7528\u6587\u4ef6\u521b\u5efa\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4e3a\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u5e76\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_file(filename, content):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;w&#39;) as file:<\/p>\n<p>            file.write(content)<\/p>\n<p>        print(f&#39;File &quot;{filename}&quot; created successfully.&#39;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&#39;An error occurred: {e}&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>filename = input(&#39;Enter the filename: &#39;)<\/p>\n<p>content = input(&#39;Enter the content: &#39;)<\/p>\n<p>create_file(filename, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u8ddf\u8e2a\u7a0b\u5e8f\u7684\u6267\u884c\u60c5\u51b5\u548c\u6392\u67e5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u5e93\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;file_operations.log&#39;, level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>def create_file(filename, content):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;w&#39;) as file:<\/p>\n<p>            file.write(content)<\/p>\n<p>        logging.info(f&#39;File &quot;{filename}&quot; created successfully.&#39;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&#39;An error occurred: {e}&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>filename = input(&#39;Enter the filename: &#39;)<\/p>\n<p>content = input(&#39;Enter the content: &#39;)<\/p>\n<p>create_file(filename, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bb0\u5f55\u65e5\u5fd7\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u548c\u5f02\u5e38\u60c5\u51b5\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u907f\u514d\u56e0\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u5904\u7406\u6587\u4ef6\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216<code>pathlib<\/code>\u5e93\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u907f\u514d\u8def\u5f84\u6df7\u6dc6\u548c\u8de8\u5e73\u53f0\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&#39;\/path\/to\/example.txt&#39;)<\/p>\n<p>path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u7b80\u5316\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>6.3 \u68c0\u67e5\u6587\u4ef6\u6743\u9650<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u524d\uff0c\u5efa\u8bae\u68c0\u67e5\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\uff0c\u907f\u514d\u56e0\u6743\u9650\u4e0d\u8db3\u800c\u5bfc\u81f4\u7684\u64cd\u4f5c\u5931\u8d25\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>filename = &#39;example.txt&#39;<\/p>\n<p>if os.access(filename, os.W_OK):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Permission denied.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u6587\u4ef6\u6743\u9650\uff0c\u53ef\u4ee5\u63d0\u524d\u53d1\u73b0\u5e76\u5904\u7406\u6743\u9650\u95ee\u9898\uff0c\u907f\u514d\u7a0b\u5e8f\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b0\u5efa\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3001<code>pathlib<\/code>\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u8981\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6<\/strong>\uff0c\u5e38\u7528\u6a21\u5f0f\u5305\u62ec<code>&#39;w&#39;<\/code>\u3001<code>&#39;x&#39;<\/code>\u548c<code>&#39;a&#39;<\/code>\uff1b<\/li>\n<li><strong><code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406\u65b9\u5f0f<\/strong>\uff0c\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\uff1b<\/li>\n<li><strong>\u7ed3\u5408<code>os<\/code>\u5e93\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c<\/strong>\uff0c\u4f8b\u5982\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u548c\u6743\u9650\u68c0\u67e5\uff1b<\/li>\n<li><strong>\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027<\/strong>\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u7ef4\u62a4\uff1b<\/li>\n<li><strong>\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u548c\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><p>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\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\u6587\u672c\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\u6587\u672c\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u662f\u4f7f\u7528<code>&#39;w&#39;<\/code>\u6216<code>&#39;x&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6307\u5b9a\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c<code>&#39;w&#39;<\/code>\u6a21\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff0c\u800c<code>&#39;x&#39;<\/code>\u6a21\u5f0f\u5219\u4f1a\u5728\u6587\u4ef6\u5df2\u5b58\u5728\u65f6\u5f15\u53d1\u9519\u8bef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;newfile.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>newfile.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4e00\u884c\u6587\u672c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u6587\u672c\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u65b0\u7684\u6587\u672c\u6587\u4ef6\u4e4b\u524d\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path.exists()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_name = &#39;newfile.txt&#39;\nif not os.path.exists(file_name):\n    with open(file_name, &#39;w&#39;) as file:\n        file.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002&#39;)\nelse:\n    print(f&#39;{file_name} \u5df2\u7ecf\u5b58\u5728\u3002&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u8986\u76d6\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u73b0\u6709\u6587\u672c\u6587\u4ef6\u8ffd\u52a0\u5185\u5bb9\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u73b0\u6709\u6587\u672c\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&#39;a&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u65b0\u6570\u636e\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u539f\u6709\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;existingfile.txt&#39;, &#39;a&#39;) as file:\n    file.write(&#39;\\n\u8fd9\u662f\u8ffd\u52a0\u7684\u5185\u5bb9\u3002&#39;)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u5728<code>existingfile.txt<\/code>\u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c\u65b0\u6587\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u65b0\u5efa\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001Path\u5bf9\u8c61\u6216\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982pathlib\u3002\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1126284,"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\/1126280"}],"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=1126280"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126280\/revisions"}],"predecessor-version":[{"id":1126287,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126280\/revisions\/1126287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126284"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}