{"id":66,"date":"2025-10-21T04:26:07","date_gmt":"2025-10-21T04:26:07","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=66"},"modified":"2025-12-17T07:47:22","modified_gmt":"2025-12-17T07:47:22","slug":"lesson-11-file-handling-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=66","title":{"rendered":"Lesson 11: File Handling in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To learn how to <strong>create, read, write, and manage files<\/strong> in Python using built-in file handling methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is File Handling?<\/strong><\/h2>\n\n\n\n<p>File handling allows Python programs to <strong>store and retrieve data from files<\/strong>.<br>Common file types: <strong>Text files (.txt)<\/strong>, <strong>CSV files (.csv)<\/strong>, <strong>JSON files (.json)<\/strong>, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Opening a File<\/strong><\/h2>\n\n\n\n<p>Use the <code>open()<\/code> function to open a file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"w\")  # w \u2192 write mode\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File Modes<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Mode<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>r<\/code><\/td><td>Read (default)<\/td><\/tr><tr><td><code>w<\/code><\/td><td>Write (creates new or overwrites)<\/td><\/tr><tr><td><code>a<\/code><\/td><td>Append<\/td><\/tr><tr><td><code>x<\/code><\/td><td>Create (fails if file exists)<\/td><\/tr><tr><td><code>r+<\/code><\/td><td>Read and write<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u270f\ufe0f <strong>3. Writing to a File<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"w\")\nfile.write(\"Hello, Python File Handling!\\n\")\nfile.write(\"This is a second line.\")\nfile.close()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd6 <strong>4. Reading from a File<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"r\")\ncontent = file.read()\nprint(content)\nfile.close()\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Python File Handling!\nThis is a second line.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>5. Reading Line by Line<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"r\")\nfor line in file:\n    print(line.strip())\nfile.close()\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Python File Handling!\nThis is a second line.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2795 <strong>6. Appending to a File<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"a\")\nfile.write(\"\\nAdding a new line.\")\nfile.close()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee1\ufe0f <strong>7. Using <code>with<\/code> Statement<\/strong><\/h2>\n\n\n\n<p>Automatically closes the file after operations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddee <strong>8. Other Useful File Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>read(size)<\/code><\/td><td>Reads <code>size<\/code> characters<\/td><td><code>file.read(10)<\/code><\/td><\/tr><tr><td><code>readline()<\/code><\/td><td>Reads one line<\/td><td><code>file.readline()<\/code><\/td><\/tr><tr><td><code>readlines()<\/code><\/td><td>Reads all lines into a list<\/td><td><code>file.readlines()<\/code><\/td><\/tr><tr><td><code>tell()<\/code><\/td><td>Returns current cursor position<\/td><td><code>file.tell()<\/code><\/td><\/tr><tr><td><code>seek(offset)<\/code><\/td><td>Moves cursor to position<\/td><td><code>file.seek(0)<\/code><\/td><\/tr><tr><td><code>truncate()<\/code><\/td><td>Truncates file<\/td><td><code>file.truncate(10)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>9. Real-Life Example \u2013 Logging Messages<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"log.txt\", \"a\") as log_file:\n    log_file.write(\"User logged in at 10:00 AM\\n\")\n\nwith open(\"log.txt\", \"r\") as log_file:\n    logs = log_file.read()\n    print(logs)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User logged in at 10:00 AM\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn how to create, read, write, and manage files in Python using built-in file handling methods. \ud83e\udde9 1. What Is File Handling? File handling allows Python programs to store and retrieve data from files.Common file types: Text files (.txt), CSV files (.csv), JSON files (.json), etc. \u2699\ufe0f 2. Opening a File [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}