Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in Python by (4k points)
recategorized by
Can someone tell me any methods to read every line of a file in Python and store each line as an element in a list?

File should be readable line by line and I want to append each line till the end of list.

1 Answer

0 votes
by (46k points)
edited by

Use this code as a reference to your question:

with open(fname) as f:
    content = f.readlines()
# if you don't want whitespace characters like `\n` at the end of each line
content = [q.strip() for q in content]

You can ask your doubts in comments down below, Happy Learning...!! 

Related questions

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
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...