{"id":336,"date":"2024-04-29T01:49:29","date_gmt":"2024-04-28T17:49:29","guid":{"rendered":"https:\/\/669082.xyz\/?p=336"},"modified":"2024-04-29T02:05:37","modified_gmt":"2024-04-28T18:05:37","slug":"20240428%e5%ad%a6%e4%b9%a0python_%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/669082.xyz\/index.php\/2024\/04\/29\/20240428%e5%ad%a6%e4%b9%a0python_%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86\/","title":{"rendered":"20240428\u5b66\u4e60Python_\u6587\u4ef6\u5904\u7406"},"content":{"rendered":"<h5>\u6587\u4ef6\u6253\u5f00<\/h5>\n<h6>\u5728 Python \u4e2d\u4f7f\u7528\u6587\u4ef6\u7684\u5173\u952e\u51fd\u6570\u662f open() \u51fd\u6570\u3002<\/h6>\n<h6>open() \u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714326453247.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-python\">f = open(&quot;demofile.txt&quot;)\n\n#\u4ee5\u4e0a\u4ee3\u7801\u7b49\u540c\u4e8e\nf = open(&quot;demofile.txt&quot;, &quot;rt&quot;)\n\n#\u56e0\u4e3a &quot;r&quot; (\u8bfb\u53d6)\u548c &quot;t&quot; (\u6587\u672c)\u662f\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u6307\u5b9a\u5b83\u4eec\u3002\n#\u5f53\u7136\u6307\u5b9a\u4e86\u4e5f\u6ca1\u95ee\u9898\n#\u6ce8\u91ca\uff1a\u8bf7\u786e\u4fdd\u6587\u4ef6\u5b58\u5728\uff0c\u5426\u5219\u60a8\u5c06\u6536\u5230\u9519\u8bef\u6d88\u606f\u3002\n<\/code><\/pre>\n<h5>\u6587\u4ef6\u8bfb\u53d6<\/h5>\n<pre><code class=\"language-python\">f = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nprint(f.read())\n\n#\u8fd4\u56de\u6587\u4ef6\u4e2d\u7684\u524d\u4e94\u4e2a\u5b57\u7b26\uff1a\nf = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nprint(f.read(5))\n\n#readline() \u65b9\u6cd5\u8fd4\u56de\u4e00\u884c\uff1a\n#\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\uff1a\nf = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nprint(f.readline())\n\n#\u901a\u8fc7\u4e24\u6b21\u8c03\u7528 readline()\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6\u524d\u4e24\u884c\nf = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nprint(f.readline())\nprint(f.readline())\n\n#\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6587\u4ef6\u4e2d\u7684\u884c\uff0c\u60a8\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff1a\nf = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nfor x in f:\n  print(x)\n\n#\u5173\u95ed\u6587\u4ef6\n#\u5b8c\u6210\u540e\u59cb\u7ec8\u5173\u95ed\u6587\u4ef6\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\nf = open(&quot;demofile.txt&quot;, &quot;r&quot;)\nprint(f.readline())\nf.close()\n<\/code><\/pre>\n<h5>Python \u6587\u4ef6 \u5199\u5165 or \u521b\u5efa<\/h5>\n<pre><code class=\"language-python\">#\u5982\u9700\u5199\u5165\u5df2\u6709\u7684\u6587\u4ef6\uff0c\u5fc5\u987b\u5411 open() \u51fd\u6570\u6dfb\u52a0\u53c2\u6570\n#  &quot;a&quot; - \u8ffd\u52a0 - \u4f1a\u8ffd\u52a0\u5230\u6587\u4ef6\u7684\u672b\u5c3e\n#  &quot;w&quot; - \u5199\u5165 - \u4f1a\u8986\u76d6\u4efb\u4f55\u5df2\u6709\u7684\u5185\u5bb9\n\nf = open(&quot;demofile2.txt&quot;, &quot;a&quot;)\nf.write(&quot;Now the file has more content!&quot;)\nf.close()\n\n# \u8ffd\u52a0\u540e\uff0c\u6253\u5f00\u5e76\u8bfb\u53d6\u8be5\u6587\u4ef6\uff1a\nf = open(&quot;demofile2.txt&quot;, &quot;r&quot;)\nprint(f.read())\n\nf = open(&quot;demofile3.txt&quot;, &quot;w&quot;)\nf.write(&quot;Woops! I have deleted the content!&quot;)\nf.close()\n\n# \u5199\u5165\u540e\uff0c\u6253\u5f00\u5e76\u8bfb\u53d6\u8be5\u6587\u4ef6\uff1a\nf = open(&quot;demofile3.txt&quot;, &quot;r&quot;)\nprint(f.read())\n\n#\u521b\u5efa\u65b0\u6587\u4ef6\n#\u5728 Python \u4e2d\u521b\u5efa\u65b0\u6587\u4ef6\uff0c\u8bf7\u4f7f\u7528 open() \u65b9\u6cd5\uff0c\u5e76\u4f7f\u7528\u4ee5\u4e0b\u53c2\u6570\u4e4b\u4e00\n# &quot;x&quot; - \u521b\u5efa - \u5c06\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u8fd4\u56de\u9519\u8bef\n# &quot;a&quot; - \u8ffd\u52a0 - \u5982\u679c\u6307\u5b9a\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\n# &quot;w&quot; - \u5199\u5165 - \u5982\u679c\u6307\u5b9a\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\n\n#\u521b\u5efa\u540d\u4e3a &quot;myfile.txt&quot; \u7684\u6587\u4ef6\uff1a\nf = open(&quot;myfile.txt&quot;, &quot;x&quot;)\n\n#\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u65b0\u6587\u4ef6\uff1a\nf = open(&quot;myfile.txt&quot;, &quot;w&quot;)\n<\/code><\/pre>\n<h5>Python \u5220\u9664\u6587\u4ef6 os.remove()<\/h5>\n<pre><code class=\"language-python\">#\u5220\u9664\u6587\u4ef6 &quot;demofile.txt&quot;\uff1a\nimport os\nos.remove(&quot;demofile.txt&quot;)\n\n#\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\n#\u4e3a\u907f\u514d\u51fa\u73b0\u9519\u8bef\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u5c1d\u8bd5\u5220\u9664\u6587\u4ef6\u4e4b\u524d\u68c0\u67e5\u8be5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a\n\n#\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u7136\u540e\u5220\u9664\u5b83\uff1a\nimport os\nif os.path.exists(&quot;demofile.txt&quot;):\n  os.remove(&quot;demofile.txt&quot;)\nelse:\n  print(&quot;The file does not exist&quot;)\n\n#\u5220\u9664\u6587\u4ef6\u5939\n#\u5982\u9700\u5220\u9664\u6574\u4e2a\u6587\u4ef6\u5939\uff0c\u8bf7\u4f7f\u7528 os.rmdir() \u65b9\u6cd5\uff1a\n#\u53ea\u80fd\u5220\u9664\u7a7a\u6587\u4ef6\u5939\nimport os\nos.rmdir(&quot;myfolder&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u6253\u5f00 \u5728 Python \u4e2d\u4f7f\u7528\u6587\u4ef6\u7684\u5173\u952e\u51fd\u6570\u662f open() \u51fd\u6570\u3002 open() \u51fd\u6570\u6709\u4e24\u4e2a\u53c2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":7,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions\/344"}],"wp:attachment":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}