How to Read a Text file In Python Effectively

Summary: In this tutorial, you will learn several ways to read text files in Python.

TL;DR

Here’s how to read all texts from

the readme file.txt in a string:with open(‘readme.txt’) as f:lines = f.readlines()Code language: Python (python)Steps to read a text file in Python To read a text file

in Python, follow these steps:

First

  • , open a text file to read it using the open() function.
  • Second, read the text from the text file by using the file read(), readline(), or readlines() method of the file object.
  • Third, close the file

  • using the file close() method.

1

) open() function The open() function

has many parameters, but you’ll focus on the first two:open

(path_to_file, mode)Code language: Python (python)

The path_to_file parameter specifies the path to the text

file.

If the program and the file are in the same folder, you must specify only the file name of the file. Otherwise, you must include the path to the file as well as the name of the file.

To specify the path to the file, use the forward slash (‘/’) even if you are working in Windows.

For example, if the readme.txt file is

stored in the sample folder as a program, you must specify the path to the file as c:/sample/readme.txt

Mode is an optional parameter. A string that specifies how you want to open the file. The following table shows the available ways

to open a text file:For example, to open a file named the-zen-of-python.txt

stored in the same folder as the program, use the following code: f = open(‘the-zen-of-python.txt

‘,’r’)Code language:

Python (python)

The open() function returns a file object that you will use to read text from a text file.

2) Reading

methods

The file object provides you with three methods for

reading text from a text file:

  • read(size) – reading some contents of a file based on the optional size and returning the contents as a string. If you omit the size, the read() method reads from where you left off to the end of the file. If the end of a file has been reached, the read() method returns an empty string.
  • readline() – reads a single line from a text file and returns the line as a string. If the end of a file has been reached, readline() returns an empty string.
  • readlines() – reads all lines of the text file in a list of strings. This method is useful if you have a small file and want to manipulate all the text in that file.

3) close() method The file you open will remain open until you close it using the close() method. It is important to close the file that is no longer in use

for the following reasons:

First of all, when you open a file in your script, the file system usually locks it so that no other program or script can use it

until you close it.

  • Second, your file system has a limited number of file descriptors that you can create before you run out of them. Although this number can be high, it is possible to open many files and exhaust file system resources.
  • Third, leaving a lot of files open can lead to race conditions that occur when multiple processes try to modify a file at the same time and can cause all sorts of unexpected behaviors.

The following is how

to call the close() method to close the file:f.close()Code language: Python (python)To close the file automatically without calling the close()

method,

use the with statement as this:with open(path_to_file) as f: contents = f.readlines()Code language: Python (python)In

practice, you will use the with statement to close the file automatically.

Examples of reading a

file

We will use the python.txt zen file for the demonstration.

The following example illustrates how to use

the read() method to read the entire contents of the the-zen-of-python.txt file into a string:with open(‘the-zen-of-python.txt’) as f: contents = f.read() print(contents)

Output:

Beautiful is better than fely. Explicit is better than implicit. Simple is better than complex. … Code language: Python (python)The following example uses the readlines() method to read the

text file and returns the contents of the file as a list of strings

:with open(‘the-zen-of-python.txt’) as f: [print(line) for line in f.readlines()]Code language: Python (python)

Output:Beautiful

is better than fely. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. … Code language: Python (python)

The reason you see a blank line after each line in a file is that each line in the text file has a newline character (\n). To delete the blank line, you can use the strip() method. For

example:with open(‘the-zen-of-python.txt’) as f: [print(line.strip()) for line in f.readlines()]Code language: Python (python)

The following example shows how to use

readline() to read the text file line by line:with open(‘the-zen-of-python.txt’) as f: while True: line = f.readline() if not line: break print(line.strip())Code language: Python (python)

Output:

Explicit is better than implicit. Complex is better than complicated. Floor is better than nested. … Code language: Python (python)

A more concise way to read

a text file line by line

The open() function returns a file object that is an iterable object. Therefore, you can use a for loop to iterate over the lines

of a text file as follows:with open(‘the-zen-of-python.txt’) as f: for line in f:print(line.strip())Code language: Python (python)

This is a more concise way to read a text file line by line.

Read

UTF-8

text files

The code in the previous examples works well with ASCII text files. However, if you are dealing with other languages like Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it’s likely a UTF-8 file that uses more than just standard ASCII text characters.

To open a UTF-8

text file, you must pass the =’utf-8′ encoding to the open() function to instruct it to expect UTF-8 characters from the file.

For the demonstration, you will use the following quotation mark file.txt which contains some quotes in Japanese.

Here’s how to loop through the quotation marks

.txt file:with open(‘quotes.txt’, encoding=’utf8′) as f: for line in f: print(line.strip())Code language: Python (python)

Output:

Summary

Use the open() function with the ‘r’ mode to open a text file to read. Use the read(),

  • readline(), or readlines() method to read a text file.
  • Always close a file after completing its reading using the close() method or the with statement.
  • Use encoding=’utf-8′ to read the UTF-8 text file.