{"id":1096335,"date":"2025-01-08T15:00:13","date_gmt":"2025-01-08T07:00:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096335.html"},"modified":"2025-01-08T15:00:15","modified_gmt":"2025-01-08T07:00:15","slug":"python%e6%96%b0%e5%bb%ba%e6%96%87%e4%bb%b6%e5%90%8d%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096335.html","title":{"rendered":"python\u65b0\u5efa\u6587\u4ef6\u540d\u5982\u4f55\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211608\/1a61febd-ec72-4f4d-a8ff-84b3085d6b23.webp\" alt=\"python\u65b0\u5efa\u6587\u4ef6\u540d\u5982\u4f55\u4fdd\u5b58\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u65b0\u5efa\u6587\u4ef6\u540d\u5e76\u4fdd\u5b58\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528 open() \u51fd\u6570\u3001\u4f7f\u7528 with \u8bed\u53e5\u3001\u4f7f\u7528 os \u6a21\u5757\u3001\u4f7f\u7528 pathlib \u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528 open() \u51fd\u6570<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 open() \u51fd\u6570\u6765\u65b0\u5efa\u6587\u4ef6\u540d\u5e76\u4fdd\u5b58\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 open() \u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 open() \u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 open() \u51fd\u6570\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c&#39;example.txt&#39; \u662f\u6587\u4ef6\u540d\uff0c&#39;w&#39; \u8868\u793a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u8986\u76d6\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5199\u5165\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 write() \u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6210\u5199\u5165\u64cd\u4f5c\u540e\uff0c\u52a1\u5fc5\u5173\u95ed\u6587\u4ef6\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 with \u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 with \u8bed\u53e5\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u8c03\u7528 close() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/strong><\/li>\n<\/ol>\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 with \u8bed\u53e5\u5757\u5185\uff0c\u4f1a\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 os \u6a21\u5757<\/h3>\n<\/p>\n<p><p>os \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u521b\u5efa\u76ee\u5f55\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>if not os.path.exists(&#39;example.txt&#39;):<\/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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 os.path.exists() \u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if not os.path.exists(&#39;new_directory&#39;):<\/p>\n<p>    os.makedirs(&#39;new_directory&#39;)<\/p>\n<p>with open(&#39;new_directory\/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>\u4f7f\u7528 os.makedirs() \u65b9\u6cd5\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\u7136\u540e\u5728\u65b0\u76ee\u5f55\u4e0b\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 pathlib \u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib \u6a21\u5757\u662f Python 3.4 \u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>file_path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 Path \u7c7b\u7684 write_text() \u65b9\u6cd5\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">directory_path = Path(&#39;new_directory&#39;)<\/p>\n<p>directory_path.mkdir(exist_ok=True)<\/p>\n<p>file_path = directory_path \/ &#39;example.txt&#39;<\/p>\n<p>file_path.write_text(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 Path \u7c7b\u7684 mkdir() \u65b9\u6cd5\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\/ \u64cd\u4f5c\u7b26\u7528\u4e8e\u8def\u5f84\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u65b0\u5efa\u6587\u4ef6\u540d\u5e76\u4fdd\u5b58\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u4f7f\u7528 open() \u51fd\u6570\u3001\u4f7f\u7528 with \u8bed\u53e5\u3001\u4f7f\u7528 os \u6a21\u5757\u3001\u4f7f\u7528 pathlib \u6a21\u5757<\/strong> \u90fd\u662f\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728 Python \u7684\u5b66\u4e60\u548c\u4f7f\u7528\u4e2d\u53d6\u5f97\u66f4\u5927\u7684\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u65b0\u6587\u4ef6\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u65b0\u6587\u4ef6\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\u3002\u53ea\u9700\u63d0\u4f9b\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff0c\u4f8b\u5982<code>&#39;w&#39;<\/code>\u7528\u4e8e\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u81ea\u52a8\u521b\u5efa\u5b83\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open(&#39;myfile.txt&#39;, &#39;w&#39;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myfile.txt<\/code>\u7684\u65b0\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u552f\u4e00\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u552f\u4e00\u6027\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u540d\u4e2d\u6dfb\u52a0\u65f6\u95f4\u6233\u6216\u968f\u673a\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u751f\u6210\u5f53\u524d\u65f6\u95f4\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u9644\u52a0\u5230\u6587\u4ef6\u540d\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u540c\u540d\u6587\u4ef6\u7684\u8986\u76d6\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6587\u4ef6\u521b\u5efa\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u6216\u8def\u5f84\u9519\u8bef\u3002\u4f7f\u7528<code>try-except<\/code>\u5757\u53ef\u4ee5\u6709\u6548\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u4fbf\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u63d0\u793a\uff0c\u5e2e\u52a9\u5176\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u65b0\u5efa\u6587\u4ef6\u540d\u5e76\u4fdd\u5b58\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528 open() \u51fd\u6570\u3001\u4f7f\u7528 with \u8bed\u53e5\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1096342,"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\/1096335"}],"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=1096335"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096335\/revisions"}],"predecessor-version":[{"id":1096347,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096335\/revisions\/1096347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096342"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}