Python language basics 87: reading from a text file

Introduction

In the previous post we looked at context managers in with-blocks in Python. We said that context managers could be used with objects that require some type of opening and closing. We can open and close – or release – files. We can also open a HTTP channel to make a web request and then close it when we’re done. With-blocks makes our code more concise in that we don’t need to call the close() function anymore in code. Python will take care of that for us. In addition our code will be more robust since it can easily happen that the programmer forgets to call the close method on all open resources.

In this post we’ll look into reading text files. We’ll also see an example of multiple context managers.

Reading from a file

Reading from a text file is very easy and straightforward. The file.readlines() method does exactly what it says. It reads the lines from a file. It returns an array of strings with the individual lines of a file. For the next example I created a text file in the c:\tmp directory called source.txt with the classic lorem-ipsum content:

“Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.”

Here’s an example of reading from this file:

try:
    input_file_path = os.path.join('C:/', 'tmp', 'source.txt')
    with open(input_file_path, mode='rt', encoding='UTF-8') as input_file:
        source_lines = input_file.readlines()
        print(source_lines)
except IOError as e:
    print('Error: {0}'.format(e))

We’re already familiar with the with-block. The only new element here is that we open the file for reading in text mode ‘rt’. We then read all the lines and print the string array.

Note that the elements will include a so-called newline character \n where a line ends:

‘”Lorem ipsum dolor sit amet, consectetur adipiscing elit, \n’, ‘sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n’, [rest of the output ignored]

This newline character can be used when writing to a file in Python and you want to place some string in a new line, just like pressing Enter in a word processor.

Let’s now see how the extracted text can be saved in another file with another with-block:

try:
    input_file_path = os.path.join('C:/', 'tmp', 'source.txt')
    output_file_path = os.path.join('C:/', 'tmp', 'target.txt')
    with open(input_file_path, mode='rt', encoding='UTF-8') as input_file:
        with open(output_file_path, mode='wt', encoding='UTF-8') as output_file:
            source_lines = input_file.readlines()
            output_file.writelines(source_lines)
except IOError as e:
    print('Error: {0}'.format(e))

We simply include another with-block within the first one, easy as that. You’ll also see that the readlines() function has a writelines() equivalent that accepts a collection of strings to be saved.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: