{"id":4194,"date":"2019-11-23T09:33:36","date_gmt":"2019-11-23T08:33:36","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=4194"},"modified":"2019-11-24T07:39:06","modified_gmt":"2019-11-24T06:39:06","slug":"open-read-write-and-append-content-files-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/open-read-write-and-append-content-files-with-python\/","title":{"rendered":"Open, read &#038; write (and append) content\/files with Python"},"content":{"rendered":"<h2>How to open a file?<\/h2>\n<p>With the following code, you open and read the content of a file<\/p>\n<pre class=\"lang:default decode:true\">with open(\"text.txt\", \"r\") as file:\r\n  print(file.read())<\/pre>\n<p>You obtain a list of the lines in the file with this<\/p>\n<pre class=\"lang:default decode:true \">with open(\"text.txt\", \"r\") as file:\r\n  print(file.readlines())<\/pre>\n<p>You read one line at the time with this<\/p>\n<pre class=\"lang:default decode:true\">with open(\"text.txt\", \"r\") as file:\r\n  print(file.readline())<\/pre>\n<p>You can print the lines simply like this:<\/p>\n<pre class=\"lang:default decode:true \">with open(\"text.txt\", \"r\") as file:\r\n  for line in file:\r\n    print(line)<\/pre>\n<h2>Write a file<\/h2>\n<pre class=\"lang:default decode:true\">with open(\"text.txt\", \"w\") as file:\r\n  file.write(\"Hello world\")<\/pre>\n<h2>Append a text to a file<\/h2>\n<pre class=\"lang:default decode:true \">with open(\"text.txt\", \"a\") as file:\r\n  file.write(\"This does not cancel the file content\")<\/pre>\n<h2>Live code video about opening and writing files<\/h2>\n<p><iframe loading=\"lazy\" title=\"Open, read, write, append Text files with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/BFnfmgKK-Ls?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\nhttps:\/\/pythonprogramming.altervista.org\/how-to-create-a-list-of-tuple-from-a-string-pythonically\/\r\n\r\n<script>\r\n\tvar title = \"Basic Stuffs in Python\"\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/open-read-write-and-append-content-files-with-python\/\", \"Read & write files\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/how-to-create-a-list-of-tuple-from-a-string-pythonically\/\",\"Write tuple easily\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/le-basi-luso-delle-liste-1\/\",\"Liste 1 (in italiano)\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/join-two-or-more-lists\/\",\"How to join lists in Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-a-file-at-ease\/\",\"Create files easily\"]\r\n\t\t];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\n","protected":false},"excerpt":{"rendered":"Read. open, write and append content to a file with Python\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/open-read-write-and-append-content-files-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":4195,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[210,242],"tags":[62,9,335,4,634,336],"class_list":["post-4194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-files","tag-append","tag-files","tag-open","tag-python","tag-rea","tag-write"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=4194"}],"version-history":[{"count":3,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4194\/revisions"}],"predecessor-version":[{"id":4198,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4194\/revisions\/4198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/4195"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=4194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=4194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=4194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}