﻿{"id":1129,"date":"2020-07-05T14:49:23","date_gmt":"2020-07-05T06:49:23","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=1129"},"modified":"2020-07-05T14:49:23","modified_gmt":"2020-07-05T06:49:23","slug":"python-duxie-wenjian","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/python-duxie-wenjian.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u8bfb\u5199\u6587\u4ef6"},"content":{"rendered":"<p>\u6587\u4ef6\u7528\u4e8e\u6c38\u4e45\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\u3002\u4ece\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u5e38\u89c1\u9700\u6c42\u3002\u4efb\u4f55\u6587\u4ef6\u5728\u8bfb\u5199\u4e4b\u524d\u90fd\u9700\u8981\u6253\u5f00\u3002\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u90fd\u4f7f\u7528open()\u65b9\u6cd5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u4ee5\u4fbf\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\uff08file object\uff09\u8bfb\u5199\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f\u4f5c\u4e3aopen()\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u4ee5\u8bf4\u660e\u6253\u5f00\u6587\u4ef6\u7684\u76ee\u7684\u3002\u8fd9\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\u3002close()\u65b9\u6cd5\u7528\u4e8e\u5728\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u540e\u91ca\u653e\u6587\u4ef6\u5bf9\u8c61\u5360\u7528\u7684\u8d44\u6e90\u3002Python\u7f16\u7a0b\u53ef\u4ee5\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u5b83\u4eec\u662f\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u672c\u6559\u7a0b\u63cf\u8ff0\u4e86\u5982\u4f55\u5728Python\u7f16\u7a0b\u4e2d\u8bfb\u5199\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f:<\/strong><\/p>\n<p>\u524d\u9762\u5df2\u7ecf\u63d0\u5230\uff0copen()\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f\uff0c\u672c\u90e8\u5206\u5c06\u5bf9\u6b64\u8fdb\u884c\u63cf\u8ff0\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u5982\u4e0b\u6240\u8ff0\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\"><strong>\u6a21\u5f0f<\/strong><\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\"><strong>\u7528\u9014<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">t<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u8868\u793a\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u662f\u9ed8\u8ba4\u6587\u4ef6\u7c7b\u578b\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">b<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u8868\u793a\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">r<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\uff0c\u8fd9\u662f\u6253\u5f00\u4efb\u4f55\u6587\u4ef6\u7684\u9ed8\u8ba4\u6a21\u5f0f\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">w<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">x<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">a<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5b83\u5c06\u6253\u5f00\u6587\u4ef6\u5e76\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff1b\u5426\u5219\uff0c\u8bf7\u521b\u5efa\u6587\u4ef6\u5e76\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u5185\u5bb9\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">r+<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\uff0c\u5e76\u5c06\u5149\u6807\u7f6e\u4e8e\u6587\u4ef6\u7684\u5f00\u5934\u3002 \u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">w+<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8986\u76d6\u6570\u636e\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"76\">a+<\/td>\n<td style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6;\" width=\"500\">\u6253\u5f00\u6587\u4ef6\u4ee5\u8fdb\u884c\u8bfb\u5199\uff0c\u5e76\u5c06\u5149\u6807\u7f6e\u4e8e\u73b0\u6709\u6587\u4ef6\u7684\u6587\u4ef6\u672b\u5c3e\u3002 \u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u521b\u5efa\u6587\u4ef6\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>Python\u4e2d\u5b58\u5728\u8bb8\u591a\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u7684\u65b9\u6cd5\u3002 \u672c\u6587\u8be6\u8ff0\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p>open():<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5305\u542b\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5f3a\u5236\u6027\u7684\uff0c\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u540d\u8fdb\u884c\u8bfb\u53d6\u6216\u5199\u5165\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u7528\u4e8e\u8bbe\u7f6e\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f\u3002\u9ed8\u8ba4\u7684\u6587\u4ef6\u8bbf\u95ee\u6a21\u5f0f\u662f\u201crt\u201d\u3002\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p>FileObject = open(\u201cFilename\u201d,\u201dFileMode\u201d)<br \/>\nclose():<\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u5173\u95ed\u6587\u4ef6\u5e76\u4f7f\u5b83\u53ef\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002 \u8c03\u7528\u6b64\u65b9\u6cd5\u540e\uff0c\u6587\u4ef6\u5904\u7406\u5bf9\u8c61\u5c06\u4e0d\u53ef\u7528\u3002<\/p>\n<p>read():<\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u4ece\u6587\u4ef6\u8bfb\u53d6\u7279\u5b9a\u6570\u91cf\u7684\u5b57\u8282\u3002<\/p>\n<p>readline():<\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u4ece\u6587\u4ef6\u8bfb\u53d6\u7279\u5b9a\u884c\u3002<\/p>\n<p>readlines():<\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7528\u9017\u53f7(,)\u5206\u9694\u7684\u6587\u4ef6\u7684\u6240\u6709\u884c\u3002<\/p>\n<p>write():<\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c linuxmi.txt\u201d\u7684\u6587\u672c\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff0c\u4ee5\u5728\u672c\u6587\u7684\u4e0b\u4e00\u90e8\u5206\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>Linux\u8ff7<br \/>\nwww.linuxmi.com<br \/>\nlinuxmi.com<br \/>\nlinuxmi<br \/>\nm.linuxmi.com<\/p>\n<p><strong>\u793a\u4f8b1\uff1a\u4f7f\u7528read()\u3001readline()\u548creadlines()\u8bfb\u53d6\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0bPython\u811a\u672c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3alinuxmi1.py\u7684\u6587\u4ef6\u3002 \u5b83\u5c06\u4f7f\u7528read()\u6839\u636e\u5b57\u8282\u5927\u5c0f\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f7f\u7528readline()\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u56fa\u5b9a\u6570\u91cf\u7684\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528readlines()\u8bfb\u53d6\u6570\u7ec4\u4e2d\u6587\u4ef6\u7684\u6240\u6709\u884c\u3002<\/p>\n<div style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6; color: #333333; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafafc; text-decoration-style: initial; text-decoration-color: initial;\">\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #221a0f; white-space: pre; color: #d3af86; margin: 0px 0.15em; display: block; border-radius: 3px; border: 1px solid #cccccc; padding: 0.5em;\">\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6<\/span>\r\nFileHandler = open(<span style=\"color: #889b4a;\">\"linuxmi.txt\"<\/span>,<span style=\"color: #889b4a;\">\"r\"<\/span>)\r\n<span style=\"color: #d6baad;\"># \u6839\u636e\u5927\u5c0f\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/span>\r\nprint(<span style=\"color: #889b4a;\">'\u8f93\u51fa\u6765\u81ea read() \u65b9\u6cd5\\n'<\/span>,FileHandler.read(<span style=\"color: #f79a32;\">2048<\/span>))\r\n<span style=\"color: #d6baad;\"># \u5173\u95ed\u6587\u4ef6<\/span>\r\nFileHandler.close()\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199<\/span>\r\nFileHandler = open(<span style=\"color: #889b4a;\">\"linuxmi.txt\"<\/span>,<span style=\"color: #889b4a;\">\"r+\"<\/span>)\r\n<span style=\"color: #d6baad;\"># \u8bfb\u53d6\u7b2c\u4e09\u884c\u7684\u6587\u4ef6\u5185\u5bb9<\/span>\r\nprint(<span style=\"color: #889b4a;\">'\u8f93\u51fa\u6765\u81ea readline() \u65b9\u6cd5\\n'<\/span>,FileHandler.readline(<span style=\"color: #f79a32;\">5<\/span>))\r\n<span style=\"color: #d6baad;\"># \u5173\u95ed\u6587\u4ef6<\/span>\r\nFileHandler.close()\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u548c\u9644\u52a0<\/span>\r\nFileHandler = open(<span style=\"color: #889b4a;\">\"linuxmi.txt\"<\/span>,<span style=\"color: #889b4a;\">\"r\"<\/span>)\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u548c\u9644\u52a0<\/span>\r\nprint(<span style=\"color: #889b4a;\">'\u8f93\u51fa\u6765\u81ea readlines() \u65b9\u6cd5\\n'<\/span>,FileHandler.readlines())\r\n<span style=\"color: #d6baad;\"># \u5173\u95ed\u6587\u4ef6<\/span>\r\nFileHandler.close()\r\n<\/code><\/pre>\n<\/div>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u5c06\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1130\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-1.png\" alt=\"\" width=\"786\" height=\"527\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-1.png 786w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-1-300x201.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-1-768x515.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-1-430x288.png 430w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/p>\n<p><strong>\u793a\u4f8b2\uff1a\u4f7f\u7528\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3alinuxmi2.py\u7684\u6587\u4ef6\u3002 \u5b83\u5c06\u4f7f\u7528for\u5faa\u73af\u4ece\u6587\u4ef6\u5bf9\u8c61\u8bfb\uff08fileObject\uff09\u53d6\u5e76\u6253\u5370\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n<div style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6; color: #333333; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafafc; text-decoration-style: initial; text-decoration-color: initial;\">\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #221a0f; white-space: pre; color: #d3af86; margin: 0px 0.15em; display: block; border-radius: 3px; border: 1px solid #cccccc; padding: 0.5em;\">\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6<\/span>\r\nfileObject = open(<span style=\"color: #889b4a;\">\"linuxmi.txt\"<\/span>, <span style=\"color: #889b4a;\">\"r\"<\/span>)\r\n<span style=\"color: #d6baad;\"># \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u5728\u7ec8\u7aef\u4e2d\u6253\u5370<\/span>\r\n<span style=\"color: #98676a;\">for<\/span> line <span style=\"color: #98676a;\">in<\/span> fileObject:\r\n  print(line)\r\n<\/code><\/pre>\n<\/div>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u5c06\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1131\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-2.png\" alt=\"\" width=\"731\" height=\"490\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-2.png 786w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-2-300x201.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-2-768x515.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-2-430x288.png 430w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p><strong>\u793a\u4f8b3\uff1a\u901a\u8fc7\u4f7f\u7528with\u8bed\u53e5\u6765\u8bfb\u53d6\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3alinuxmi3.py\u7684\u6587\u4ef6\u3002\u5b83\u5c06\u5728\u4e0d\u4f7f\u7528\u4efb\u4f55\u6587\u4ef6\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\u901a\u8fc7\u8bed\u53e5\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<div style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6; color: #333333; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafafc; text-decoration-style: initial; text-decoration-color: initial;\">\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #221a0f; white-space: pre; color: #d3af86; margin: 0px 0.15em; display: block; border-radius: 3px; border: 1px solid #cccccc; padding: 0.5em;\">\r\n<span style=\"color: #d6baad;\"># \u4f7f\u7528with\u8bed\u53e5\u8bfb\u53d6\u6587\u4ef6<\/span>\r\n<span style=\"color: #98676a;\">with<\/span> open(<span style=\"color: #889b4a;\">\"linuxmi.txt\"<\/span>) <span style=\"color: #98676a;\">as<\/span> fhandler:\r\n  print(fhandler.readlines())\r\n<\/code><\/pre>\n<\/div>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u5c06\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1132\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-3.png\" alt=\"\" width=\"731\" height=\"490\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-3.png 786w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-3-300x201.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-3-768x515.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-3-430x288.png 430w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p><strong>\u7f16\u5199\u6587\u672c\u6587\u4ef6\uff1a<\/strong><\/p>\n<p>\u901a\u8fc7\u5b9a\u4e49\u6587\u4ef6\u5bf9\u8c61\u6216\u5c06\u5176\u4e0e\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u4e2d\u3002<\/p>\n<p><strong>\u793a\u4f8b4\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u5199\u5165\u6587\u4ef6\uff08file object\uff09<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3alinuxmi.com.py\u7684\u6587\u4ef6\u3002 \u5b83\u5c06\u6253\u5f00\u4e00\u4e2a\u7528\u4e8e\u5199\u5165\u7684\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u4e09\u884c\u3002<\/p>\n<div style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6; color: #333333; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafafc; text-decoration-style: initial; text-decoration-color: initial;\">\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #221a0f; white-space: pre; color: #d3af86; margin: 0px 0.15em; display: block; border-radius: 3px; border: 1px solid #cccccc; padding: 0.5em;\">\r\n<span style=\"color: #d6baad;\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/span>\r\nfileObject = open(<span style=\"color: #889b4a;\">\"www.linuxmi.com.txt\"<\/span>, <span style=\"color: #889b4a;\">\"w\"<\/span>)\r\n<span style=\"color: #d6baad;\">#  \u6dfb\u52a0\u4e00\u4e9b\u6587\u672c<\/span>\r\nfileObject.write(<span style=\"color: #889b4a;\">\"Linux\u8ff7\u6b22\u8fce\u60a8\\n\"<\/span>)\r\nfileObject.write(<span style=\"color: #889b4a;\">\"\u6211\u4eec\u7684\u7f51\u5740\u662fwww.linuxmi.com\\n\"<\/span>)\r\nfileObject.write(<span style=\"color: #889b4a;\">\"\u624b\u673a\u7ad9m.linuxmi.com\\n\"<\/span>)\r\n<span style=\"color: #d6baad;\"># \u5173\u95ed\u6587\u4ef6<\/span>\r\nfileObject.close()\r\n<\/code><\/pre>\n<\/div>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p>\u8fd0\u884c\u811a\u672c\uff0c\u5e76\u68c0\u67e5\u662f\u5426\u4f7f\u7528\u5185\u5bb9\u521b\u5efa\u4e86\u6587\u4ef6\u3002 \u8fd0\u884c\u811a\u672c\u5e76\u8fd0\u884c\u201c cat\u201d\u547d\u4ee4\u540e\uff0c\u5c06\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1133\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-4.png\" alt=\"\" width=\"731\" height=\"490\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-4.png 786w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-4-300x201.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-4-768x515.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-4-430x288.png 430w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p><strong>\u793a\u4f8b5\uff1a\u4f7f\u7528with\u8bed\u53e5\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<p>\u65e0\u9700\u5b9a\u4e49\u6587\u4ef6\u5bf9\u8c61\u5c31\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002 \u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3am.linuxmi.com.py\u7684\u6587\u4ef6\u3002 \u901a\u8fc7\u4f7f\u7528with\u8bed\u53e5\uff0c\u5b83\u5c06\u5728\u6587\u4ef6\u4e2d\u5199\u5165\u4e24\u884c\u3002<\/p>\n<div style=\"font-size: 14px; overflow-wrap: break-word; font-family: tahoma, \u5b8b\u4f53; line-height: 1.6; color: #333333; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafafc; text-decoration-style: initial; text-decoration-color: initial;\">\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #221a0f; white-space: pre; color: #d3af86; margin: 0px 0.15em; display: block; border-radius: 3px; border: 1px solid #cccccc; padding: 0.5em;\">\r\n<span style=\"color: #d6baad;\"># \u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/span>\r\n<span style=\"color: #98676a;\">with<\/span> open(<span style=\"color: #889b4a;\">\"m.linuxmi.com.txt\"<\/span>,<span style=\"color: #889b4a;\">'w'<\/span>) <span style=\"color: #98676a;\">as<\/span> fileObj:\r\n   fileObj.write(<span style=\"color: #889b4a;\">\"Linux\u8ff7\u4e3a\u60a8\u63d0\u4f9b\u6700\u65b0\u7684\u5f00\u6e90\u8d44\u8baf\\n\"<\/span>)\r\n   fileObj.write(<span style=\"color: #889b4a;\">\"linuxmi.com\u63d0\u4f9b\u6700\u65b0\u6700\u524d\u6cbf\u7684\u5f00\u6e90\u6280\u672f\\n\"<\/span>)\r\n<\/code><\/pre>\n<\/div>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p>\u8fd0\u884c\u811a\u672c\u548c\u201c cat\u201d\u547d\u4ee4\u8bfb\u53d6\u6587\u4ef6\u540e\uff0c\u5c06\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1134\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-5.png\" alt=\"\" width=\"731\" height=\"490\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-5.png 786w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-5-300x201.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-5-768x515.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/python-5-430x288.png 430w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u672c\u6559\u7a0b\u901a\u8fc7\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u7684\u793a\u4f8b\u63cf\u8ff0\u4e86\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5185\u5bb9\u5e76\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u3002Python\u65b0\u624b\u5c06\u80fd\u591f\u77e5\u9053\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u6240\u9700\u51fd\u6570\u7684\u7528\u6cd5\u3002\u4f60\u5b66\u4f1a\u4e86\u5417\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u7528\u4e8e\u6c38\u4e45\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\u3002\u4ece\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u5e38\u89c1\u9700\u6c42\u3002\u4efb\u4f55\u6587\u4ef6\u5728\u8bfb\u5199\u4e4b &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[44],"class_list":["post-1129","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"views":743,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=1129"}],"version-history":[{"count":1,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1129\/revisions"}],"predecessor-version":[{"id":1135,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1129\/revisions\/1135"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=1129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=1129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=1129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}