Python language basics 87: reading from a text file


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:

    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()
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:

    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()
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.

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: Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


A great site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Love to use Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: