Skip to Content
Python Cookbook
book

Python Cookbook

by Alex Martelli, David Ascher
July 2002
Intermediate to advanced
608 pages
15h 46m
English
O'Reilly Media, Inc.
Content preview from Python Cookbook

Reading from a File

Credit: Luther Blissett

Problem

You want to read text or data from a file.

Solution

Here’s the most convenient way to read all of the file’s contents at once into one big string:

all_the_text = open('thefile.txt').read(  )    # all text from a text file
all_the_data = open('abinfile', 'rb').read(  ) # all data from a binary file

However, it is better to bind the file object to a variable so that you can call close on it as soon as you’re done. For example, for a text file:

file_object = open('thefile.txt')
all_the_text = file_object.read(  )
file_object.close(  )

There are four ways to read a text file’s contents at once as a list of strings, one per line:

list_of_all_the_lines = file_object.readlines(  )
list_of_all_the_lines = file_object.read(  ).splitlines(1)
list_of_all_the_lines = file_object.read().splitlines(  )
list_of_all_the_lines = file_object.read(  ).split('\n')

The first two ways leave a '\n' at the end of each line (i.e., in each string item in the result list), while the other two ways remove all trailing '\n' characters. The first of these four ways is the fastest and most Pythonic. In Python 2.2 and later, there is a fifth way that is equivalent to the first one:

list_of_all_the_lines = list(file_object)

Discussion

Unless the file you’re reading is truly huge, slurping it all into memory in one gulp is fastest and generally most convenient for any further processing. The built-in function open creates a Python file object. With that object, you call the read ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Python Cookbook - Second Edition

Modern Python Cookbook - Second Edition

Steven F. Lott
Python Cookbook, 3rd Edition

Python Cookbook, 3rd Edition

David Beazley, Brian K. Jones

Publisher Resources

ISBN: 0596001673Supplemental ContentCatalog PageErrata