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 fileHowever, 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 ...