In this tutorial, we are going to learn how to read a file in Python 3. After we have learned how to open a file in Python, we are going to learn how to write to the file and save it again.  In previous posts, we have learned how to open a range of different files using Python. For instance, we have learned how to open JSON, CSV, Excel, and HTML files using Pandas, and the json library. Here, however, we are going to open plain files (.txt) in Python.

Files in Windows Explorer
  • Save
Example files to read with Python open()

Prerequisites

For this how to read a file in Python tutorial, we need to have Python 3 installed. One of the easiest ways to install Python is to download a Python distribution, such as Anaconda, ActivePython, or Enthought Canopy, to name a few. Here’s a YouTube tutorial on how to install Anaconda.

Reading Files in Python

Now, when we are working with Python, there is no need to import a library in order to read and write files. Reading and writing files, in Python, is handled natively in the language.                  

In this post, we will, thus, focus on using Python to open a file and we will use the built-in open function to get a file object.

Python’s File Object

Now, when we are using Python’s open function, it will return something known as a file object. These objects will contain methods and attributes that we can use to gather information about the file we have opened (much like we’ve learned when using Pandas dataframe objects, for instance). One neat thing, of course, is that we can use these methods to change the file that we have opened using Python.

In this Python read from a file tutorial, we are going to work with the mode attribute. This attribute will tell us which mode the file we have opened, using Python, is in. Furthermore, the name attribute will give us the name of the file we have opened in Python (i.e., the name of the file object).

How do You Read a File in Python?

Opening, and reading, a file in Python is easy: we just type ourfile = open('PATH_TO_FILE') and then ourfile.read().

What Does read() Return in Python?

Now, if we open a file in Python the read() return a Python string object.

How to Read a File in Python using the open() function

In this section, we are going to learn how to load a file in Python using the open() function. In it’s simplest example, open() will open a file and create a file object.

As can be seen, in the image above, there are a couple of arguments that can be used, however, when opening a file using Pythons open() function. The most commonly used arguments, however, are the first two. Note, the first is mandatory and the rest of them are optional. If we don’t add a mode, the file will be opened in read-only mode in Python.

Using the mode argument when reading files in Python

In this section, of the post on how to read a file in Python, we will go through the different modes in which we can read a file. As previously mentioned, if we don’t pass anything with the mode argument the file will be written in read-only. In this reading files in Python tutorial, we are going to work with the following modes:

Python open() and the modes
  • Save
Some of the modes we can open files with

That is, we can open a file in read-only, write, append, and read and write mode. If we use append (‘a’) we will append information at the end of that file.

A Simple Read a File in Python Example

Now, we are going to read a file in Python using only the file name as an argument. As evident, in the code example below, open() takes a string with the file path as input. In this reading a file in Python example, it is assumed that the example file is in the same directory as the Python script. Or in this case, the Jupyter notebook.

exfile = open('example_file')
print(exfile)
Python open() return a file object
  • Save
File object in Python

In the image, above, it becomes evident that we have a file object that we have opened in read-only mode. To iterate what we did, we opened up the text file (example_file.txt) without any other arguments. This, in turn, leads to that the file was opened in the read mode. Thus, we cannot write to that file. If we want to print the name of the file we can just type print(exfile.name).

Creating a Text file and Writing to the File using Python

In the next how to read a file in Python example, we are going to create a file using Python open(). Now, we are going to use open with the write mode. That is, we are going to open a file object and then use the file objects write method.

 exfile = open('example_file2', 'w')
print(exfile)
Write mode - Python file object
  • Save
Write mode

Now, in the image above, we can see that we have a file object in write mode (‘w’). In the next code chunk, we are going to add one line of text to this file:

exfile.write('This is example file 2 \n')

If we want to, we can, of course, add more lines by using the write method again:

exfile.write('Line number 2, in example file 2')
exfile.close()

Note, how we closed the file using close() in the last line above. In the image, below, we can see the example file we have created with Python.

Content of file written in Python
  • Save
Content of the created file

How to Read a Text File in Python using open()

In the next Python read a file example, we will learn how to open a text (.txt) file in Python. This is, of course, simple and we have basically already the knowledge on how to do this with Python. That is if we just want to read the .txt file in Python we can use open and the read mode:

txtfile = open('example_file.txt')

Opening a File in Python: read() Example:

This was simple. Now, if we want to print the content of the text file we have three options. First, we can use the read() method. This Python method will read the entire file. That is, txtfile.read() will give us the following output:

Reading a file in Python using read()
  • Save
Output of the read() method

Reading a File to a Python list: readlines() Example:

If we, on the other hand, want to read the content from a file to a Python list, we can use the readlines() method. That is if we want to read the text file into a list we type:

txtfile = open('example_file.txt')  
print(txtfile.readlines())
How to read a file in Python to a list.
  • Save
List created with readlines()

Furthermore, we can also use the sizehint argument. This enables us to get certain lines. For example, the following code will read the two first lines, into two different string variables, and print it:

txtfile = open('example_file.txt')
line = txtfile.readlines(1)
print(line)

line2 = txtfile.readlines(2)
print(line2)
how to load a file in Python to a list
  • Save
First two lines read from a file

Finally, when we have opened a file in Python we can also loop through the content and print line by line:

txtfile = open('example_file.txt')
for line in txtfile:
    print(line)
how to read a file in Python - line by line
  • Save
Output from the loop (reading the file line by line)

Reading a File in Python and Appending Content to It

In the next example, we are going to read a file in Python and append information to the file. More specifically, we are going to load a .txt file using Python and append some data to it. Again, we start by opening the file in Python using open() but using the append mode:

open('example_file2.txt', 'a')
.

Next, we are going to add content to this using write(), again.

txtfile.write('\n More text here.')

When appending text, in Windows 10 at least, we have to add the \n before the line or else this line will be appended next to the last character (on the last line, of the file). If we are to add more lines, we have to remember to do this as well;

txtfile.write(‘\nLast line of text, I promise.)
txtfile.close()

Finally, we can open the text file using a text editor (e.g., Notepad, Gedit) and we’ll see the last two lines that we’ve added:

  • Save

How to Read a file in Python using the with Statement

In the next how to open a file in Python example, we are going to read a file using the with statement. This has the advantage that we don’t have to remember to close the file and the syntax for using the with statement is clean to read:

with open('example_file2.txt') as txtfile2:
    print(txtfile2.read())
How to read a file in Python using the with statement
  • Save
Output

Now, if we try to use the read() method Python will throw a ValueError:

txtfile2.read()
reading files in Python examples
  • Save
Error

Splitting Words in File and Counting Words

As final reading a file in Python example, we are going to use the string split() method to split the sentences in the text file into words. When we have read the file and split the words, we are going to use the Counter subclass from collections to use Python to count words in an opened file.

from collections import Counter

with open('example_file2.txt') as txtfile2:
    wordcount = Counter(txtfile2.read().split())
    
print(len(wordcount))
# Output: 43

Counting Words in Python

Now, the Counter subclass is giving us a dictionary that contains all words and occurrences of each word. Thus, we can print all words and their count like this:

for k in sorted(wordcount, key=wordcount.get, reverse=True):
    print(k, wordcount[k])

In the code example above, we are looping through the keys in the dictionary and sort them. This way, we get the most common words on top. Of course, reading a file with many words, using Python, and printing the result like this is not feasible.

If we have the need it is, of course, also possible to rename a file (or multiple files) in Python using the os.rename() method.

Reading other File Formats in Python

Now, we have learned how to load a file in Python, and we will briefly discuss other file formats that Python can handle. Python can, of course, open a wide range of different file formats. In previous posts, we have used the json library to parse JSON files in Python, for instance. Furthermore, we have also learned how to read csv files in Python using Pandas, open Excel files with Pandas, among other file formats that are common for storing data.

Conclusion: Reading & Writing Files in Python

In this tutorial, we have learned the basics of how to open files in Python. More specifically, we have learned how to read a file in different modes, create and write to a file, append data to a file, and how to read a file in Python using the with statement. In future posts, we will learn more about how to read other file formats using Python (e.g., CSV, Excel).

Drop a comment below if you need anything covered in this or a future blog, and stay tuned!

How to Read a File (.txt) in Python 3
  • Save
Share via
Copy link