{"id":691,"date":"2020-10-22T03:52:54","date_gmt":"2020-10-22T03:52:54","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=691"},"modified":"2025-03-30T10:33:10","modified_gmt":"2025-03-30T10:33:10","slug":"python-read-text-file","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-read-text-file\/","title":{"rendered":"Python Read Text File"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you learn various ways to read 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 shows how to read all texts from the <code>readme.txt<\/code> file into a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'readme.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    lines = f.readlines()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='steps-for-reading-a-text-file-in-python'>Steps for reading a text file in Python <a href=\"#steps-for-reading-a-text-file-in-python\" class=\"anchor\" id=\"steps-for-reading-a-text-file-in-python\" title=\"Anchor for Steps for reading a text file in Python\">#<\/a><\/h2>\n\n\n\n<p>To read a text file in Python, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, open a text file for reading by using the <code>open()<\/code> function.<\/li>\n\n\n\n<li>Second, read text from the text file using the file <code>read()<\/code>, <code>readline()<\/code>, or <code>readlines()<\/code> method of the file object.<\/li>\n\n\n\n<li>Third, close the file using the file <code>close()<\/code> method.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-open-function'>1) open() function <a href=\"#1-open-function\" class=\"anchor\" id=\"1-open-function\" title=\"Anchor for 1) open() function\">#<\/a><\/h3>\n\n\n\n<p>The <code>open()<\/code> function has many parameters but you&#8217;ll be focusing on the first two:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">open(path_to_file, mode)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>path_to_file<\/code> parameter specifies the path to the text file. <\/p>\n\n\n\n<p>If the program and file are in the same folder, you need to specify only the filename of the file. Otherwise, you need to include the path to the file as well as the filename. <\/p>\n\n\n\n<p>To specify the path to the file, you use the forward-slash (<code>'\/'<\/code>) even if you&#8217;re working on Windows. <\/p>\n\n\n\n<p>For example, if the file <code>readme.txt<\/code> is stored in the <code>sample<\/code> folder as the program, you need to specify the path to the file as <code>c:\/sample\/readme.txt<\/code><\/p>\n\n\n\n<p>The <code>mode<\/code> is an optional parameter. It&#8217;s a string that specifies the mode in which you want to open the file. The following table shows available modes for opening a text file:<\/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>'r'<\/code><\/td><td>Open for text file for reading text<\/td><\/tr><tr><td><code>'w'<\/code><\/td><td>Open a text file for writing text<\/td><\/tr><tr><td><code>'a'<\/code><\/td><td>Open a text file for appending text<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For example, to open a file whose name is <code>the-zen-of-python.txt<\/code> stored in the same folder as the program, you use the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"> f = open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>,<span class=\"hljs-string\">'r'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>open()<\/code> function returns a file object which you will use to read text from a text file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-reading-text-methods'>2) Reading text methods <a href=\"#2-reading-text-methods\" class=\"anchor\" id=\"2-reading-text-methods\" title=\"Anchor for 2) Reading text methods\">#<\/a><\/h3>\n\n\n\n<p>The file object provides you with three methods for reading text from a text file:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>read(size)<\/code> &#8211; read some contents of a file based on the optional size and return the contents as a string. If you omit the size, the <code>read()<\/code> method reads from where it left off till the end of the file. If the end of a file has been reached, the <code>read()<\/code> method\u00a0returns an empty string.<\/li>\n\n\n\n<li><code>readline()<\/code> &#8211; read a single line from a text file and return the line as a string. If the end of a file has been reached, the <code>readline()<\/code>\u00a0returns an empty string.<\/li>\n\n\n\n<li><code>readlines()<\/code> &#8211; read all the lines of the text file into a list of strings. This method is useful if you have a small file and you want to manipulate the whole text of that file.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-close-method'>3) close() method <a href=\"#3-close-method\" class=\"anchor\" id=\"3-close-method\" title=\"Anchor for 3) close() method\">#<\/a><\/h3>\n\n\n\n<p>The file that you open will remain open until you close it using the <code>close()<\/code> method.<\/p>\n\n\n\n<p>It&#8217;s important to close the file that is no longer in use for the following reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, when you open a file in your script, the file system usually locks it down so no other programs or scripts can use it until you close it.<\/li>\n\n\n\n<li>Second, your file system has a limited number of file descriptors that you can create before it runs out of them. Although this number might be high, it&#8217;s possible to open a lot of files and deplete your file system resources.<\/li>\n\n\n\n<li>Third, leaving many files open may lead to race conditions which occur when multiple <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/differences-between-processes-and-threads\/\">processes<\/a> attempt to modify one file at the same time and can cause all kinds of unexpected behaviors.<\/li>\n<\/ul>\n\n\n\n<p>The following shows how to call the <code>close()<\/code> method to close the file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f.close()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To close the file automatically without calling the <code>close()<\/code> method, you use the <code>with<\/code> statement like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(path_to_file) <span class=\"hljs-keyword\">as<\/span> f:\n    contents = f.readlines()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In practice, you&#8217;ll use the <code>with<\/code> statement to close the file automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='reading-a-text-file-examples'>Reading a text file examples <a href=\"#reading-a-text-file-examples\" class=\"anchor\" id=\"reading-a-text-file-examples\" title=\"Anchor for Reading a text file examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use <a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/the-zen-of-python.txt\" target=\"_blank\" rel=\"noreferrer noopener\">the-zen-of-python.txt<\/a> file for the demonstration.<\/p>\n\n\n\n<p>The following example illustrates how to use the <code>read()<\/code> method to read all the contents of the <code>the-zen-of-python.txt<\/code> file into a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    contents = f.read()\n    print(contents)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Beautiful <span class=\"hljs-keyword\">is<\/span> better than ugly.\nExplicit <span class=\"hljs-keyword\">is<\/span> better than implicit.\nSimple <span class=\"hljs-keyword\">is<\/span> better than complex.\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>readlines()<\/code> method to read the text file and returns the file contents as a list of strings:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    &#91;print(line) <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> f.readlines()]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Beautiful <span class=\"hljs-keyword\">is<\/span> better than ugly.\n\nExplicit <span class=\"hljs-keyword\">is<\/span> better than implicit.\n\nSimple <span class=\"hljs-keyword\">is<\/span> better than complex.\n\nComplex <span class=\"hljs-keyword\">is<\/span> better than complicated.\n\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The reason you see a blank line after each line from a file is that each line in the text file has a newline character (\\n). To remove the blank line, you can use the <code>strip()<\/code> method. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    &#91;print(line.strip()) <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> f.readlines()]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to use the <code>readline()<\/code> to read the text file line by line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n        line = f.readline()\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> line:\n            <span class=\"hljs-keyword\">break<\/span>\n        print(line.strip())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Explicit <span class=\"hljs-keyword\">is<\/span> better than implicit.\nComplex <span class=\"hljs-keyword\">is<\/span> better than complicated.\nFlat <span class=\"hljs-keyword\">is<\/span> better than nested.\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='a-more-concise-way-to-read-a-text-file-line-by-line'>A more concise way to read a text file line by line <a href=\"#a-more-concise-way-to-read-a-text-file-line-by-line\" class=\"anchor\" id=\"a-more-concise-way-to-read-a-text-file-line-by-line\" title=\"Anchor for A more concise way to read a text file line by line\">#<\/a><\/h2>\n\n\n\n<p>The <code>open()<\/code> function returns a file object which is an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterable<\/a> object. Therefore, you can use a <code>for<\/code> loop to iterate over the lines of a text file as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'the-zen-of-python.txt'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> f:\n        print(line.strip())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is a more concise way to read a text file line by line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='read-utf-8-text-files'>Read UTF-8 text files <a href=\"#read-utf-8-text-files\" class=\"anchor\" id=\"read-utf-8-text-files\" title=\"Anchor for Read UTF-8 text files\">#<\/a><\/h2>\n\n\n\n<p>The code in the previous examples works fine with ASCII text files. However, if you&#8217;re dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it&#8217;s likely a UTF-8 file that uses more than just the standard ASCII text characters.<\/p>\n\n\n\n<p>To open a UTF-8 text file, you need to pass the <code>encoding='utf-8'<\/code> to the <code>open()<\/code> function to instruct it to expect UTF-8 characters from the file.<\/p>\n\n\n\n<p>For the demonstration, you&#8217;ll use the following <code><a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/quotes.txt\" target=\"_blank\" rel=\"noreferrer noopener\">quotes.txt<\/a><\/code> file that contains some quotes in Japanese.<\/p>\n\n\n\n<p>The following shows how to loop through the <code>quotes.txt<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'quotes.txt'<\/span>, encoding=<span class=\"hljs-string\">'utf8'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> f:\n        print(line.strip())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/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=\"670\" height=\"56\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-read-utf-8-text-file.png\" alt=\"Python read utf-8 text file\" class=\"wp-image-713\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-read-utf-8-text-file.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-read-utf-8-text-file-300x25.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\n\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>'r'<\/code> mode to open a text file for reading.<\/li>\n\n\n\n<li>Use the <code>read()<\/code>, <code>readline()<\/code>, or <code>readlines()<\/code> method to read a text file.<\/li>\n\n\n\n<li>Always close a file after completing reading it using the <code>close()<\/code> method or the <code>with<\/code> statement.<\/li>\n\n\n\n<li>Use the <code>encoding='utf-8'<\/code> to read the UTF-8 text 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=read-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=\"691\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-read-text-file\/\"\n\t\t\t\tdata-post-title=\"Python Read 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=\"691\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-read-text-file\/\"\n\t\t\t\tdata-post-title=\"Python Read 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 learn various ways to read text files in Python. TL;DR # The following shows how to read all texts from the readme.txt file into a string: Steps for reading a text file in Python # To read a text file in Python, you follow these steps: 1) open() function # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":65,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-691","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/691","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=691"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/691\/revisions"}],"predecessor-version":[{"id":7225,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/691\/revisions\/7225"}],"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=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}