{"id":19,"date":"2016-01-06T15:56:10","date_gmt":"2016-01-06T15:56:10","guid":{"rendered":"http:\/\/mr-west.uk\/python\/?page_id=19"},"modified":"2022-12-01T15:24:42","modified_gmt":"2022-12-01T15:24:42","slug":"saving-to-text-files","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/","title":{"rendered":"Python  Reading from \/ Writing to Text Files"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Read file<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Reading a file and print each line<\/h3>\n<p>Often you will want to read in data from a text file and do something with each line of data. For this we can use the in-built open function.<\/p>\n<p>Let&#8217;s say we have text file that contains 3 words, one on each line. We can use open() to read the file contents into Python<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<p>We can load this file and print out each line in the file:<\/p>\n<p><strong>Code<\/strong><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">f  <span style=\"color: #666666;\">=<\/span> <span style=\"color: #aa22ff;\">open<\/span>(<span style=\"color: #bb4444;\">'animals.txt'<\/span>,<span style=\"color: #bb4444;\">'r'<\/span>)\r\n<span style=\"color: #aa22ff; font-weight: bold;\">for<\/span> animal <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> f:\r\n    <span style=\"color: #aa22ff; font-weight: bold;\">print<\/span>(animal<span style=\"color: #666666;\">.<\/span>strip(<span style=\"color: #bb4444;\">'<\/span><span style=\"color: #bb6622; font-weight: bold;\">\\n<\/span><span style=\"color: #bb4444;\">'<\/span>))<\/pre>\n<\/div>\n<p><strong>Output<\/strong><\/p>\n<pre>cat dog mouse<\/pre>\n<ul>\n<li>Note how when we open a file for reading we use &#8216;r&#8217; read mode.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>Reading a single line file<\/strong><\/p>\n<p>If your file only has a single line and you want to read it in, then use readline()<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">f <span style=\"color: #666666;\">=<\/span> <span style=\"color: #008000;\">open<\/span>(<span style=\"color: #ba2121;\">'highscore.txt'<\/span>,<span style=\"color: #ba2121;\">'r'<\/span>)\r\nhigh_score <span style=\"color: #666666;\">=<\/span> f<span style=\"color: #666666;\">.<\/span>readline()\r\n<span style=\"color: #008000; font-weight: bold;\">print<\/span>(<span style=\"color: #ba2121;\">'High Score:'<\/span>,high_score)\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Read to list<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Reading data from a text file into a list<\/h3>\n<p>Most of the time you will want to read the file into a list. This is simple and easy to achieve:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">f  <span style=\"color: #666666;\">=<\/span> <span style=\"color: #aa22ff;\">open<\/span>(<span style=\"color: #bb4444;\">'animals.txt'<\/span>,<span style=\"color: #bb4444;\">'r'<\/span>)\r\n\r\nanimal_list <span style=\"color: #666666;\">=<\/span> []\r\n\r\n<span style=\"color: #aa22ff; font-weight: bold;\">for<\/span> animal <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> f:\r\n    animal_list<span style=\"color: #666666;\">.<\/span>append(animal<span style=\"color: #666666;\">.<\/span>strip(<span style=\"color: #bb4444;\">'<\/span><span style=\"color: #bb6622; font-weight: bold;\">\\n<\/span><span style=\"color: #bb4444;\">'<\/span>))\r\n\r\n<span style=\"color: #aa22ff; font-weight: bold;\">print<\/span>(animal_list)\r\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>['cat', 'dog', 'mouse']<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Write<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Writing to Text Files<\/h3>\n<p>Sometimes we want to save data to a file. Here we open the file in &#8216;w&#8217; write mode and use the write() method.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">f <span style=\"color: #666666;\">=<\/span> <span style=\"color: #008000;\">open<\/span>(<span style=\"color: #ba2121;\">'birds.txt'<\/span>,<span style=\"color: #ba2121;\">'w'<\/span>)\r\nbirds <span style=\"color: #666666;\">=<\/span> [<span style=\"color: #ba2121;\">\"robin\"<\/span>,<span style=\"color: #ba2121;\">\"sparrow\"<\/span>,<span style=\"color: #ba2121;\">\"eagle\"<\/span>]\r\n\r\n<span style=\"color: #008000; font-weight: bold;\">for<\/span> bird <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> birds:\r\n    f<span style=\"color: #666666;\">.<\/span>write(bird <span style=\"color: #666666;\">+<\/span> <span style=\"color: #ba2121;\">'<\/span><span style=\"color: #bb6622; font-weight: bold;\">\\n<\/span><span style=\"color: #ba2121;\">'<\/span>)\r\n\r\nf<span style=\"color: #666666;\">.<\/span>close()<\/pre>\n<\/div>\n<p><strong>Output file:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1385\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/writing-to-a-text-file-in-python-1.png\" alt=\"\" width=\"734\" height=\"235\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/writing-to-a-text-file-in-python-1.png 734w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/writing-to-a-text-file-in-python-1-300x96.png 300w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><\/p>\n<p><strong>Notes<\/strong><\/p>\n<ul>\n<li>If you want to completely overwrite any data previously saved into the file use &#8216;w&#8217;. If you just want to append(add) to the existing contents of the file use &#8216;w&#8217;<\/li>\n<li>make sure you call f.close() after you have finishing writing all the data otherwise it won&#8217;t save the changes.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Videos<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Reading Text Files Video Tutorial<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/6BfmLoUGGIo\" width=\"850\" height=\"600\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/h3>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/0B4M7J3ubGpimeUZxMEFTYVhGV0U\/view\">Can&#8217;t access YouTube? Click here for the Google Drive version<\/a><\/p>\n<hr \/>\n<h3>Writing Text Files Video Tutorial<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/RZV2Pn-MSLI\" width=\"850\" height=\"600\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/h3>\n<p><a href=\"https:\/\/drive.google.com\/open?id=0B4M7J3ubGpimaFhWYzZSMzJBMHc\">Can&#8217;t access YouTube? Click here for the Google Drive Version<\/a><\/p>\n\n<\/div><h2 class=\"tabtitle\">Pros &amp; Cons<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Pro &amp; Cons of text files<\/h3>\n<p><strong>It is great for storing:<\/strong><\/p>\n<div class=\"arconix-list\" data-arconix-icon=\"fa-chevron-circle-right\" data-arconix-color=\"black\">\n<ul>\n<li>Variables (like strings and integers)<\/li>\n<li>Simple 1-dimensional lists (such as a shopping list)<\/li>\n<\/ul>\n<\/div>\n<p><strong>It is not good for:<\/strong><\/p>\n<div class=\"arconix-list\" data-arconix-icon=\"fa-chevron-circle-right\" data-arconix-color=\"black\">\n<ul>\n<li>Dictionaries (<a href=\"http:\/\/www.pitt.edu\/~naraehan\/python2\/pickling.html\">use pickle instead<\/a>)<\/li>\n<li>2 Dimensional lists ( <a href=\"https:\/\/automatetheboringstuff.com\/chapter14\/\">use CSV writer instead<\/a>)<\/li>\n<li>Complex relational data \u00a0(<a href=\"http:\/\/learnlearn.uk\/sql\/\">use a database instead<\/a>)<\/li>\n<\/ul>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Challenge 33<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 33 &#8211; Password Checker<\/h3>\n<p><strong>Bronze<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-874\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png\" alt=\"\" width=\"29\" height=\"29\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star.png 274w\" sizes=\"(max-width: 29px) 100vw, 29px\" \/>\u00a0Read in the list of 100 most common passwords from the file <a href=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2022\/11\/passwords.txt\"><span style=\"color: #0000ff;\">passwords.txt<\/span> <\/a>into a list called common_passwords.<\/p>\n<p><strong>Silver<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-875\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0Ask the user to input a test password and let them know if their password is in the list.<\/p>\n<p><strong>Gold<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-876\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 Get your program to repeat asking the user to enter a password until it is not in the list of common password<\/p>\n\n<\/div><h2 class=\"tabtitle\">34<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 34 &#8211; Shopping List Saver<\/h3>\n<p><strong>Bronze<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-874\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png\" alt=\"\" width=\"29\" height=\"29\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star.png 274w\" sizes=\"(max-width: 29px) 100vw, 29px\" \/>\u00a0 Write a program that asks the user to enter 10 items to add a list called shopping.<\/p>\n<p><strong>Silver<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-875\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 Get the program to save the list to a text file called shopping.txt<\/p>\n<p><strong>Gold<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-876\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 Adapt the program so that the program keeps adding items to the shopping list until the user inputs an empty string.<\/p>\n\n<\/div><h2 class=\"tabtitle\">35<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 35 &#8211; Square Numbers<\/h3>\n<p><strong>Bronze<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-874\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png\" alt=\"\" width=\"29\" height=\"29\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star.png 274w\" sizes=\"(max-width: 29px) 100vw, 29px\" \/>\u00a0 Write a program that reads in a list of numbers from the file <a href=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2022\/11\/numbers.txt\"><span style=\"color: #0000ff;\">numbers.txt<\/span> <\/a>and a list called <strong>numbers<\/strong><\/p>\n<p><strong>Silver<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-875\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 For each number it should calculate the square of the number and add the square of the number to a list called <strong>squares<\/strong>.<\/p>\n<p><strong>Gold<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-876\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 The program should then write the square numbers to a text file called <span style=\"color: #0000ff;\">square_numbers.txt<\/span>.<\/p>\n\n<\/div><h2 class=\"tabtitle\">36<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 36 &#8211; &#x1f49a; Handling non-ASCII characters\u00a0&#x1f49a;<\/h3>\n<h3>Resources<\/h3>\n<p><strong>\u00a0Bronze<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-874\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png\" alt=\"\" width=\"29\" height=\"29\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star.png 274w\" sizes=\"(max-width: 29px) 100vw, 29px\" \/>\u00a0Write a program that reads the <a href=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2022\/12\/unicode_characters.txt\">following non-ASCII character file<\/a> into python and prints out the list of words\/emojis. You will need to set the encoding to UTF8 when opening the file\u00a0 e.g.\u00a0<em> f = open(&#8220;eg.txt , &#8220;r&#8221;, encoding = &#8220;utf-8&#8221;)<\/em><\/p>\n<p><strong>Silver<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-875\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 For the first character in each\u00a0 line print out the character Unicode value , using the<a href=\"https:\/\/www.w3schools.com\/python\/ref_func_ord.asp\"> ord() function.<\/a><\/p>\n<p><strong>Gold<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-876\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0 Add Unicode emoji art to your python program to make it look &#x1f63b;&#x1f63b;&#x1f63b;bling-tastic\u00a0&#x1f63b;&#x1f63b;&#x1f63b;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Reading a file and print each line Often you will want to read in data from a text file and do something with each line of data. For this we can use the in-built open function. Let&#8217;s say we have text file that contains 3 words, one on each line. We can use open() to&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python  Reading from \/ Writing to Text Files<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"on","neve_meta_content_width":85,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Reading from \/ Writing to Text Files - Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Reading from \/ Writing to Text Files - Python\" \/>\n<meta property=\"og:description\" content=\"Reading a file and print each line Often you will want to read in data from a text file and do something with each line of data. For this we can use the in-built open function. Let&#8217;s say we have text file that contains 3 words, one on each line. We can use open() to&hellip;&nbsp;Read More &raquo;Python Reading from \/ Writing to Text Files\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-01T15:24:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/writing-to-a-text-file-in-python-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\",\"name\":\"Python Reading from \/ Writing to Text Files - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2016-01-06T15:56:10+00:00\",\"dateModified\":\"2022-12-01T15:24:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Reading from \/ Writing to Text Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Reading from \/ Writing to Text Files - Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/","og_locale":"en_GB","og_type":"article","og_title":"Python Reading from \/ Writing to Text Files - Python","og_description":"Reading a file and print each line Often you will want to read in data from a text file and do something with each line of data. For this we can use the in-built open function. Let&#8217;s say we have text file that contains 3 words, one on each line. We can use open() to&hellip;&nbsp;Read More &raquo;Python Reading from \/ Writing to Text Files","og_url":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/","og_site_name":"Python","article_modified_time":"2022-12-01T15:24:42+00:00","og_image":[{"url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/writing-to-a-text-file-in-python-1.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/","url":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/","name":"Python Reading from \/ Writing to Text Files - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2016-01-06T15:56:10+00:00","dateModified":"2022-12-01T15:24:42+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/saving-to-text-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/saving-to-text-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Python Reading from \/ Writing to Text Files"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Reading a file and print each line Often you will want to read in data from a text file and do something with each line of data. For this we can use the in-built open function. Let&#8217;s say we have text file that contains 3 words, one on each line. We can use open() to&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/19"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":40,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/19\/revisions"}],"predecessor-version":[{"id":1425,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/19\/revisions\/1425"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}