{"id":695,"date":"2020-10-22T07:14:30","date_gmt":"2020-10-22T07:14:30","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=695"},"modified":"2025-03-30T11:46:27","modified_gmt":"2025-03-30T11:46:27","slug":"python-write-text-file","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-write-text-file\/","title":{"rendered":"Python Write Text File"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn various ways to write text files in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tldr'>TL;DR <a href=\"#tldr\" class=\"anchor\" id=\"tldr\" title=\"Anchor for TL;DR\">#<\/a><\/h2>\n\n\n\n<p>The following illustrates how to write a string to a text file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    f.write(<span class=\"hljs-string\">'readme'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='steps-for-writing-to-text-files'>Steps for writing to text files <a href=\"#steps-for-writing-to-text-files\" class=\"anchor\" id=\"steps-for-writing-to-text-files\" title=\"Anchor for Steps for writing to text files\">#<\/a><\/h2>\n\n\n\n<p>To write to a text file in Python, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, open the text file for writing (or append) using the <code>open()<\/code> function.<\/li>\n\n\n\n<li>Second, write to the text file using the <code>write()<\/code> or <code>writelines()<\/code> method.<\/li>\n\n\n\n<li>Third, close the file using the <code>close()<\/code> method.<\/li>\n<\/ul>\n\n\n\n<p>The following shows the basic syntax of the <code>open()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">f = open(file, mode)<\/code><\/span><\/pre>\n\n\n<p>The <code>open()<\/code> function accepts many parameters. But you&#8217;ll focus on the first two:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>file<\/code> parameter specifies the path to the text file that you want to open for writing.<\/li>\n\n\n\n<li>The <code>mode<\/code> parameter specifies the mode for which you want to open the text file.<\/li>\n<\/ul>\n\n\n\n<p>For writing to a text file, you use one of the following modes:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Mode<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>'w'<\/code><\/td><td>Open a text file for writing. If the file exists, the function will truncate all the contents as soon as you open it. If the file doesn&#8217;t exist, the function creates a new file.<\/td><\/tr><tr><td><code>'a'<\/code><\/td><td>Open a text file for appending text. If the file exists, the function append contents at the end of the file.<\/td><\/tr><tr><td>&#8216;+&#8217;<\/td><td>Open a text file for updating (both reading &amp; writing).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The <code>open()<\/code> function returns a file object that has two useful methods for writing text to the file: <code>write()<\/code> and <code>writelines()<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>write()<\/code> method writes a string to a text file. <\/li>\n\n\n\n<li>The <code>writelines()<\/code> method write a list of strings to a file at once.<\/li>\n<\/ul>\n\n\n\n<p>The <code>writelines()<\/code> method accepts an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterable<\/a> object, not just a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a>, so you can pass a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a> of strings, a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-set\/\">set<\/a> of strings, etc., to the <code>writelines()<\/code> method.<\/p>\n\n\n\n<p>To write a line to a text file, you need to manually add a new line character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">f.write(<span class=\"hljs-string\">'\\n'<\/span>)\nf.writelines(<span class=\"hljs-string\">'\\n'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='writing-text-file-examples'>Writing text file examples <a href=\"#writing-text-file-examples\" class=\"anchor\" id=\"writing-text-file-examples\" title=\"Anchor for Writing text file examples\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to use the <code>write()<\/code> function to write a list of texts to a text file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">lines = &#91;<span class=\"hljs-string\">'Readme'<\/span>, <span class=\"hljs-string\">'How to write text files in Python'<\/span>]\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> lines:\n        f.write(line)\n        f.write(<span class=\"hljs-string\">'\\n'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the readme.txt file doesn&#8217;t exist, the <code>open()<\/code> function will create a new file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"130\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method-1.png\" alt=\"\" class=\"wp-image-697\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method-1.png 504w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method-1-300x77.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<p>The following shows how to write a list of text strings to a text file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">lines = &#91;<span class=\"hljs-string\">'Readme'<\/span>, <span class=\"hljs-string\">'How to write text files in Python'<\/span>]\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    f.writelines(lines)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you treat each element of the list as a line, you need to concatenate it with the newline character like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">lines = &#91;<span class=\"hljs-string\">'Readme'<\/span>, <span class=\"hljs-string\">'How to write text files in Python'<\/span>]\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    f.write(<span class=\"hljs-string\">'\\n'<\/span>.join(lines))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"130\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method.png\" alt=\"\" class=\"wp-image-696\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method.png 504w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-write-method-300x77.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='appending-text-files'>Appending text files <a href=\"#appending-text-files\" class=\"anchor\" id=\"appending-text-files\" title=\"Anchor for Appending text files\">#<\/a><\/h2>\n\n\n\n<p>To append to a text file, you need to open the text file for appending mode. The following example appends new lines to the <code>readme.txt<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">more_lines = &#91;<span class=\"hljs-string\">''<\/span>, <span class=\"hljs-string\">'Append text files'<\/span>, <span class=\"hljs-string\">'The End'<\/span>]\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>, <span class=\"hljs-string\">'a'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    f.write(<span class=\"hljs-string\">'\\n'<\/span>.join(more_lines))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"463\" height=\"128\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-appending-mode.png\" alt=\"\" class=\"wp-image-698\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-appending-mode.png 463w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-write-text-file-appending-mode-300x83.png 300w\" sizes=\"auto, (max-width: 463px) 100vw, 463px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='writing-to-a-utf-8-text-file'>Writing to a UTF-8 text file <a href=\"#writing-to-a-utf-8-text-file\" class=\"anchor\" id=\"writing-to-a-utf-8-text-file\" title=\"Anchor for Writing to a UTF-8 text file\">#<\/a><\/h2>\n\n\n\n<p>If you write UTF-8 characters to a text file using the code from the previous examples, you&#8217;ll get an error like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-44: character maps to <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">undefined<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To open a file and write UTF-8 characters to a file, you need to pass the <code>encoding='utf-8'<\/code> parameter to the <code>open()<\/code> function.<\/p>\n\n\n\n<p>The following example shows how to write UTF-8 characters to a text file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">\nquote = <span class=\"hljs-string\">'\u6210\u529f\u3092\u53ce\u3081\u308b\u4eba\u3068\u306f\u4eba\u304c\u6295\u3052\u3066\u304d\u305f\u30ec\u30f3\u30ac\u3067\u3057\u3063\u304b\u308a\u3057\u305f\u57fa\u76e4\u3092\u7bc9\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u4eba\u306e\u3053\u3068\u3067\u3042\u308b\u3002'<\/span>\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'quotes.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>, encoding=<span class=\"hljs-string\">'utf-8'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    f.write(quote)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>open()<\/code> function with the <code>w<\/code> or <code>a<\/code> mode to open a text file for appending.<\/li>\n\n\n\n<li>Always close the file after completing writing using the <code>close()<\/code> method or use the <code>with<\/code> statement when opening the file.<\/li>\n\n\n\n<li>Use <code>write()<\/code> and <code>writelines()<\/code> methods to write to a text file.<\/li>\n\n\n\n<li>Pass the <code>encoding='utf-8'<\/code> to the <code>open()<\/code> function to write UTF-8 characters into a file.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=write-text-file\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"695\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-write-text-file\/\"\n\t\t\t\tdata-post-title=\"Python Write Text File\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"695\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-write-text-file\/\"\n\t\t\t\tdata-post-title=\"Python Write Text File\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn various ways to write text files in Python. TL;DR # The following illustrates how to write a string to a text file: Steps for writing to text files # To write to a text file in Python, you follow these steps: The following shows the basic syntax of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":66,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-695","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/695\/revisions"}],"predecessor-version":[{"id":7226,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/695\/revisions\/7226"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}