{"id":8693,"date":"2023-05-15T16:38:08","date_gmt":"2023-05-15T08:38:08","guid":{"rendered":"\/?p=8693"},"modified":"2023-05-15T16:38:08","modified_gmt":"2023-05-15T08:38:08","slug":"python%e8%bf%9b%e9%98%b6","status":"publish","type":"post","link":"\/?p=8693","title":{"rendered":"python\u8fdb\u9636"},"content":{"rendered":"<h3>\u65b9\u6cd5<\/h3>\n<p>\u5728 <code>Python<\/code> \u4e2d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6cd5\u9a8c\u8bc1\uff1a<\/p>\n<ol>\n<li>\u5728 <strong>\u6807\u8bc6\u7b26<\/strong> \/ <strong>\u6570\u636e<\/strong> \u540e\u8f93\u5165\u4e00\u4e2a <code>.<\/code>\uff0c\u7136\u540e\u6309\u4e0b <code>TAB<\/code> \u952e\uff0c<code>iPython<\/code> \u4f1a\u63d0\u793a\u8be5\u5bf9\u8c61\u80fd\u591f\u8c03\u7528\u7684 <strong>\u65b9\u6cd5\u5217\u8868<\/strong><\/li>\n<li>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>dir<\/code> \u4f20\u5165 <strong>\u6807\u8bc6\u7b26<\/strong> \/ <strong>\u6570\u636e<\/strong>\uff0c\u53ef\u4ee5\u67e5\u770b\u5bf9\u8c61\u5185\u7684 <strong>\u6240\u6709\u5c5e\u6027\u53ca\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><strong>\u63d0\u793a<\/strong> <code>__\u65b9\u6cd5\u540d__<\/code> \u683c\u5f0f\u7684\u65b9\u6cd5\u662f <code>Python<\/code> \u63d0\u4f9b\u7684 <strong>\u5185\u7f6e\u65b9\u6cd5 \/ \u5c5e\u6027<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5e8f\u53f7<\/th>\n<th style=\"text-align: left;\">\u65b9\u6cd5\u540d<\/th>\n<th style=\"text-align: left;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: left;\">\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">01<\/td>\n<td style=\"text-align: left;\"><strong>new<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u4f1a\u88ab \u81ea\u52a8 \u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">02<\/td>\n<td style=\"text-align: left;\"><strong>init<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u65f6\uff0c\u4f1a\u88ab \u81ea\u52a8 \u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">03<\/td>\n<td style=\"text-align: left;\"><strong>del<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u5bf9\u8c61\u88ab\u4ece\u5185\u5b58\u4e2d\u9500\u6bc1\u524d\uff0c\u4f1a\u88ab \u81ea\u52a8 \u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">04<\/td>\n<td style=\"text-align: left;\"><strong>str<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u8fd4\u56de\u5bf9\u8c61\u7684\u63cf\u8ff0\u4fe1\u606f\uff0cprint \u51fd\u6570\u8f93\u51fa\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">05<\/td>\n<td style=\"text-align: left;\"><strong>file<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u8fd4\u56de\u6587\u4ef6\u6240\u5728\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">06<\/td>\n<td style=\"text-align: left;\"><strong>name<\/strong><\/td>\n<td style=\"text-align: left;\">\u65b9\u6cd5<\/td>\n<td style=\"text-align: left;\">\u6d4b\u8bd5\u6a21\u5757\u4ee3\u7801\u53ea\u5728\u6d4b\u8bd5\u60c5\u51b5\u4e0b\u8fd0\u884c,\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u6267\u884c!<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>\u5229\u7528\u597d <code>dir()<\/code> \u51fd\u6570\uff0c\u5728\u5b66\u4e60\u65f6\u5f88\u591a\u5185\u5bb9\u5c31\u4e0d\u9700\u8981\u6b7b\u8bb0\u786c\u80cc\u4e86 <\/p>\n<\/blockquote>\n<h4>\u521d\u59cb\u5316\u65b9\u6cd5 \u2014\u2014\u540c\u65f6\u8bbe\u7f6e\u521d\u59cb\u503c<\/h4>\n<p>\u5728\u5f00\u53d1\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5728 <strong>\u521b\u5efa\u5bf9\u8c61\u7684\u540c\u65f6\uff0c\u5c31\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong>\uff0c\u53ef\u4ee5\u5bf9 <code>__init__<\/code> \u65b9\u6cd5\u8fdb\u884c <strong>\u6539\u9020<\/strong><\/p>\n<ol>\n<li>\u628a\u5e0c\u671b\u8bbe\u7f6e\u7684\u5c5e\u6027\u503c\uff0c\u5b9a\u4e49\u6210 <code>__init__<\/code> \u65b9\u6cd5\u7684\u53c2\u6570<\/li>\n<li>\u5728\u65b9\u6cd5\u5185\u90e8\u4f7f\u7528 <code>self.\u5c5e\u6027 = \u5f62\u53c2<\/code> \u63a5\u6536\u5916\u90e8\u4f20\u9012\u7684\u53c2\u6570<\/li>\n<li>\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u4f7f\u7528 <code>\u7c7b\u540d(\u5c5e\u60271, \u5c5e\u60272...)<\/code> \u8c03\u7528<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">class Cat:\n\n    def __init__(self, name):\n        print(&quot;\u521d\u59cb\u5316\u65b9\u6cd5 %s&quot; % name)\n        self.name = name\n    ...\n\ntom = Cat(&quot;Tom&quot;)\n...\n\nlazy_cat = Cat(&quot;\u5927\u61d2\u732b&quot;)\n...<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>\u79c1\u6709\u5c5e\u6027\u548c\u79c1\u6709\u65b9\u6cd5<\/h4>\n<ul>\n<li>\u5728\u5b9a\u4e49\u65b9\u6cd5\u548c\u5c5e\u6027\u65f6,\u5728\u5c5e\u6027\u540d\u6216\u8005\u65b9\u6cd5\u540d\u524d\u589e\u52a0<strong>\u4e24\u4e2a\u4e0b\u5212\u7ebf<\/strong>,\u5b9a\u4e49\u7684\u5c31\u662f<strong>\u79c1\u6709<\/strong>\u5c5e\u6027\u6216\u65b9\u6cd5<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">self.__age = 18\ndef __scret(self):\n    pass<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5<\/h4>\n<ul>\n<li>\u7c7b\u65b9\u6cd5\u5c31\u662f\u9488\u5bf9<strong>\u7c7b\u5bf9\u8c61<\/strong>\u5b9a\u4e49\u7684\u65b9\u6cd5<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># \u7c7b\u65b9\u6cd5\n@classmethod\ndef \u65b9\u6cd5\u540d(cls):\n    pass\n\n# \u9759\u6001\u65b9\u6cd5\n@staticmethod\ndef \u9759\u6001\u65b9\u6cd5\u540d():\n    pass<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u5c01\u88c5<\/h3>\n<ul>\n<li>\u65b9\u6cd5\u5199\u5728\u7c7b\u4e2d\u5c31\u662f\u5c01\u88c5<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">class HouseItem:\n\n    def __init__(self, name, area):\n        &quot;&quot;&quot;\n\n        :param name: \u5bb6\u5177\u540d\u79f0\n        :param area: \u5360\u5730\u9762\u79ef\n        &quot;&quot;&quot;\n        self.name = name\n        self.area = area\n\n    def __str__(self):\n        return &quot;[%s] \u5360\u5730\u9762\u79ef %.2f&quot; % (self.name, self.area)\n\n# 1. \u521b\u5efa\u5bb6\u5177\nbed = HouseItem(&quot;\u5e2d\u68a6\u601d&quot;, 4)\nchest = HouseItem(&quot;\u8863\u67dc&quot;, 2)\ntable = HouseItem(&quot;\u9910\u684c&quot;, 1.5)\n\nprint(bed)\nprint(chest)\nprint(table)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u7ee7\u627f and \u591a\u7ee7\u627f<\/h3>\n<ul>\n<li>\n<p>\u5f53<\/p>\n<p>\u7236\u7c7b<\/p>\n<p>\u65b9\u6cd5\u4e0d\u80fd\u6ee1\u8db3\u5b50\u7c7b\u9700\u6c42\u65f6,\u53ef\u4ee5\u5bf9\u65b9\u6cd5\u8fdb\u884c<\/p>\n<p>\u91cd\u5199(override)<\/p>\n<ul>\n<li>\u8986\u76d6\u7236\u7c7b\u7684\u65b9\u6cd5<\/li>\n<li>\u5bf9\u7236\u7c7b\u65b9\u6cd5\u8fdb\u884c\u6269\u5c55<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># \u5355\u7ee7\u627f\nclass \u7c7b\u540d(\u7236\u7c7b\u540d):\n    pass\n# \u591a\u7ee7\u627f\nclass \u7c7b\u540d(\u7236\u7c7b\u540d,\u7236\u7c7b\u540d):\n    pass<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li>\u5bf9\u7236\u7c7b\u65b9\u6cd5\u8fdb\u884c\u6269\u5c55\n<ul>\n<li>\u5728<strong>\u5b50\u7c7b\u4e2d\u91cd\u5199<\/strong>\u7236\u7c7b\u7684\u65b9\u6cd5<\/li>\n<li>\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u4f7f\u7528<code>super()<\/code>.\u7236\u7c7b\u65b9\u6cd5\u6765\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u7684\u6267\u884c<\/li>\n<li>\u4ee3\u7801\u5176\u4ed6\u4f4d\u7f6e\u9488\u5bf9\u5b50\u7c7b\u7684\u9700\u6c42,\u7f16\u5199<strong>\u5b50\u7c7b\u7279\u6709\u7684\u4ee3\u7801\u5b9e\u73b0<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><code>super()<\/code>\u5c31\u662f<code>super<\/code>\u7c7b\u521b\u5efa\u7684\u5bf9\u8c61 <\/li>\n<li>\u6700\u5e38\u4f7f\u7528\u7684\u573a\u666f\u5c31\u662f\u5728<strong>\u91cd\u5199\u7236\u7c7b\u65b9\u6cd5<\/strong>\u65f6,\u8c03\u7528<strong>\u5728\u7236\u7c7b\u4e2d\u5c01\u88c5\u7684\u65b9\u6cd5\u5b9e\u73b0<\/strong> <\/li>\n<li><strong>\u5b50\u7c7b\u5bf9\u8c61<\/strong>\u4e0d\u80fd\u5728\u81ea\u5df1\u7684\u65b9\u6cd5\u7684\u5185\u90e8,<strong>\u76f4\u63a5<\/strong>\u8bbf\u95ee\u7236\u7c7b\u7684<strong>\u79c1\u6709\u5c5e\u6027\u548c\u79c1\u6709\u65b9\u6cd5<\/strong> <\/li>\n<li><strong>\u5b50\u7c7b\u5bf9\u8c61<\/strong>\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u7684<strong>\u516c\u6709<\/strong>\u65b9\u6cd5<strong>\u95f4\u63a5<\/strong>\u8bbf\u95ee<strong>\u79c1\u6709\u5c5e\u6027\u6216\u79c1\u6709\u65b9\u6cd5 <\/strong> <\/li>\n<\/ul>\n<h3>\u591a\u6001<\/h3>\n<ul>\n<li>\u5728\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u4f20\u5165\u4e0d\u540c\u7684<strong>\u72d7\u5bf9\u8c61<\/strong>\u5b9e\u53c2\uff0c\u5c31\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6267\u884c\u6548\u679c<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def game(self):\n        print(&quot;%s \u8e66\u8df3\u73a9&quot; % self.name)\n\nclass XiaoTianDog(Dog):\n    def game(self):\n        print(&quot;%s \u8fb9\u98de\u8fb9\u8e66\u8df3\u73a9&quot; % self.name)\n\nclass Person:\n    def __init__(self, name):\n        self.name = name\n\n    def game_width_dog(self, dog):\n        print(&quot;%s \u4e0e %s \u4e00\u8d77\u73a9\u800d&quot; % (self.name, dog.name))\n        dog.game()\n\nxiaoming = Person(&quot;\u5c0f\u660e&quot;)\nxiaotianquan = XiaoTianDog(&quot;\u54ee\u5929\u72ac&quot;)\nxiaoming.game_width_dog(xiaotianquan)\nwangcai = Dog(&quot;\u65fa\u8d22&quot;)\nxiaoming.game_width_dog(wangcai)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-javascript\">\u5c0f\u660e \u4e0e \u54ee\u5929\u72ac \u4e00\u8d77\u73a9\u800d\n\u54ee\u5929\u72ac \u8fb9\u98de\u8fb9\u8e66\u8df3\u73a9\n\u5c0f\u660e \u4e0e \u65fa\u8d22 \u4e00\u8d77\u73a9\u800d\n\u65fa\u8d22 \u8e66\u8df3\u73a9<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u5355\u4f8b<\/h3>\n<ul>\n<li>\u8bbe\u8ba1\u6a21\u5f0f\n<ul>\n<li>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u9488\u5bf9<strong>\u67d0\u4e00\u7279\u5b9a\u95ee\u9898<\/strong>\u7684\u6210\u719f\u7684\u89e3\u51b3\u65b9\u6848<\/li>\n<li>\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e3a\u4e86\u53ef\u91cd\u7528\u4ee3\u7801\u3001\u8ba9\u4ee3\u7801\u66f4\u5bb9\u6613\u88ab\u4eba\u7406\u89e3\u3001\u4fdd\u8bc1\u4ee3\u7801\u7684\u53ef\u8bfb\u6027<\/li>\n<\/ul>\n<\/li>\n<li>\u5355\u4f8b\u8bbe\u8ba1\u6a21\u5f0f\n<ul>\n<li>\u76ee\u7684\u2013\u8ba9\u7c7b\u521b\u5efa\u7684\u5bf9\u8c61,\u5728\u7cfb\u7edf\u4e2d<strong>\u53ea\u6709\u552f\u4e00\u7684\u4e00\u4e2a\u5b9e\u4f8b<\/strong><\/li>\n<li>\u6bcf\u4e00\u6b21\u6267\u884c<code>\u7c7b\u540d()<\/code>\u8fd4\u56de\u7684\u5bf9\u8c61\u3001<strong>\u5185\u5b58\u5730\u5740\u662f\u76f8\u540c\u7684<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\u5e94\u7528\u573a\u666f:\n<ul>\n<li><strong>\u97f3\u4e50\u64ad\u653e<\/strong> \u5bf9\u8c61<\/li>\n<li><strong>\u56de\u6536\u7ad9<\/strong> \u5bf9\u8c61<\/li>\n<li><strong>\u6253\u5370\u673a <\/strong>\u5bf9\u8c61<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u91cd\u5199new\u65b9\u6cd5\u7684\u4ee3\u7801\u975e\u5e38\u56fa\u5b9a <\/p>\n<\/blockquote>\n<ul>\n<li>\u91cd\u5199 <code>__new__<\/code>\u65b9\u6cd5\u4e00\u5b9a\u8981<code>return super().__new__(cls)<\/code> <\/li>\n<li><code>__new__<\/code>\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5,\u5728\u8c03\u7528\u65f6\u9700\u8981\u4e3b\u52a8\u4f20\u9012<code>cls<\/code>\u53c2\u6570 <\/li>\n<\/ul>\n<p>\u6848\u4f8b\u6f14\u793a<\/p>\n<ul>\n<li>\u53ea\u6267\u884c\u4e00\u6b21\u521d\u59cb\u5316<\/li>\n<li>\u5185\u5b58\u5730\u5740\u662f\u552f\u4e00\u7684<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">class MusicPlayer:\n    instance = None\n    init_flag = False\n\n    def __new__(cls, *args, **kwargs):\n        if cls.instance is None:\n            cls.instance = super().__new__(cls)\n        return cls.instance\n\n    def __init__(self):\n        if MusicPlayer.init_flag:\n            return\n        print(&quot;\u521d\u59cb\u5316\u64ad\u653e\u5668&quot;)\n        MusicPlayer.init_flag = True\n\nplayer1 = MusicPlayer()\nprint(player1)\nplayer2 = MusicPlayer()\nprint(player2)\n# \u8fd0\u884c\u7ed3\u679c\n\u521d\u59cb\u5316\u64ad\u653e\u5668\n&lt;__main__.MusicPlayer object at 0x0000017824D39A20&gt;\n&lt;__main__.MusicPlayer object at 0x0000017824D39A20&gt;<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u5f02\u5e38<\/h3>\n<ul>\n<li><strong>\u7b80\u5355\u6355\u83b7\u5f02\u5e38<\/strong>\u8bed\u6cd5\u683c\u5f0f<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">try:\n    \u5c1d\u8bd5\u6267\u884c\u7684\u4ee3\u7801\nexcept:\n    \u51fa\u73b0\u9519\u8bef\u7684\u5904\u7406<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li><strong>\u9519\u8bef\u7c7b\u578b\u6355\u83b7<\/strong>\u8bed\u6cd5\u683c\u5f0f<\/li>\n<li>\u5f53<code>python<\/code>\u89e3\u91ca\u5668<strong>\u629b\u51fa\u5f02\u5e38<\/strong>\u65f6,<strong>\u6700\u540e\u4e00\u884c<\/strong>\u9519\u8bef\u4fe1\u606f\u7684<strong>\u7b2c\u4e00\u4e2a\u5355\u8bcd<\/strong>,\u5c31\u662f\u9519\u8bef\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">try:\n    pass\nexcept \u9519\u8bef\u7c7b\u578b1:\n    pass\nexcept (\u9519\u8bef\u7c7b\u578b2,\u9519\u8bef\u7c7b\u578b3):\n    pass\n# \u6355\u83b7\u672a\u77e5\u9519\u8bef\nexcept Exception as result:\n    print(&quot;\u672a\u77e5\u9519\u8bef %s&quot; % result)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li><strong>\u5f02\u5e38\u6355\u83b7\u5b8c\u6574<\/strong>\u8bed\u6cd5<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">try:\n    pass\nexcept \u9519\u8bef\u7c7b\u578b1:\n    pass\nexcept (\u9519\u8bef\u7c7b\u578b2,\u9519\u8bef\u7c7b\u578b3):\n    pass\nexcept Exception as result:\n    print(&quot;\u672a\u77e5\u9519\u8bef %s&quot; % result)\nelse:\n    # \u6ca1\u6709\u5f02\u5e38\u624d\u4f1a\u6267\u884c\u7684\u4ee3\u7801\n    pass\nfinally:\n    # \u5fc5\u5b9a\u6267\u884c\u7684\u4ee3\u7801\n    pass<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li><strong>\u5f02\u5e38\u4f20\u9012\u6027<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">def demo1():\n    return int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570&quot;))\n\ndef demo2():\n    demo1()\n\ntry:\n    print(demo2())\nexcept Exception as result:\n    print(result)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<blockquote><\/blockquote>\n<ul>\n<li>\u518d\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u5728\u4e3b\u51fd\u6570\u4e2d\u589e\u52a0<strong>\u5f02\u5e38\u6355\u83b7<\/strong><\/li>\n<li>\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u5176\u4ed6\u51fd\u6570,\u53ea\u8981\u51fa\u73b0\u5f02\u5e38,\u90fd\u4f1a\u4f20\u9012\u5230\u4e3b\u51fd\u6570\u7684<strong>\u5f02\u5e38\u6355\u83b7<\/strong>\u4e2d<\/li>\n<li>\u8fd9\u6837\u4e0d\u9700\u8981\u518d\u4ee3\u7801\u4e2d\u589e\u52a0\u5927\u91cf\u7684<strong>\u5f02\u5e38\u6355\u83b7<\/strong>,\u4fdd\u8bc1\u4ee3\u7801\u7684\u6574\u6d01\u884c.<\/li>\n<li>\u4e3b\u52a8\u629b\u51fa<code>raise<\/code>\u5f02\u5e38<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">def input_password():\n    pwd = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801&quot;)\n    if len(pwd) &gt;= 8:\n        return pwd\n    ex = Exception(&quot;\u5bc6\u7801\u957f\u5ea6\u4e0d\u591f&quot;)\n    raise ex\n\ntry:\n\n    print(input_password())\nexcept Exception as result:\n    print(result)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u6a21\u5757<\/h3>\n<ul>\n<li>\u4f7f\u7528<code>as<\/code>\u6307\u5b9a\u6a21\u5757\u7684\u522b\u540d<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">import \u6a21\u5757\u540d1 as \u6a21\u5757\u540d<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<blockquote>\n<p>\u6a21\u5757\u522b\u540d\u5e94\u8be5\u7b26\u5408\u5927\u9a7c\u5cf0\u547d\u540d\u6cd5 <\/p>\n<\/blockquote>\n<ul>\n<li>\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165<strong>\u90e8\u5206<\/strong>\u5de5\u5177<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">from \u6a21\u5757\u540d1 import \u5de5\u5177\u540d<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<blockquote>\n<p>\u5bfc\u5165\u4e4b\u540e\u53ef\u76f4\u63a5\u4f7f\u7528\u6a21\u5757\u63d0\u4f9b\u7684\u5de5\u5177 \u2013 \u5168\u5c40\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b \u5982\u679c\u4e24\u4e2a\u6a21\u5757\u5b58\u5728<strong>\u540c\u540d\u51fd\u6570<\/strong>,\u90a3\u4e48\u540e\u5bfc\u5165\u7684\u6a21\u5757\u7684\u51fd\u6570\u4f1a<strong>\u8986\u76d6<\/strong>\u5148\u5bfc\u5165\u7684\u51fd\u6570 <\/p>\n<\/blockquote>\n<ul>\n<li>\u4ece\u6a21\u5757\u5bfc\u5165\u6240\u6709\u5de5\u5177<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">from \u6a21\u5757\u540d1 import *<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li><code>__name<\/code> \u5c5e\u6027\u517c\u987e\u6d4b\u8bd5\u548c\u5bfc\u5165<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">def main():\n    pass\nif __name__ == &quot;__main__&quot;:\n    main<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<ul>\n<li><code>__file__<\/code> \u6587\u4ef6\u6240\u5728\u8def\u5f84<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">import my_package\nprint(my_package.__file__)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u5305<\/h3>\n<pre><code>__inti__.py<\/code><\/pre>\n<ul>\n<li>\u8981\u5728\u5916\u754c\u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757,\u9700\u8981\u5728<code>__inti__.py<\/code>\u4e2d\u6307\u5b9a\u5bf9\u5916\u754c\u63d0\u4f9b\u7684\u6a21\u5757\u5217\u8868<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># \u4ece \u5f53\u524d\u76ee\u5f55 \u5bfc\u5165 \u6a21\u5757\u5217\u8868\nfrom . import ...\n# \u793a\u4f8b\nfrom .send import send_message<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u5236\u4f5c\u53d1\u5e03\u538b\u7f29\u5305<\/h3>\n<h4>1) \u521b\u5efa setup.py<\/h4>\n<ul>\n<li><code>setup.py<\/code> \u7684\u6587\u4ef6<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">from distutils.core import setup\n\nsetup(name=&quot;hm_message&quot;,  # \u5305\u540d\n      version=&quot;1.0&quot;,  # \u7248\u672c\n      description=&quot;ssm&#039;s \u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u6a21\u5757&quot;,  # \u63cf\u8ff0\u4fe1\u606f\n      long_description=&quot;\u5b8c\u6574\u7684\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u6a21\u5757&quot;,  # \u5b8c\u6574\u63cf\u8ff0\u4fe1\u606f\n      author=&quot;ssm&quot;,  # \u4f5c\u8005\n      author_email=&quot;1600767556@qq.com&quot;,  # \u4f5c\u8005\u90ae\u7bb1\n      url=&quot;www.shaoshaossm.github.io&quot;,  # \u4e3b\u9875\n      py_modules=[&quot;hm_message.send_message&quot;,\n                  &quot;hm_message.receive_message&quot;])<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<p>\u6709\u5173\u5b57\u5178\u53c2\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u9605\u5b98\u65b9\u7f51\u7ad9\uff1a<\/p>\n<p><a href=\"https:\/\/docs.python.org\/2\/distutils\/apiref.html\">https:\/\/docs.python.org\/2\/distutils\/apiref.html<\/a><\/p>\n<h4>2) \u6784\u5efa\u6a21\u5757<\/h4>\n<pre><code class=\"language-javascript\">$ python3 setup.py build<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>3) \u751f\u6210\u53d1\u5e03\u538b\u7f29\u5305<\/h4>\n<pre><code class=\"language-javascript\">$ python3 setup.py sdist<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u8981\u5236\u4f5c\u54ea\u4e2a\u7248\u672c\u7684\u6a21\u5757\uff0c\u5c31\u4f7f\u7528\u54ea\u4e2a\u7248\u672c\u7684\u89e3\u91ca\u5668\u6267\u884c\uff01 <\/p>\n<\/blockquote>\n<h4>\u5b89\u88c5\u6a21\u5757<\/h4>\n<pre><code class=\"language-javascript\">$ tar -zxvf hm_message-1.0.tar.gz \n\n$ sudo python3 setup.py install<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<p><strong>\u5378\u8f7d\u6a21\u5757<\/strong><\/p>\n<p>\u76f4\u63a5\u4ece\u5b89\u88c5\u76ee\u5f55\u4e0b\uff0c\u628a\u5b89\u88c5\u6a21\u5757\u7684 <strong>\u76ee\u5f55<\/strong> \u5220\u9664\u5c31\u53ef\u4ee5<\/p>\n<pre><code class=\"language-javascript\">$ cd \/usr\/local\/lib\/python3.5\/dist-packages\/\n$ sudo rm -r hm_message*<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4><code>pip<\/code> \u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757<\/h4>\n<ul>\n<li>\n<p>\u7b2c\u4e09\u65b9\u6a21\u5757<\/p>\n<p>\u901a\u5e38\u662f\u6307\u7531 <\/p>\n<p>\u77e5\u540d\u7684\u7b2c\u4e09\u65b9\u56e2\u961f<\/p>\n<p>\u5f00\u53d1\u7684<\/p>\n<p>\u5e76\u4e14\u88ab <\/p>\n<p>\u7a0b\u5e8f\u5458\u5e7f\u6cdb\u4f7f\u7528<\/p>\n<p>\u7684 <\/p>\n<pre><code>Python<\/code><\/pre>\n<p>\u5305 \/ \u6a21\u5757<\/p>\n<ul>\n<li>\u4f8b\u5982 <code>pygame<\/code> \u5c31\u662f\u4e00\u5957\u975e\u5e38\u6210\u719f\u7684 <strong>\u6e38\u620f\u5f00\u53d1\u6a21\u5757<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>pip<\/code> \u662f\u4e00\u4e2a\u73b0\u4ee3\u7684\uff0c\u901a\u7528\u7684 <code>Python<\/code> \u5305\u7ba1\u7406\u5de5\u5177<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4f9b\u4e86\u5bf9 <code>Python<\/code> \u5305\u7684\u67e5\u627e\u3001\u4e0b\u8f7d\u3001\u5b89\u88c5\u3001\u5378\u8f7d\u7b49\u529f\u80fd<\/p>\n<\/li>\n<\/ul>\n<p>\u5b89\u88c5\u548c\u5378\u8f7d\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\"># \u5c06\u6a21\u5757\u5b89\u88c5\u5230 Python 2.x \u73af\u5883\n$ sudo pip install pygame\n$ sudo pip uninstall pygame\n\n# \u5c06\u6a21\u5757\u5b89\u88c5\u5230 Python 3.x \u73af\u5883\n$ sudo pip3 install pygame\n$ sudo pip3 uninstall pygame<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>\u5728 <code>Mac<\/code> \u4e0b\u5b89\u88c5 <code>iPython<\/code><\/h4>\n<pre><code class=\"language-javascript\">$ sudo pip install ipython<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>\u5728 <code>Linux<\/code> \u4e0b\u5b89\u88c5 <code>iPython<\/code><\/h4>\n<pre><code class=\"language-javascript\">$ sudo apt install ipython\n$ sudo apt install ipython3<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>I\/O<\/h3>\n<h3>\u64cd\u4f5c\u6587\u4ef6\u7684\u51fd\u6570\/\u65b9\u6cd5<\/h3>\n<ul>\n<li>\u5728 <code>Python<\/code> \u4e2d\u8981\u64cd\u4f5c\u6587\u4ef6\u9700\u8981\u8bb0\u4f4f 1 \u4e2a\u51fd\u6570\u548c 3 \u4e2a\u65b9\u6cd5<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5e8f\u53f7<\/th>\n<th style=\"text-align: left;\">\u51fd\u6570\/\u65b9\u6cd5<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">01<\/td>\n<td style=\"text-align: left;\">open<\/td>\n<td style=\"text-align: left;\">\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4e14\u8fd4\u56de\u6587\u4ef6\u64cd\u4f5c\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">02<\/td>\n<td style=\"text-align: left;\">read<\/td>\n<td style=\"text-align: left;\">\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u5185\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">03<\/td>\n<td style=\"text-align: left;\">write<\/td>\n<td style=\"text-align: left;\">\u5c06\u6307\u5b9a\u5185\u5bb9\u5199\u5165\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">04<\/td>\n<td style=\"text-align: left;\">close<\/td>\n<td style=\"text-align: left;\">\u5173\u95ed\u6587\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li><code>open<\/code> \u51fd\u6570\u8d1f\u8d23\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4e14\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61<\/li>\n<li><code>read<\/code>\/<code>write<\/code>\/<code>close<\/code> \u4e09\u4e2a\u65b9\u6cd5\u90fd\u9700\u8981\u901a\u8fc7 <strong>\u6587\u4ef6\u5bf9\u8c61<\/strong> \u6765\u8c03\u7528<\/li>\n<\/ul>\n<h3>read \u65b9\u6cd5 \u2014\u2014 \u8bfb\u53d6\u6587\u4ef6<\/h3>\n<ul>\n<li>\n<pre><code>open<\/code><\/pre>\n<p>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff08\u6587\u4ef6\u540d\u533a\u5206\u5927\u5c0f\u5199\uff09<\/p>\n<ul>\n<li>\u5982\u679c\u6587\u4ef6 <strong>\u5b58\u5728<\/strong>\uff0c\u8fd4\u56de <strong>\u6587\u4ef6\u64cd\u4f5c\u5bf9\u8c61<\/strong><\/li>\n<li>\u5982\u679c\u6587\u4ef6 <strong>\u4e0d\u5b58\u5728<\/strong>\uff0c\u4f1a <strong>\u629b\u51fa\u5f02\u5e38<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>read<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027 <strong>\u8bfb\u5165<\/strong> \u5e76 <strong>\u8fd4\u56de<\/strong> \u6587\u4ef6\u7684 <strong>\u6240\u6709\u5185\u5bb9<\/strong><\/p>\n<\/li>\n<li>\n<pre><code>close<\/code><\/pre>\n<p>\u65b9\u6cd5\u8d1f\u8d23 <\/p>\n<p>\u5173\u95ed\u6587\u4ef6<\/p>\n<ul>\n<li>\u5982\u679c <strong>\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6<\/strong>\uff0c<strong>\u4f1a\u9020\u6210\u7cfb\u7edf\u8d44\u6e90\u6d88\u8017\uff0c\u800c\u4e14\u4f1a\u5f71\u54cd\u5230\u540e\u7eed\u5bf9\u6587\u4ef6\u7684\u8bbf\u95ee<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>read<\/code> \u65b9\u6cd5\u6267\u884c\u540e\uff0c\u4f1a\u628a <strong>\u6587\u4ef6\u6307\u9488<\/strong> \u79fb\u52a8\u5230 <strong>\u6587\u4ef6\u7684\u672b\u5c3e<\/strong><\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># 1. \u6253\u5f00 - \u6587\u4ef6\u540d\u9700\u8981\u6ce8\u610f\u5927\u5c0f\u5199\nfile = open(&quot;README&quot;)\n\n# 2. \u8bfb\u53d6\ntext = file.read()\nprint(text)\n\n# 3. \u5173\u95ed\nfile.close()<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u5f0f<\/h3>\n<ul>\n<li><code>open<\/code> \u51fd\u6570\u9ed8\u8ba4\u4ee5 <strong>\u53ea\u8bfb\u65b9\u5f0f<\/strong> \u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4e14\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61<\/li>\n<\/ul>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">f = open(&quot;\u6587\u4ef6\u540d&quot;, &quot;\u8bbf\u95ee\u65b9\u5f0f&quot;)<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u8bbf\u95ee\u65b9\u5f0f<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">r<\/td>\n<td style=\"text-align: left;\">\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u6587\u4ef6\u7684\u6307\u9488\u5c06\u4f1a\u653e\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u8fd9\u662f\u9ed8\u8ba4\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u629b\u51fa\u5f02\u5e38<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">w<\/td>\n<td style=\"text-align: left;\">\u4ee5\u53ea\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\u4f1a\u88ab\u8986\u76d6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">a<\/td>\n<td style=\"text-align: left;\">\u4ee5\u8ffd\u52a0\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u8be5\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u6587\u4ef6\u6307\u9488\u5c06\u4f1a\u653e\u5728\u6587\u4ef6\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">r+<\/td>\n<td style=\"text-align: left;\">\u4ee5\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u6587\u4ef6\u7684\u6307\u9488\u5c06\u4f1a\u653e\u5728\u6587\u4ef6\u7684\u5f00\u5934\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u629b\u51fa\u5f02\u5e38<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">w+<\/td>\n<td style=\"text-align: left;\">\u4ee5\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\u4f1a\u88ab\u8986\u76d6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">a+<\/td>\n<td style=\"text-align: left;\">\u4ee5\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u8be5\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u6587\u4ef6\u6307\u9488\u5c06\u4f1a\u653e\u5728\u6587\u4ef6\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u5199\u5165\u6587\u4ef6\u793a\u4f8b<\/li>\n<li>\u82e5\u6587\u4ef6\u4e2d\u6709\u4e2d\u6587\u9700\u52a0\u5165<code>encoding=&#039;utf-8&#039;<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># \u6253\u5f00\u6587\u4ef6\nf = open(&quot;README&quot;, &quot;w&quot;,encoding=&#039;utf-8&#039;)\n\nf.write(&quot;hello python\uff01\\n&quot;)\nf.write(&quot;\u4eca\u5929\u5929\u6c14\u771f\u597d&quot;)\n\n# \u5173\u95ed\u6587\u4ef6\nf.close()<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<p><code>readline<\/code> \u65b9\u6cd5<\/p>\n<ul>\n<li><code>readline<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u8bfb\u53d6\u4e00\u884c\u5185\u5bb9<\/li>\n<li>\u65b9\u6cd5\u6267\u884c\u540e\uff0c\u4f1a\u628a <strong>\u6587\u4ef6\u6307\u9488<\/strong> \u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u51c6\u5907\u518d\u6b21\u8bfb\u53d6<\/li>\n<\/ul>\n<p><strong>\u8bfb\u53d6\u5927\u6587\u4ef6\u7684\u6b63\u786e\u59ff\u52bf<\/strong><\/p>\n<pre><code class=\"language-javascript\"># \u6253\u5f00\u6587\u4ef6\nfile = open(&quot;README&quot;)\n\nwhile True:\n    # \u8bfb\u53d6\u4e00\u884c\u5185\u5bb9\n    text = file.readline()\n\n    # \u5224\u65ad\u662f\u5426\u8bfb\u5230\u5185\u5bb9\n    if not text:\n        break\n\n    # \u6bcf\u8bfb\u53d6\u4e00\u884c\u7684\u672b\u5c3e\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a `\\n`\n    print(text, end=&quot;&quot;)\n\n# \u5173\u95ed\u6587\u4ef6\nfile.close()<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u590d\u5236\u6587\u4ef6<\/h3>\n<h4>\u5c0f\u6587\u4ef6\u590d\u5236<\/h4>\n<ul>\n<li>\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u6587\u4ef6\uff0c\u8bfb\u53d6\u5b8c\u6574\u5185\u5bb9\uff0c\u5e76\u5199\u5165\u5230\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># 1. \u6253\u5f00\u6587\u4ef6\nfile_read = open(&quot;README&quot;)\nfile_write = open(&quot;README[\u590d\u4ef6]&quot;, &quot;w&quot;)\n\n# 2. \u8bfb\u53d6\u5e76\u5199\u5165\u6587\u4ef6\ntext = file_read.read()\nfile_write.write(text)\n\n# 3. \u5173\u95ed\u6587\u4ef6\nfile_read.close()\nfile_write.close()<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h4>\u5927\u6587\u4ef6\u590d\u5236<\/h4>\n<ul>\n<li>\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u5185\u5bb9\uff0c\u5e76\u987a\u5e8f\u5199\u5165\u5230\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\"># 1. \u6253\u5f00\u6587\u4ef6\nfile_read = open(&quot;README&quot;)\nfile_write = open(&quot;README[\u590d\u4ef6]&quot;, &quot;w&quot;)\n\n# 2. \u8bfb\u53d6\u5e76\u5199\u5165\u6587\u4ef6\nwhile True:\n    # \u6bcf\u6b21\u8bfb\u53d6\u4e00\u884c\n    text = file_read.readline()\n\n    # \u5224\u65ad\u662f\u5426\u8bfb\u53d6\u5230\u5185\u5bb9\n    if not text:\n        break\n\n    file_write.write(text)\n\n# 3. \u5173\u95ed\u6587\u4ef6\nfile_read.close()\nfile_write.close()<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<h3>\u6587\u4ef6\/\u76ee\u5f55\u64cd\u4f5c<\/h3>\n<h3>\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<pre><code class=\"language-javascript\"># \u5bfc\u5165\u524d\u63d0\nimport os<\/code><\/pre>\n<p>\u590d\u5236<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5e8f\u53f7<\/th>\n<th style=\"text-align: left;\">\u65b9\u6cd5\u540d<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<th style=\"text-align: left;\">\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">01<\/td>\n<td style=\"text-align: left;\">rename<\/td>\n<td style=\"text-align: left;\">\u91cd\u547d\u540d\u6587\u4ef6<\/td>\n<td style=\"text-align: left;\">os.rename(\u6e90\u6587\u4ef6\u540d, \u76ee\u6807\u6587\u4ef6\u540d)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">02<\/td>\n<td style=\"text-align: left;\">remove<\/td>\n<td style=\"text-align: left;\">\u5220\u9664\u6587\u4ef6<\/td>\n<td style=\"text-align: left;\">os.remove(\u6587\u4ef6\u540d)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u76ee\u5f55\u64cd\u4f5c<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5e8f\u53f7<\/th>\n<th style=\"text-align: left;\">\u65b9\u6cd5\u540d<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<th style=\"text-align: left;\">\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">01<\/td>\n<td style=\"text-align: left;\">listdir<\/td>\n<td style=\"text-align: left;\">\u76ee\u5f55\u5217\u8868<\/td>\n<td style=\"text-align: left;\">os.listdir(\u76ee\u5f55\u540d)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">02<\/td>\n<td style=\"text-align: left;\">mkdir<\/td>\n<td style=\"text-align: left;\">\u521b\u5efa\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\">os.mkdir(\u76ee\u5f55\u540d)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">03<\/td>\n<td style=\"text-align: left;\">rmdir<\/td>\n<td style=\"text-align: left;\">\u5220\u9664\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\">os.rmdir(\u76ee\u5f55\u540d)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">04<\/td>\n<td style=\"text-align: left;\">getcwd<\/td>\n<td style=\"text-align: left;\">\u83b7\u53d6\u5f53\u524d\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\">os.getcwd()<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">05<\/td>\n<td style=\"text-align: left;\">chdir<\/td>\n<td style=\"text-align: left;\">\u4fee\u6539\u5de5\u4f5c\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\">os.chdir(\u76ee\u6807\u76ee\u5f55)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">06<\/td>\n<td style=\"text-align: left;\">path.isdir<\/td>\n<td style=\"text-align: left;\">\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6<\/td>\n<td style=\"text-align: left;\">os.path.isdir(\u6587\u4ef6\u8def\u5f84)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>\u63d0\u793a\uff1a\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u64cd\u4f5c\u90fd\u652f\u6301 <strong>\u76f8\u5bf9\u8def\u5f84<\/strong> \u548c <strong>\u7edd\u5bf9\u8def\u5f84<\/strong> <\/p>\n<\/blockquote>\n<p><code>eva()<\/code> : \u5c06<strong>\u5b57\u7b26\u4e32<\/strong>\u5f53\u6210<strong>\u6709\u6548\u7684\u8868\u8fbe\u5f0f<\/strong>\u6765\u6c42\u503c\uff0c\u5e76<strong>\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c<\/strong> <\/p>\n<pre><code class=\"language-javascript\">input_str = input(&quot;\u8bf7\u8f93\u5165\u7b97\u672f\u9898\uff1a&quot;)\nprint(eval(input_str))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5 \u5728 Python \u4e2d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6cd5\u9a8c\u8bc1\uff1a \u5728 \u6807\u8bc6\u7b26 \/ \u6570\u636e \u540e\u8f93\u5165\u4e00\u4e2a .\uff0c\u7136\u540e\u6309\u4e0b TAB \u952e\uff0ciPython \u4f1a\u63d0\u793a   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[34],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8693"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8693"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8693\/revisions"}],"predecessor-version":[{"id":8699,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8693\/revisions\/8699"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8693"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}