Skip to content

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

How to Read a Text File in Python (Python open) Cover Image

In this tutorial, you’ll learn how to read a text file in Python with the open function. Learning how to safely open, read, and close text files is an important skill to learn as you begin working with different types of files. In this tutorial, you’ll learn how to use context managers to safely and efficiently handle opening files.

By the end of this tutorial, you’ll have learned:

  • How to open and read a text file using Python
  • How to read files in different ways and in different formats
  • How to read a file all at once or line-by-line
  • How to read a file to a list or to a dictionary

How To Open a Text File in Python

Python provides a number of easy ways to create, read, and write files. Since we’re focusing on how to read a text file, let’s take a look at the Python open() function. This function, well, facilitates opening a file.

Let’s take a look at this Python open function:

open(
    file,           # The pathname
    mode=’r’,       # The mode to open the file in
    buffering=-1,   # The buffering policy
    encoding=None,  # The encoding used for the file
    errors=None,    # How encoding/decoding errors are handled
    newline=None,   # How to identify new lines
    closefd=True,   # Whether to keep file descriptor open
    opener=None     # Using a custom opener
    )

In this tutorial, we’ll focus on just three of the most important parameters: file=, mode=, and encoding=.

When opening a file, we have a number of different options in terms of how to open the file. This is controlled by the mode parameter. Let’s take a look at the various arguments this parameter takes:

CharacterMeaning
'r'Open for reading (default)
'w'Open for writing (first truncates the file)
'x'Open for exclusive creation (fails if file already exists)
'a'Open for writing (appends to file if it exists)
'b'Open in binary mode
't'Text mode
'+'Open for updating (reading and writing)
The different arguments for the mode= parameter in the Python open function

Ok, let’s see how we can open a file in Python. Feel free to download this text file, if you want to follow along line by line. Save the file and note its path into the file_path variable below:

# Opening a text file in Python
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

file = open(file_path)
print(file)

# Returns: <_io.TextIOWrapper name='https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt' mode='r' encoding='UTF-8'>

When we run this, we’re opening the text file. At this, I’ll take a quick detour and discuss the importance of closing the file as well. Python must be explicitly told to manage the external resources we pass in. By default, Python will try and retain the resource for as long as possible, even when we’re done using it.

Because of this, we can close the file by using the .close() method:

# Closing a file with .close()
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

file = open(file_path)
file.close()

Handling Files with Context Managers in Python

A better alternative to this approach is to use a context manager. The context manager handles opening the file, performing actions on it, and then safely closing the file! This means that your resources can be safer and your code can be cleaner.

Let’s take a look at how we can use a context manager to open a text file in Python:

# Using a context manager to open a file
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/nikpi/Desktop/sample_text.txt'

with open(file_path) as file:
    ...

We can see here that by using the with keyword, we were able to open the file. The context manager then implicitly handles closing the file once all the nested actions are complete!

Ok, now that you have an understanding of how to open a file in Python, let’s see how we can actually read the file!

How To Read a Text File in Python

Let’s start by reading the entire text file. This can be helpful when you don’t have a lot of content in your file and want to see the entirety of the file’s content. To do this, we use the aptly-named .read() method.

Let’s see how we can use a context manager and the .read() method to read an entire text file in Python:

# Reading an entire text file in Python
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    print(file.read())

# Returns:
# Hi there!
# Welcome to datagy!
# Today we’re learning how to read text files.
# See you later!

We can see how easy that was! The .read() method returns a string, meaning that we could assign it to a variable as well.

The .read() method also takes an optional parameter to limit how many characters to read. Let’s see how we can modify this a bit:

# Reading only some characters
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    print(file.read(28))

# Returns:
# Hi there!
# Welcome to datagy!

If the argument provided is negative or blank, then the entire file will be read.

How to Read a Text File in Python Line by Line

In some cases, your files will be too large to conveniently read all at once. This is where being able to read your file line by line becomes important.

The Python .readline() method returns only a single line at a time. This can be very helpful when you’re parsing the file for a specific line, or simply want to print the lines slightly modified.

Let’s see how we can use this method to print out the file line by line:

# Reading a single line in Python
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    print(file.readline())

# Returns:
# Hi there!

In the example above, only the first line was returned. We can call the method multiple times in order to print more than one line:

# Reading multiple lines in Python
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    print(file.readline())
    print(file.readline())

# Returns:
# Hi there!

# Welcome to datagy!

This process can feel a bit redundant, especially because it requires you to know how many lines there are. Thankfully, the file object we created is an iterable, and we can simply iterate over these items:

# Printing all lines, line by line
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    for line in file:
        print(line)

# Returns:
# Hi there!

# Welcome to datagy!

# Today we’re learning how to read text files.

# See you later!

How to Read a Text File in Python to a List

Sometimes you’ll want to store the data that you read in a collection object, such as a Python list. We can accomplish this using the .readlines() method, which reads all lines at once into a list.

Let’s see how we can use this method:

# Reading a text file to a list
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    line_list = file.readlines()

print(line_list)

# Returns:
# ['Hi there!\n', 'Welcome to datagy!\n', 'Today we’re learning how to read text files.\n', 'See you later!']

We can remove the new line characters by using the .rstrip() method, which removes trailing whitespace:

# Removing trailing new lines from our list
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path) as file:
    line_list = file.readlines()
    line_list = [item.rstrip() for item in line_list]

print(line_list)

# Returns:
# ['Hi there!', 'Welcome to datagy!', 'Today we’re learning how to read text files.', 'See you later!']

How to Read a Text File in Python to a Dictionary

Now, let’s see how we can read a file to a dictionary in Python. For this section, download the file linked here. The file contains a line by line shopping list of supplies that we need for a project:

Lumber, 4
Screws, 16
Nails, 12
Paint, 1
Hammer, 1

We want to be able to read this text file into a dictionary, so that we can easily reference the number of supplies we need per item.

Again, we can use the .readlines() method. We can then parse each item in the list and, using a dictionary comprehension, split the values to create a dictionary:

# Reading a file into a dictionary
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/text_resources.txt'

with open(file_path) as file:
    resources = {}
    for line in file:
        key, value = line.rstrip().split(',', 1)
        resources[key] = value

    # Or as a dictionary comprehension:
    resources2 = {key:value for line in file for key, value in [line.rstrip().split(',', 1)]}

print(resources)

# Returns: {'Lumber': ' 4', 'Screws': ' 16', 'Nails': ' 12', 'Paint': ' 1', 'Hammer': ' 1'}

In this case, the for loop is significantly easier to read. Let’s see what we’re doing here:

  1. We instantiate an empty dictionary
  2. We loop over each line in the file
  3. For each line, we remove the trailing white-space and split the line by the first comma
  4. We then assign the first value to be the key of the dictionary and then assign the value to be the remaining items

How to Read a Text File in Python with Specific Encoding

In some cases, you’ll be working with files that aren’t encoded in a way that Python can immediately handle. When this happens, you can specify the type of encoding to use. For example, we can read the file using the 'utf-8' encoding by writing the code below:

# Specifying the encoding
file_path = 'https://e6v4p8w2.rocketcdn.me/Users/datagy/Desktop/sample_text.txt'

with open(file_path, encoding='utf-8') as file:
    line_list = file.readlines()

Conclusion

In this post, you learned how to use Python to read a text file. You learned how to safely handle opening and closing the file using the with context manager. You then learned how to read a file, first all at once, then line by line. You also learned how to convert a text file into a Python list and how to parse a text file into a dictionary using Python.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.