{"id":1138,"date":"2021-10-06T22:51:28","date_gmt":"2021-10-06T13:51:28","guid":{"rendered":"http:\/\/192.168.20.1\/?p=1138"},"modified":"2021-10-07T06:47:53","modified_gmt":"2021-10-06T21:47:53","slug":"python-read-readline-readlines","status":"publish","type":"post","link":"https:\/\/rlab.cavtime.net\/?p=1138","title":{"rendered":"Python read() \/ readline() \/ readlines()"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>Python \u306e read() \/ readline() \/ readlines() \u306e\u76f8\u9055\u70b9\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<h2>Environment<\/h2>\n<pre>\r\n$ cat \/etc\/os-release\r\nNAME=\"Ubuntu\"\r\nVERSION=\"21.04 (Hirsute Hippo)\"\r\nID=ubuntu\r\nID_LIKE=debian\r\nPRETTY_NAME=\"Ubuntu 21.04\"\r\nVERSION_ID=\"21.04\"\r\nHOME_URL=\"https:\/\/www.ubuntu.com\/\"\r\nSUPPORT_URL=\"https:\/\/help.ubuntu.com\/\"\r\nBUG_REPORT_URL=\"https:\/\/bugs.launchpad.net\/ubuntu\/\"\r\nPRIVACY_POLICY_URL=\"https:\/\/www.ubuntu.com\/legal\/terms-and-policies\/privacy-policy\"\r\nVERSION_CODENAME=hirsute\r\nUBUNTU_CODENAME=hirsute\r\n$ uname -a\r\nLinux x201 5.11.0-37-generic #41-Ubuntu SMP Mon Sep 20 16:39:20 UTC 2021 x86_64 x86_64 x86_64 GNU\/Linux\r\n$ python3 --version\r\nPython 3.9.5\r\n$ \r\n<\/pre>\n<h2>file<\/h2>\n<pre>$ cat interface.log\r\ninterface GigabitEthernet0\/0\r\n ip address 203.0.113.102 255.255.255.252\r\n duplex auto\r\n speed auto\r\n media-type rj45\r\n$\r\n<\/pre>\n<h2>read()<\/h2>\n<pre>config_file = open(\"interface.log\",\"r\")\r\n<span style=\"color: #ff0000;\">config_cmds = config_file.read()<\/span>\r\nprint(config_cmds)\r\nconfig_file.close()\r\n<\/pre>\n<pre>$ python3 config.py\r\ninterface GigabitEthernet0\/0\r\n ip address 203.0.113.102 255.255.255.252\r\n duplex auto\r\n speed auto\r\n media-type rj45\r\n$<\/pre>\n<h2>readline()<\/h2>\n<pre>config_file = open(\"interface.log\",\"r\")\r\n<span style=\"color: #ff0000;\">config_cmds = config_file.readline()<\/span>\r\nprint(config_cmds)\r\nconfig_file.close()\r\n<\/pre>\n<pre>$ python3 config.py\r\ninterface GigabitEthernet0\/0\r\n\r\n$ \r\n<\/pre>\n<h2>readlines()<\/h2>\n<pre>config_file = open(\"interface.log\",\"r\")\r\n<span style=\"color: #ff0000;\">config_cmds = config_file.readlines()<\/span>\r\nprint(config_cmds)\r\nconfig_file.close()\r\n<\/pre>\n<pre>['interface GigabitEthernet0\/0\\n', ' ip address 203.0.113.102 255.255.255.252\\n', ' duplex auto\\n', ' speed auto\\n', ' media-type rj45']\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overview Python \u306e read() \/ readline() \/ readlines() \u306e\u76f8\u9055\u70b9\u3092\u78ba\u8a8d\u3059\u308b\u3002 Environment $ cat \/etc\/os-release NAME=&#8221;Ubuntu&#8221; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-1138","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/posts\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1138"}],"version-history":[{"count":4,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions"}],"predecessor-version":[{"id":1142,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions\/1142"}],"wp:attachment":[{"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rlab.cavtime.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}