{"id":83,"date":"2026-03-01T17:24:46","date_gmt":"2026-03-01T12:24:46","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=83"},"modified":"2026-03-14T07:56:28","modified_gmt":"2026-03-14T02:56:28","slug":"file-modes","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/","title":{"rendered":"File Modes"},"content":{"rendered":"\n<p>File modes define how a file should be opened \u2014 whether for reading, writing, appending, or both.<\/p>\n\n\n\n<p>When using the <code>open()<\/code> function, the second parameter specifies the mode.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"r\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>COMMON FILE MODES<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Read Mode (<code>\"r\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens a file for reading<\/li>\n\n\n\n<li>Default mode<\/li>\n\n\n\n<li>Gives error if file does not exist<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"r\") as file:<br>    content = file.read()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Write Mode (<code>\"w\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens a file for writing<\/li>\n\n\n\n<li>Creates file if it does not exist<\/li>\n\n\n\n<li>Overwrites file if it already exists<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"w\") as file:<br>    file.write(\"Hello World\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Append Mode (<code>\"a\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens file for adding content<\/li>\n\n\n\n<li>Creates file if it does not exist<\/li>\n\n\n\n<li>Does NOT delete existing content<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"a\") as file:<br>    file.write(\"New Line\\n\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Exclusive Creation Mode (<code>\"x\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creates a new file<\/li>\n\n\n\n<li>Gives error if file already exists<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"newfile.txt\", \"x\") as file:<br>    file.write(\"New File Created\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>READ &amp; WRITE MODES<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Read and Write (<code>\"r+\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens file for reading and writing<\/li>\n\n\n\n<li>File must exist<\/li>\n\n\n\n<li>Does NOT overwrite automatically<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"r+\") as file:<br>    file.write(\"Updated\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Write and Read (<code>\"w+\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens file for reading and writing<\/li>\n\n\n\n<li>Overwrites existing content<\/li>\n\n\n\n<li>Creates file if it does not exist<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"w+\") as file:<br>    file.write(\"Fresh Start\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Append and Read (<code>\"a+\"<\/code>)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens file for reading and appending<\/li>\n\n\n\n<li>Does NOT overwrite<\/li>\n\n\n\n<li>Creates file if it does not exist<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"a+\") as file:<br>    file.write(\"Extra Line\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BINARY MODES<\/strong><\/h2>\n\n\n\n<p>Add <code>\"b\"<\/code> to work with binary files (images, PDFs, etc.)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"rb\"<\/code> \u2192 Read binary<\/li>\n\n\n\n<li><code>\"wb\"<\/code> \u2192 Write binary<\/li>\n\n\n\n<li><code>\"ab\"<\/code> \u2192 Append binary<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"image.jpg\", \"rb\") as file:<br>    data = file.read()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TEXT MODE (Default)<\/strong><\/h2>\n\n\n\n<p>Add <code>\"t\"<\/code> for text mode (optional because it is default)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"rt\"<\/code> \u2192 Read text<\/li>\n\n\n\n<li><code>\"wt\"<\/code> \u2192 Write text<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SUMMARY TABLE<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Mode<\/th><th>Purpose<\/th><th>File Exists?<\/th><\/tr><\/thead><tbody><tr><td>r<\/td><td>Read<\/td><td>Must exist<\/td><\/tr><tr><td>w<\/td><td>Write<\/td><td>Overwrites<\/td><\/tr><tr><td>a<\/td><td>Append<\/td><td>Keeps content<\/td><\/tr><tr><td>x<\/td><td>Create<\/td><td>Error if exists<\/td><\/tr><tr><td>r+<\/td><td>Read &amp; Write<\/td><td>Must exist<\/td><\/tr><tr><td>w+<\/td><td>Write &amp; Read<\/td><td>Overwrites<\/td><\/tr><tr><td>a+<\/td><td>Append &amp; Read<\/td><td>Keeps content<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BEST PRACTICE<\/strong><\/h2>\n\n\n\n<p>Always use the <code>with<\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"r\") as file:<br>    data = file.read()<\/pre>\n\n\n\n<p>This ensures the file closes automatically.<\/p>\n\n\n\n<p>Understanding file modes is essential for handling data safely and effectively in Python applications.<\/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 > File Modes<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773456888098\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":34,"template":"","class_list":["post-83","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>File Modes - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.\" \/>\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\/file-modes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Modes - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/file-modes\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T02:56:28+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/file-modes\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/file-modes\\\/\",\"name\":\"File Modes - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:24:46+00:00\",\"dateModified\":\"2026-03-14T02:56:28+00:00\",\"description\":\"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/file-modes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/file-modes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/file-modes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > File Handling > File Modes\"}]},{\"@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":"File Modes - One Language. Endless Possibilities","description":"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.","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\/file-modes\/","og_locale":"en_US","og_type":"article","og_title":"File Modes - One Language. Endless Possibilities","og_description":"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T02:56:28+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/","url":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/","name":"File Modes - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:24:46+00:00","dateModified":"2026-03-14T02:56:28+00:00","description":"Learn Python file modes including read, write, append, and binary modes with examples to handle files safely and efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/file-modes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/file-modes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > File Handling > File Modes"}]},{"@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\/83","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=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}