A common question asked when programming with Python is How do I read every line of a file in Python and store each line as an element in a list? Below we will go over an example to show you how to read the file and append each line to the end of a list.
Reading a small file into a list
Below is an example reading from a file using the with open method. This is good for small files, it should be noted that if you are working with a large file readlines() is not very efficient and can result in MemoryErrors.
with open(filename) as f:
filecontent = f.readlines()
Reading a large file into a list
Larger files can be read more efficiently using a traditional for loop. Below is a simple example.
filecontent = [line for line in open('filename')]
Removing white space or newline characters from each line
Another common requirement is removing white space or newline characters from the end of each line. This can be done using the strip() or rstrip() methods.
Removing white space using strip()
with open(filename) as f:
filecontent = f.readlines()
filecontent = [x.strip() for x in filecontent]
Removing new lines using rstrip()
with open(filename) as f:
filecontent = f.readlines()
filecontent = [x.rstrip('\n') for x in filecontent]