+1 vote
2 views
by (4.1k points)
edited 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 (46.1k 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
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.8k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 11, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...