{"id":2923,"date":"2020-02-01T06:55:57","date_gmt":"2020-02-01T06:55:57","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2923"},"modified":"2023-02-16T19:57:17","modified_gmt":"2023-02-16T19:57:17","slug":"python-read-file","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-read-file","title":{"rendered":"Python Read File &#8211; 3 Ways You Must Know"},"content":{"rendered":"\n<p>We have already seen in our <a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-file-handling\">Python- File Handling Tutorial<\/a> that how we can perform different operations in and on a <strong>file<\/strong> using <strong>Python<\/strong> programming. <\/p>\n\n\n\n<p>One of the operations was the reading from the file, which was already created. Here we are going to elaborate on the process and look at the different methods by which we can read a file directly in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Different Methods to Read from a File in Python<\/h2>\n\n\n\n<p>Before we jump right into the methods of reading a file, we must take care of a few things. First of all, for reading, the existence of a file is very important. Secondly, the <strong>mode<\/strong> in which the file has been opened also matters. There are various modes in which a file can be opened in Python programming, namely,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>r<\/code> &#8211; read-only<\/li><li><code>w<\/code> &#8211; only write<\/li><li><code>a <\/code>&#8211; append-only<\/li><li><code>r+<\/code> &#8211; read as well as write<\/li><li><code>w+<\/code> &#8211; write as well as read<\/li><li><code>a+<\/code> &#8211; append as well as read<\/li><\/ul>\n\n\n\n<p>Out of all the different modes available for opening a file, the file contents could be read-only in <code>r<\/code>, <code>r+<\/code>, <code>w+<\/code>, and <code>a+<\/code> modes. After we make sure that a file exists and open it in a proper readable mode, we can go further to the different functions or methods used to read file content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. read() in Python<\/h3>\n\n\n\n<p>The <code>read()<\/code> method in Python is a <strong>pre-defined<\/strong> function which returns the read data in the form of a <strong>string<\/strong>. The syntax for the <code>read()<\/code> method is,<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>file_open_object.read( n )<\/strong><\/p>\n\n\n\n<p>Where <strong>file_open_object<\/strong> is the object created while opening a specific file,<\/p>\n\n\n\n<p>and <strong>&#8216;n&#8217;<\/strong> is the number of bytes to be read from the file. In the case where <strong>n<\/strong> is not specified, the <code>read()<\/code> function reads the whole file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"349\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/new_file.png\" alt=\"New File\" class=\"wp-image-2929\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/new_file.png 518w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/new_file-300x202.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><figcaption>new_file content<\/figcaption><\/figure>\n\n\n\n<p>Consider the contents to be read belong to the above-shown file, named <strong>new_file.txt<\/strong>. Hence using <code>read()<\/code> we can read the information present inside <strong>new_file<\/strong>. Let us see how we can do that,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfile = open(&quot;new_file.txt&quot;, &quot;r&quot;)\nprint(file.read())\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPython\nC\nC++\nJava\nKotlin\n<\/pre><\/div>\n\n\n<p>Again for reading a specific number of bytes, we can use <code>read()<\/code> in the following way,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfile = open(&quot;new_file.txt&quot;, &quot;r&quot;)\nprint(file.read(6))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPython\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. readline() in Python <\/h3>\n\n\n\n<p><code>readline() <\/code>is yet another pre-defined method in Python, which returns a read line in the form of a <strong>string<\/strong>. Below is the syntax for <code>readline() <\/code>function,<\/p>\n\n\n\n<p class=\"has-text-align-center\">  <strong>file_open_object.readline( n ) <\/strong><\/p>\n\n\n\n<p>Similarly, here <strong>file_open_object<\/strong> is the object created while opening the file and <strong>&#8216;n&#8217;<\/strong> is the number of bytes which the function would read almost. Noteworthy, if <strong>n<\/strong> exceeds the length of a line, the function doesn&#8217;t consider the next line. Take a closer look at the function use,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfile = open(&quot;new_file.txt&quot;, &quot;r&quot;)\nprint(demo_file.readline())\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPython\\n\n<\/pre><\/div>\n\n\n<p><strong>Point to be noted:<\/strong> Here newline( <code>\\n <\/code>) is also considered as a <strong>character<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. readlines()  In Python <\/h3>\n\n\n\n<p><code>readlines() <\/code> reads all the <strong>lines<\/strong> present inside a specified file and returns a list containing the string forms of the read lines. Given below is the syntax,<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>file_open_object.readlines()<\/strong><\/p>\n\n\n\n<p>Using the <code>readlines()<\/code> method,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfile = open(&quot;new_file.txt&quot;, &quot;r&quot;)\nprint(demo_file.readlines())\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;Python\\n&#039;, &#039;C\\n&#039;, &#039;C++\\n&#039;, &#039;Java\\n&#039;, &#039;Kotlin&#039;]\n<\/pre><\/div>\n\n\n<p><strong>References:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/tutorial\/inputoutput.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/tutorial\/inputoutput.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/7485458\/python-reading-text-file\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/7485458\/python-reading-text-file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already seen in our Python- File Handling Tutorial that how we can perform different operations in and on a file using Python programming. One of the operations was the reading from the file, which was already created. Here we are going to elaborate on the process and look at the different methods by [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":2933,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2923"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2923\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/2933"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}