{"id":79,"date":"2026-03-01T17:06:39","date_gmt":"2026-03-01T12:06:39","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=79"},"modified":"2026-03-13T13:30:45","modified_gmt":"2026-03-13T08:30:45","slug":"reading-text-files","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/","title":{"rendered":"Reading Text Files"},"content":{"rendered":"\n<p>Reading text files is an important skill in Python. It allows you to work with stored data such as reports, logs, configurations, and datasets.<\/p>\n\n\n\n<p>Python provides built-in functions to open and read files easily.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>OPENING A FILE<\/strong><\/h1>\n\n\n\n<p>To read a file, use the <code>open()<\/code> function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"r\")<\/pre>\n\n\n\n<p>\u2022 <code>\"example.txt\"<\/code> \u2192 File name<br>\u2022 <code>\"r\"<\/code> \u2192 Read mode<\/p>\n\n\n\n<p>After opening the file, you can read its content.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>READING FILE CONTENT<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1. read() \u2013 Reads the entire file<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"r\")<br>content = file.read()<br>print(content)<br>file.close()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. readline() \u2013 Reads one line at a time<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"r\")<br>line = file.readline()<br>print(line)<br>file.close()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. readlines() \u2013 Reads all lines into a list<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"r\")<br>lines = file.readlines()<br>print(lines)<br>file.close()<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>USING WITH STATEMENT (BEST PRACTICE)<\/strong><\/h1>\n\n\n\n<p>Using <code>with<\/code> automatically closes the file after use.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"r\") as file:<br>    content = file.read()<br>    print(content)<\/pre>\n\n\n\n<p>This is the recommended way to handle files.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>READING FILE LINE BY LINE<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"r\") as file:<br>    for line in file:<br>        print(line.strip())<\/pre>\n\n\n\n<p><code>.strip()<\/code> removes extra spaces or newline characters.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>FILE MODES<\/strong><\/h1>\n\n\n\n<p>\u2022 <code>\"r\"<\/code> \u2192 Read (default)<br>\u2022 <code>\"w\"<\/code> \u2192 Write (overwrites file)<br>\u2022 <code>\"a\"<\/code> \u2192 Append<br>\u2022 <code>\"x\"<\/code> \u2192 Create new file<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>HANDLING FILE NOT FOUND ERROR<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    with open(\"example.txt\", \"r\") as file:<br>        print(file.read())<br>except FileNotFoundError:<br>    print(\"File not found.\")<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Always close the file (or use <code>with<\/code>)<br>\u2022 Use try-except to handle errors<br>\u2022 Read large files line by line to save memory<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY FILE READING IS IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Used in data analysis<br>\u2022 Used in report generation<br>\u2022 Important for automation<br>\u2022 Required in real-world applications<\/p>\n\n\n\n<p>Understanding how to read text files is essential for working with external data in Python.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > File Handling > Reading Text Files<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773389199914\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":31,"template":"","class_list":["post-79","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reading Text Files - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reading Text Files - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T08:30:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/reading-text-files\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/reading-text-files\\\/\",\"name\":\"Reading Text Files - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:06:39+00:00\",\"dateModified\":\"2026-03-13T08:30:45+00:00\",\"description\":\"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/reading-text-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/reading-text-files\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/reading-text-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > File Handling > Reading Text Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reading Text Files - One Language. Endless Possibilities","description":"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement","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:\/\/gigz.pk\/python\/lesson\/reading-text-files\/","og_locale":"en_US","og_type":"article","og_title":"Reading Text Files - One Language. Endless Possibilities","og_description":"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement","og_url":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T08:30:45+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/","url":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/","name":"Reading Text Files - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:06:39+00:00","dateModified":"2026-03-13T08:30:45+00:00","description":"Learn Python file reading: open, read, and process text files safely with read(), readline(), readlines(), and with statement","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/reading-text-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > File Handling > Reading Text Files"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}