{"id":80,"date":"2026-03-01T17:10:56","date_gmt":"2026-03-01T12:10:56","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=80"},"modified":"2026-03-13T15:20:41","modified_gmt":"2026-03-13T10:20:41","slug":"writing-text-files","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/","title":{"rendered":"\u00a0Writing Text Files"},"content":{"rendered":"\n<p>Writing text files allows you to store data permanently in a file.<br>It is useful for saving reports, logs, user data, and program results.<\/p>\n\n\n\n<p>Python provides simple methods to create and write data into text files.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>OPENING A FILE FOR WRITING<\/strong><\/h1>\n\n\n\n<p>To write into a file, use the <code>open()<\/code> function with write mode.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">file = open(\"example.txt\", \"w\")<\/pre>\n\n\n\n<p>\u2022 <code>\"w\"<\/code> \u2192 Write mode (creates a new file or overwrites existing file)<\/p>\n\n\n\n<p>After writing, always close the file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">file.write(\"Hello Python\")<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>The <code>with<\/code> statement automatically closes the file after writing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"w\") as file:<br>    file.write(\"Welcome to Python programming.\")<\/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>WRITING MULTIPLE LINES<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"w\") as file:<br>    file.write(\"Line 1\\n\")<br>    file.write(\"Line 2\\n\")<br>    file.write(\"Line 3\\n\")<\/pre>\n\n\n\n<p><code>\\n<\/code> adds a new line.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>APPEND MODE<\/strong><\/h1>\n\n\n\n<p>If you don\u2019t want to overwrite the file, use append mode <code>\"a\"<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"example.txt\", \"a\") as file:<br>    file.write(\"This line is added later.\\n\")<\/pre>\n\n\n\n<p>Append mode adds content at the end of the file.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WRITING A LIST TO A FILE<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">lines = [\"Apple\\n\", \"Banana\\n\", \"Mango\\n\"]with open(\"fruits.txt\", \"w\") as file:<br>    file.writelines(lines)<\/pre>\n\n\n\n<p><code>writelines()<\/code> writes multiple lines at once.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>FILE MODES SUMMARY<\/strong><\/h1>\n\n\n\n<p>\u2022 <code>\"w\"<\/code> \u2192 Write (overwrite file)<br>\u2022 <code>\"a\"<\/code> \u2192 Append (add to file)<br>\u2022 <code>\"x\"<\/code> \u2192 Create new file (error if exists)<br>\u2022 <code>\"r+\"<\/code> \u2192 Read and write<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>HANDLING ERRORS<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    with open(\"example.txt\", \"w\") as file:<br>        file.write(\"Data saved successfully.\")<br>except Exception as e:<br>    print(\"An error occurred:\", e)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Write mode overwrites existing content<br>\u2022 Append mode keeps old content<br>\u2022 Always use <code>with<\/code> for safe file handling<br>\u2022 Use <code>\\n<\/code> for new lines<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY WRITING FILES IS IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Save program output<br>\u2022 Store user data<br>\u2022 Create reports and logs<br>\u2022 Essential for automation and data processing<\/p>\n\n\n\n<p>Understanding how to write text files is a key skill for building practical 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 > Writing 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-1773391598043\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":32,"template":"","class_list":["post-80","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>\u00a0Writing Text Files - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python file writing: create, write, and append text files safely using write(), writelines(), 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\/writing-text-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Writing Text Files - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python file writing: create, write, and append text files safely using write(), writelines(), and with statement.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T10:20:41+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\\\/writing-text-files\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/writing-text-files\\\/\",\"name\":\"\u00a0Writing Text Files - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:10:56+00:00\",\"dateModified\":\"2026-03-13T10:20:41+00:00\",\"description\":\"Learn Python file writing: create, write, and append text files safely using write(), writelines(), and with statement.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/writing-text-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/writing-text-files\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/writing-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 > Writing 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":"\u00a0Writing Text Files - One Language. Endless Possibilities","description":"Learn Python file writing: create, write, and append text files safely using write(), writelines(), 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\/writing-text-files\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Writing Text Files - One Language. Endless Possibilities","og_description":"Learn Python file writing: create, write, and append text files safely using write(), writelines(), and with statement.","og_url":"https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T10:20:41+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\/writing-text-files\/","url":"https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/","name":"\u00a0Writing Text Files - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:10:56+00:00","dateModified":"2026-03-13T10:20:41+00:00","description":"Learn Python file writing: create, write, and append text files safely using write(), writelines(), and with statement.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/writing-text-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/writing-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 > Writing 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\/80","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=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}