{"id":391,"date":"2026-03-08T10:50:51","date_gmt":"2026-03-08T10:50:51","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=391"},"modified":"2026-03-08T10:50:51","modified_gmt":"2026-03-08T10:50:51","slug":"%f0%9f%93%98-lesson-11-file-handling-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=391","title":{"rendered":"\ud83d\udcd8 Lesson 11: File Handling in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Below is a <strong>FULL beginner-friendly Lesson 11<\/strong>, explaining <strong>File Handling in Python<\/strong> with <strong>clear explanations, examples, and outputs<\/strong>.<br>File handling is important because programs often <strong>store and read data from files<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcd8 Lesson 11: File Handling in Python<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 1. What is File Handling?<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>File handling<\/strong> allows Python programs to:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 Read data from files<br>\u2714 Write data to files<br>\u2714 Store information permanently<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example uses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Saving user data<\/li>\n\n\n\n<li>Logging system activity<\/li>\n\n\n\n<li>Reading configuration files<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 2. Opening a File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Python uses the <code>open()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"filename\", \"mode\")\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\">File Modes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Mode<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>\"r\"<\/code><\/td><td>Read file<\/td><\/tr><tr><td><code>\"w\"<\/code><\/td><td>Write file (overwrites)<\/td><\/tr><tr><td><code>\"a\"<\/code><\/td><td>Append to file<\/td><\/tr><tr><td><code>\"x\"<\/code><\/td><td>Create new file<\/td><\/tr><tr><td><code>\"b\"<\/code><\/td><td>Binary mode<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 3. Reading a File<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Read Entire File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Assume file <strong>data.txt<\/strong> contains:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Python Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"r\")\n\ncontent = file.read()\n\nprint(content)\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"r\"<\/code> mode reads file<\/li>\n\n\n\n<li><code>read()<\/code> reads entire content<\/li>\n\n\n\n<li><code>close()<\/code> closes file<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 4. Reading File Line by Line<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"r\")\n\nfor line in file:\n    print(line)\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<br>Loop reads <strong>each line one by one<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 5. Writing to a File<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"w\")\n\nfile.write(\"Sameer\\n\")\nfile.write(\"Ayesha\\n\")\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"w\"<\/code> mode <strong>overwrites existing file<\/strong><\/li>\n\n\n\n<li><code>\\n<\/code> creates a new line<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">File Content After Running<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 6. Appending to a File<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"a\")\n\nfile.write(\"Saddam\\n\")\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File Content<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<br><code>\"a\"<\/code> mode <strong>adds content without deleting old data<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 7. Reading One Line<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"r\")\n\nline = file.readline()\n\nprint(line)\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<br><code>readline()<\/code> reads <strong>one line only<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 8. Reading All Lines as List<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"data.txt\", \"r\")\n\nlines = file.readlines()\n\nprint(lines)\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Sameer\\n', 'Ayesha\\n', 'Saddam\\n']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<br>Each line becomes a <strong>list element<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 9. Using <code>with open()<\/code> (Best Practice)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Python automatically closes file.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 7<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 No need to call <code>close()<\/code><br>\u2714 Cleaner and safer code<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 10. Creating a New File<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example 8<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"students.txt\", \"x\")\n\nfile.write(\"Chandini\")\n\nfile.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"x\"<\/code> creates a new file<\/li>\n\n\n\n<li>Error occurs if file already exists<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 11. Real-Life Example<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Student Record System<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter student name: \")\n\nwith open(\"students.txt\", \"a\") as file:\n    file.write(name + \"\\n\")\n\nprint(\"Student saved successfully\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter student name: Gousya\nStudent saved successfully\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 12. Common File Methods<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>read()<\/code><\/td><td>Reads entire file<\/td><\/tr><tr><td><code>readline()<\/code><\/td><td>Reads one line<\/td><\/tr><tr><td><code>readlines()<\/code><\/td><td>Reads all lines<\/td><\/tr><tr><td><code>write()<\/code><\/td><td>Writes data<\/td><\/tr><tr><td><code>close()<\/code><\/td><td>Closes file<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd39 13. Common Mistakes<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Forgetting to close file<br>\u274c Using <code>\"w\"<\/code> accidentally (deletes content)<br>\u274c File not found error<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a FULL beginner-friendly Lesson 11, explaining File Handling in Python with clear explanations, examples, and outputs.File handling is important because programs often store and read data from files. \ud83d\udcd8 Lesson 11: File Handling in Python \ud83d\udd39 1. What is File Handling? File handling allows Python programs to: \u2714 Read data from files\u2714 Write [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/391","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=391"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":392,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions\/392"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}