Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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 Answer

0 votes
by (106k points)

To read a file line-by-line into a list you can use the below-mentioned code:-

with open(filename) as f:

content = f.readlines() 

content = [x.strip() for x in content]

Related questions

+1 vote
1 answer
0 votes
1 answer
asked Jul 26, 2019 in Python by selena (1.6k points)
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...