{"id":1075074,"date":"2025-01-08T11:40:37","date_gmt":"2025-01-08T03:40:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075074.html"},"modified":"2025-01-08T11:40:39","modified_gmt":"2025-01-08T03:40:39","slug":"python%e5%a6%82%e4%bd%95%e6%96%b0%e5%81%9a%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075074.html","title":{"rendered":"python\u5982\u4f55\u65b0\u505a\u4e00\u4e2a\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104535\/28d90cbf-5c91-474f-81ff-5fb46cca3527.webp\" alt=\"python\u5982\u4f55\u65b0\u505a\u4e00\u4e2a\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code>\u8bed\u53e5\uff09\u6765\u5b9e\u73b0\u3001\u6307\u5b9a\u6587\u4ef6\u6a21\u5f0f\u7b49\u65b9\u5f0f\u6765\u5b8c\u6210\u3002\u63a8\u8350\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002<code>open()<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u6a21\u5f0f\u3002\u6587\u4ef6\u6a21\u5f0f\u5305\u62ec\u8bfb\uff08<code>r<\/code>\uff09\u3001\u5199\uff08<code>w<\/code>\uff09\u3001\u8ffd\u52a0\uff08<code>a<\/code>\uff09\u7b49\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c<code>w<\/code>\u6a21\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002\u4e3a\u4e86\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u80fd\u591f\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code>\u8bed\u53e5\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;new_file.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;new_file.txt&#39;<\/code>\u662f\u6587\u4ef6\u540d\uff0c<code>&#39;w&#39;<\/code>\u662f\u5199\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u8986\u76d6\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001<code>open()<\/code>\u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p><code>open()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u6765\u6253\u5f00\u6587\u4ef6\u7684\u5185\u7f6e\u51fd\u6570\u3002\u5b83\u6709\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u6a21\u5f0f\u3002\u6587\u4ef6\u6a21\u5f0f\u53ef\u4ee5\u662f\u8bfb\u53d6\u6a21\u5f0f\uff08<code>r<\/code>\uff09\u3001\u5199\u5165\u6a21\u5f0f\uff08<code>w<\/code>\uff09\u3001\u8ffd\u52a0\u6a21\u5f0f\uff08<code>a<\/code>\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6587\u4ef6\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bfb\u53d6\u6a21\u5f0f\uff08<code>r<\/code>\uff09<\/strong>\uff1a\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>FileNotFoundError<\/code>\u3002<\/li>\n<li><strong>\u5199\u5165\u6a21\u5f0f\uff08<code>w<\/code>\uff09<\/strong>\uff1a\u4ee5\u5199\u5165\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\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\u4f1a\u8986\u76d6\u6587\u4ef6\u3002<\/li>\n<li><strong>\u8ffd\u52a0\u6a21\u5f0f\uff08<code>a<\/code>\uff09<\/strong>\uff1a\u4ee5\u8ffd\u52a0\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\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\u5199\u5165\u7684\u6570\u636e\u4f1a\u88ab\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/li>\n<\/ul>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>example.txt<\/code>\u662f\u6587\u4ef6\u540d\uff0c<code>w<\/code>\u662f\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u521b\u5efa\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u8986\u76d6\u3002<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u80fd\u591f\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code>\u8bed\u53e5\uff09<\/h2>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u88ab\u6b63\u786e\u5173\u95ed\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<code>with<\/code>\u8bed\u53e5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u90fd\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\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;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u80fd\u591f\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u5373\u4f7f\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u6587\u4ef6\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u800c\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/p>\n<p>with open(&#39;\/home\/user\/example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u8def\u5f84\uff0c<code>with<\/code>\u8bed\u53e5\u90fd\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6587\u4ef6\u6743\u9650\u548c\u9519\u8bef\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u6587\u4ef6\u6743\u9650\u548c\u9519\u8bef\u5904\u7406\u4e5f\u662f\u9700\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002\u6587\u4ef6\u6743\u9650\u51b3\u5b9a\u4e86\u8c01\u53ef\u4ee5\u8bbf\u95ee\u6216\u4fee\u6539\u6587\u4ef6\u3002\u9519\u8bef\u5904\u7406\u53ef\u4ee5\u786e\u4fdd\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u6587\u4ef6\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u6743\u9650\u7531\u8bfb\u3001\u5199\u3001\u6267\u884c\u6743\u9650\u7ec4\u6210\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.chmod()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u4e3a\u53ea\u8bfb<\/strong><\/h2>\n<p>os.chmod(&#39;example.txt&#39;, 0o444)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>os.chmod()<\/code>\u51fd\u6570\u5c06<code>example.txt<\/code>\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;This is an example file.&#39;)<\/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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>try...except<\/code>\u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406\u4e86\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u65e0\u8bba\u53d1\u751f\u4f55\u79cd\u5f02\u5e38\uff0c\u7a0b\u5e8f\u90fd\u80fd\u591f\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5199\u5165\u591a\u884c\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u6216<code>writelines()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 write() \u65b9\u6cd5\u5199\u5165\u591a\u884c\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Line 1\\n&#39;)<\/p>\n<p>    file.write(&#39;Line 2\\n&#39;)<\/p>\n<p>    file.write(&#39;Line 3\\n&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 writelines() \u65b9\u6cd5\u5199\u5165\u591a\u884c\u5185\u5bb9<\/strong><\/h2>\n<p>lines = [&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;, &#39;Line 3\\n&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>write()<\/code>\u65b9\u6cd5\u9010\u884c\u5199\u5165\u5185\u5bb9\uff0c<code>writelines()<\/code>\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u7279\u522b\u662f\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 UTF-8 \u7f16\u7801\u521b\u5efa\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;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u4ef6\u3002&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 ASCII \u7f16\u7801\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;ascii&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u3002UTF-8\u7f16\u7801\u652f\u6301\u6240\u6709Unicode\u5b57\u7b26\uff0c\u800cASCII\u7f16\u7801\u53ea\u652f\u6301\u57fa\u672c\u7684\u62c9\u4e01\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u4e0e<code>os.path<\/code>\u4e0d\u540c\uff0c<code>pathlib<\/code>\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\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>with path.open(&#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>pathlib.Path<\/code>\u5bf9\u8c61\u8868\u793a\u6587\u4ef6\u8def\u5f84\uff0c<code>open()<\/code>\u65b9\u6cd5\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u517c\u5bb9\u6027<\/h2>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u6587\u4ef6\u8def\u5f84\u7684\u8868\u793a\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002\u5728Windows\u4e0a\uff0c\u8def\u5f84\u4f7f\u7528\u53cd\u659c\u6760\uff08<code><\/code>\uff09\uff0c\u5728Unix\/Linux\u4e0a\uff0c\u8def\u5f84\u4f7f\u7528\u6b63\u659c\u6760\uff08<code>\/<\/code>\uff09\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.getcwd()<\/p>\n<h2><strong>\u751f\u6210\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_dir, &#39;example.txt&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;This is an example file.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>os.path.join()<\/code>\u51fd\u6570\u751f\u6210\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code>\u8bed\u53e5\uff09\u3001\u6307\u5b9a\u6587\u4ef6\u6a21\u5f0f\u7b49\u65b9\u5f0f\u6765\u5b8c\u6210\u3002\u786e\u4fdd\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3001\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u548c\u7f16\u7801\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u7b49\u90fd\u662f\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u6b63\u786e\u6027\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\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\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u65b0\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0c\u7ed3\u5408\u6587\u4ef6\u6a21\u5f0f<code>&#39;w&#39;<\/code>\u6216<code>&#39;x&#39;<\/code>\u3002\u6a21\u5f0f<code>&#39;w&#39;<\/code>\u4f1a\u521b\u5efa\u6587\u4ef6\u5e76\u6253\u5f00\u4ee5\u4f9b\u5199\u5165\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\u5219\u4f1a\u8986\u76d6\u3002\u800c\u6a21\u5f0f<code>&#39;x&#39;<\/code>\u5219\u53ea\u4f1a\u5728\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\u521b\u5efa\u6587\u4ef6\uff0c\u82e5\u6587\u4ef6\u5df2\u5b58\u5728\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;new_file.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002&#39;)\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u5728Python\u4e2d\u521b\u5efa\u54ea\u4e9b\u7c7b\u578b\u7684\u6587\u4ef6\uff1f<\/strong><br \/>Python\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efa\u6587\u672c\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>&#39;wb&#39;<\/code>\u6a21\u5f0f\u521b\u5efa\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528\u7279\u5b9a\u5e93\u521b\u5efa\u5982CSV\u3001JSON\u7b49\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u521b\u5efaCSV\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nwith open(&#39;data.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:\n    writer = csv.writer(csvfile)\n    writer.writerow([&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;])\n    writer.writerow([&#39;\u5f20\u4e09&#39;, 25])\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u65b0\u6587\u4ef6\u4e4b\u524d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u8be5\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u5b58\u5728\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\u5b58\u5728\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;new_file.txt&#39;\nif not os.path.exists(file_path):\n    with open(file_path, &#39;w&#39;) as file:\n        file.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002&#39;)\nelse:\n    print(&#39;\u6587\u4ef6\u5df2\u5b58\u5728\u3002&#39;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8bed\u53e5\uff09\u6765\u5b9e\u73b0\u3001\u6307\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":1075082,"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\/1075074"}],"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=1075074"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075074\/revisions"}],"predecessor-version":[{"id":1075083,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075074\/revisions\/1075083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075082"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}