How to read a file line-by-line into a list?


Question

How do I read every line of a file in Python and store each line as an element in a list?

I want to read the file line by line and append each line to the end of the list.

1
2029
11/16/2018 12:14:30 AM

with open(filename) as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content] 
2014
7/19/2019 12:56:29 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon