{"id":217,"date":"2019-12-26T23:20:00","date_gmt":"2019-12-26T23:20:00","guid":{"rendered":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/"},"modified":"2019-12-26T23:20:00","modified_gmt":"2019-12-26T23:20:00","slug":"python-tutorial-series-files","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/","title":{"rendered":"Python Tutorial Series &#8211; Files"},"content":{"rendered":"<p>In Python, files are opened with the open command. The open command takes two parameters.<br \/>\nThe first parameter is the file name. The second parameter is the mode to open the file in:<\/p>\n<pre><\/pre>\n<p>\nr &#8211; Open the file for reading. An error is thrown if the file does not exist.<br \/>\na &#8211; Open the file for appending to the end of it. The file is created if it doesn&#8217;t exist.<br \/>\nw &#8211; Open the file for writing to it. The file is created if it doesn&#8217;t exist.<br \/>\nx &#8211; Create the file. An error is thrown if the file exists.<\/p>\n<p>t &#8211; Open the file as a text file. By default, files are opened in text mode.<br \/>\nb &#8211; Open the file as a binary file.<\/p>\n<p><\/p>\n<h2>\nText Files<\/h2>\n<p><\/p>\n<h3>\nReading<\/h3>\n<p>Reading a file using Python is done as follows:<\/p>\n<pre><\/pre>\n<p>\nf = open(&#8216;test.txt&#8217;, &#8216;r&#8217;)<br \/>\nf.close()<\/p>\n<p>\nIn the above example, f.close(), will close the file when it&#8217;s done being read from.<br \/>\nFiles may also be opened using a with statement. When using the with statement, f.close() is automatically called:<\/p>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as file:<br \/>\n&nbsp; &nbsp; data = file.read()<\/p>\n<p>\nTo read from a file, do the following:<\/p>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as f:<br \/>\n&nbsp; &nbsp; data = f.read()<\/p>\n<p>\nRead a certain number of characters:<\/p>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as f:<br \/>\n&nbsp; &nbsp; data = f.read()<\/p>\n<p>\nRead a single line:<\/p>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as f:<br \/>\n&nbsp; &nbsp; data = f.readline()<\/p>\n<p>\nRead the entire file:<\/p>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;r&#8217;) as f:<br \/>\n&nbsp; &nbsp; for data in f:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(data)<\/p>\n<p><\/p>\n<h3>\nWriting<\/h3>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;w&#8217;) as f:<br \/>\n&nbsp; &nbsp; f.write(&#8216;Writing some text content in a file&#8217;)<\/p>\n<p><\/p>\n<h2>\nBinary Files<\/h2>\n<h3>\nReading<\/h3>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;rb&#8217;) as f:<br \/>\n&nbsp; &nbsp; data = f.read()<\/p>\n<p><\/p>\n<h3>\nWriting<\/h3>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;wb&#8217;) as f:<br \/>\n&nbsp; &nbsp; f.write(&#8220;Writing some binary content in a filen&#8221;.encode(&#8216;utf8&#8217;))<\/p>\n<p><\/p>\n<h3>\nGo to a position<\/h3>\n<pre><\/pre>\n<p>\nwith open(&#8216;test.txt&#8217;, &#8216;rb&#8217;) as f:<br \/>\n&nbsp; &nbsp; data = f.seek(10, 20)<\/p>\n<p>\n<a href=\"https:\/\/amzn.to\/2EimtFd\" rel=\"noopener\" target=\"_blank\">More Python<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1593276036&amp;asins=1593276036&amp;linkId=49e81436e850d4eb917cf05eb7bb46a9&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1631595814&amp;asins=1631595814&amp;linkId=ab04bfbaa6f7474875f3b72a0ce3fc64&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1119363020&amp;asins=1119363020&amp;linkId=2d2671dadd845a42d1ab35c0ea47ec7a&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, files are opened with the open command. The open command takes two parameters. The first parameter is the file name. The second parameter is the mode to open the file in: r &#8211; Open the file for reading. An error is thrown if the file does not exist. a &#8211; Open the file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,343,341],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-python","category-python-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Tutorial Series - Files - Zofxare Blog Home<\/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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tutorial Series - Files - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"In Python, files are opened with the open command. The open command takes two parameters. The first parameter is the file name. The second parameter is the mode to open the file in: r &#8211; Open the file for reading. An error is thrown if the file does not exist. a &#8211; Open the file [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-26T23:20:00+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"Python Tutorial Series &#8211; Files\",\"datePublished\":\"2019-12-26T23:20:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/\"},\"wordCount\":350,\"commentCount\":0,\"articleSection\":[\"python\",\"python tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/\",\"name\":\"Python Tutorial Series - Files - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-26T23:20:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tutorial Series &#8211; Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tutorial Series - Files - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/","og_locale":"en_US","og_type":"article","og_title":"Python Tutorial Series - Files - Zofxare Blog Home","og_description":"In Python, files are opened with the open command. The open command takes two parameters. The first parameter is the file name. The second parameter is the mode to open the file in: r &#8211; Open the file for reading. An error is thrown if the file does not exist. a &#8211; Open the file [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-26T23:20:00+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"Python Tutorial Series &#8211; Files","datePublished":"2019-12-26T23:20:00+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/"},"wordCount":350,"commentCount":0,"articleSection":["python","python tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/","name":"Python Tutorial Series - Files - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-26T23:20:00+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Tutorial Series &#8211; Files"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":0,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}