[Python tutorial] Chapter 68 reading text files

Posted by netman182 on Wed, 02 Mar 2022 11:12:46 +0100

At the beginning of this article, we will learn how Python operates files. The first is how to read text files.

quick reference

The following code demonstrates how to read from readme Txt file:

with open('readme.txt') as f:
    lines = f.readlines()

To read a text file

The steps to read a text file in Python are as follows:

  • First, use the open() function to open a text file in read mode.
  • Secondly, use the read(), readline() or readlines() methods of the file object to read the text in the file.
  • Finally, close the file using the close() method of the file object.

open() function

The open() function supports multiple parameters. The main parameters include two:

open(path_to_file, mode)

path_ to_ The file parameter specifies the path to the text file. If the file and program are in the same directory, you only need to specify the file name. Otherwise, we need to specify the full path and file name. For Windows file paths, we can use slashes (/) instead of double backslashes (\ \). For example, "c:/sample/readme.txt".

Mode is an optional parameter that specifies the mode in which the file is opened. The following table lists the available modes:

patterndescribe
'r'Open text file in read mode
'w'Open text file in write mode
'a'Open text file in append mode

For example, the following code means to open the Zen of Python in the program directory in read mode Txt file:

 f = open('the-zen-of-python.txt','r')

The open() function returns a file object for reading the contents of the file.

Read file contents

File objects provide three ways to read the contents of text files:

  • read() – reads all text and returns a string. This method is suitable for processing small files at one time.
  • readline() – reads the file by line, returning one string at a time.
  • readlines() – reads all text and returns a list of strings.

close() method

It will remain open until we close the file using the close() method. It is very important to close files that are no longer in use in time. If you do not close the files, the program may crash or the files may be damaged.

The following code calls the close() method to close the open file:

f.close()

To automatically close a file, use the with statement:

with open(path_to_file) as f:
    contents = f.readlines()

In practice, we can use the with statement to automatically close the file without manually executing the close() method.

Examples

We use the Zen of Python Txt file, the contents of which are as follows:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

The following example uses the read() method to read the contents of the file and store it in a string object:

with open('the-zen-of-python.txt') as f:
    contents = f.read()
    print(contents)

The output results are as follows:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
...

The following example uses the readlines() method to read the file and returns a list of strings:

lines = []
with open('the-zen-of-python.txt') as f:
    lines = f.readlines()

count = 0
for line in lines:
    count += 1
    print(f'line {count}: {line}')    

The output results are as follows:

line 1: Beautiful is better than ugly.

line 2: Explicit is better than implicit.

line 3: Simple is better than complex.
...

The following example uses the readline() method to read the file contents line by line:

with open('the-zen-of-python.txt') as f:
    line = f.readline()
    while line:
        line = f.readline()
        print(line)

The output results are as follows:

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.
...

A concise method of reading files line by line

The open() function returns a traversable file object. Therefore, we can use the for loop to traverse the file contents line by line:

with open('the-zen-of-python.txt') as f:
    for line in f:
        print(line)

Read UTF-8 encoded file

The above example code can read ASCII encoded text files. However, if we need to deal with other languages, such as Chinese characters, the file encoding will not use ASCII, but is likely to be UTF-8.

To open the UTF-8 file, we need to pass an encoding = 'UTF-8' parameter to the open() function. For demonstration purposes, we create the following quotes Txt file in UTF-8 format:

Spring sleeps without dawn, and birds sing everywhere.
After one night of wind and showers, how many are the fallen flowers?

The following code demonstrates how to read the file:

with open('quotes.txt', encoding='utf8') as f:
    for line in f:
        print(line.strip())

summary

  • Open the text file in read mode using the open() function and the 'r' parameter.
  • Use read(), readline(), or readlines() to read the contents of the file.
  • After reading the file, use the close() method to close the file, or use the with statement to close the file automatically.
  • Use encoding = 'UTF-8' to read files in UTF-8 encoding format.

Topics: Python