{"id":930203,"date":"2024-12-26T17:11:10","date_gmt":"2024-12-26T09:11:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/930203.html"},"modified":"2024-12-26T17:11:12","modified_gmt":"2024-12-26T09:11:12","slug":"python%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%bafile","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/930203.html","title":{"rendered":"python\u5982\u4f55\u65b0\u5efafile"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065055\/00aa720f-dedb-401e-9a89-01fd17132e96.webp\" alt=\"python\u5982\u4f55\u65b0\u5efafile\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u65b0\u5efa\u6587\u4ef6\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>pathlib<\/code>\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7\u7b2c\u4e09\u65b9\u5e93\u5982<code>os<\/code>\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\uff0c\u901a\u8fc7\u8c03\u7528<code>open(&#39;filename&#39;, &#39;w&#39;)<\/code>\u3001<code>\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6<\/code>\u3001<code>\u5e76\u53ef\u4ee5\u7acb\u5373\u5bf9\u5176\u8fdb\u884c\u5199\u5165\u64cd\u4f5c<\/code>\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\u53ef\u4ee5\u4f7f\u7528<code>with open(&#39;filename&#39;, &#39;w&#39;) as file<\/code>\u7684\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u5173\u95ed\u7684\u95ee\u9898\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>open()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u6a21\u5f0f\u6765\u5b9e\u73b0\u6587\u4ef6\u7684\u521b\u5efa\u4e0e\u64cd\u4f5c\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u521b\u5efa\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5c06\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u521b\u5efa\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u65b0\u7684\u5185\u5bb9\u5c06\u88ab\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/li>\n<li><code>&#39;x&#39;<\/code>\uff1a\u72ec\u5360\u521b\u5efa\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u64cd\u4f5c\u5c06\u5931\u8d25\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 &#39;w&#39; \u6a21\u5f0f\u521b\u5efa\u65b0\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/p>\n<p>with open(&#39;newfile.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\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u51fa\u73b0\u6587\u4ef6\u672a\u5173\u95ed\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>pathlib<\/code>\u5e93\u521b\u5efa\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u76f4\u89c2\u548c\u73b0\u4ee3\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = Path(&#39;newfile.txt&#39;)<\/p>\n<p>file_path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u7684<code>Path<\/code>\u5bf9\u8c61\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u5404\u79cd\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\uff0c\u4f8b\u5982\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3001\u521b\u5efa\u76ee\u5f55\u7b49\u3002\u5176\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u521b\u5efa\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u6587\u4ef6\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &#39;newfile.txt&#39;<\/p>\n<p>if not os.path.exists(file_path):<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>os.path.exists()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u4ef6\u4e4b\u524d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff0c\u4ece\u800c\u907f\u514d\u8986\u76d6\u5df2\u6709\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6587\u4ef6\u521b\u5efa\u8fc7\u7a0b\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u8def\u5f84<\/strong>\uff1a\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u3002\u5982\u679c\u8981\u5728\u7279\u5b9a\u76ee\u5f55\u4e0b\u521b\u5efa\u6587\u4ef6\uff0c\u9700\u8981\u63d0\u4f9b\u5b8c\u6574\u7684\u8def\u5f84\u3002<\/li>\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u6587\u4ef6\u521b\u5efa\u65f6\u7684\u6743\u9650\u95ee\u9898\u3002\u786e\u4fddPython\u811a\u672c\u6709\u8db3\u591f\u7684\u6743\u9650\u5728\u76ee\u6807\u76ee\u5f55\u4e0b\u521b\u5efa\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u3001\u6587\u4ef6\u8def\u5f84\u65e0\u6548\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efa\u6587\u4ef6\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3001<code>pathlib<\/code>\u5e93\u8fd8\u662f<code>os<\/code>\u6a21\u5757\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u548c\u9ad8\u6548\u3002\u5728\u7f16\u5199\u6587\u4ef6\u64cd\u4f5c\u4ee3\u7801\u65f6\uff0c\u59cb\u7ec8\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u8def\u5f84\u7684\u6b63\u786e\u6027\u3001\u6743\u9650\u95ee\u9898\u4ee5\u53ca\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7a33\u5b9a\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\u7684\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\u4ef6\u3002\u53ea\u9700\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\uff0c\u5e76\u6307\u5b9a\u6a21\u5f0f\u4e3a<code>&#39;w&#39;<\/code>\uff08\u5199\u5165\u6a21\u5f0f\uff09\u6216<code>&#39;x&#39;<\/code>\uff08\u72ec\u5360\u521b\u5efa\u6a21\u5f0f\uff09\u3002\u4f8b\u5982\uff0c<code>open(&#39;newfile.txt&#39;, &#39;w&#39;)<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>newfile.txt<\/code>\u7684\u65b0\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5199\u5165\u6a21\u5f0f\u4f1a\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\uff0c\u800c\u72ec\u5360\u521b\u5efa\u6a21\u5f0f\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u521b\u5efa\u6587\u4ef6\u65f6\u53ef\u4ee5\u6307\u5b9a\u54ea\u4e9b\u53c2\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u5916\uff0c<code>open()<\/code>\u51fd\u6570\u8fd8\u652f\u6301\u5176\u4ed6\u53c2\u6570\uff0c\u5982<code>encoding<\/code>\u548c<code>newline<\/code>\u3002<code>encoding<\/code>\u53c2\u6570\u5141\u8bb8\u60a8\u6307\u5b9a\u6587\u4ef6\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4f8b\u5982<code>&#39;utf-8&#39;<\/code>\uff0c\u800c<code>newline<\/code>\u5219\u7528\u4e8e\u63a7\u5236\u6362\u884c\u7b26\u7684\u5904\u7406\u3002\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u6587\u4ef6\u7684\u521b\u5efa\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5199\u5165\u6570\u636e\u5230\u65b0\u521b\u5efa\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u8c03\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>write()<\/code>\u65b9\u6cd5\u5e76\u4f20\u5165\u8981\u5199\u5165\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;newfile.txt&#39;, &#39;w&#39;) as file:  \n    file.write(&#39;Hello, World!&#39;)  \n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u5199\u5165\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u548c\u6587\u4ef6\u635f\u574f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u65b0\u5efa\u6587\u4ef6\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u4f7f\u7528pathlib\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7\u7b2c\u4e09 [&hellip;]","protected":false},"author":3,"featured_media":930209,"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\/930203"}],"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=930203"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/930203\/revisions"}],"predecessor-version":[{"id":930210,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/930203\/revisions\/930210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/930209"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=930203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=930203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=930203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}