In Python, how do I read a file line-by-line into a list?

0 votes
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.
Oct 9, 2018 in Python by ana1504.k
• 6,830 points
79 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
with open(fname) 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]
answered Oct 9, 2018 by SDeb
• 11,100 points

Related Questions In Python

0 votes
1 answer

How do I read a file on only read only mode in python?

Hey @David! TRy something like this: campaign_data = ...READ MORE

answered 5 days ago in Python by Jason
10 views
0 votes
1 answer

How do I remove an element from a list by index in Python?

You can use the pop() method to ...READ MORE

answered 4 days ago in Python by Nisa
• 830 points
13 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
490 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27 in Python by rajesh
• 1,210 points
24 views
0 votes
1 answer

How to write to CSV line by line in python

The simple way of doing this will ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,140 points
116 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
67 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 11,100 points
48 views
0 votes
1 answer

How do I convert a Python program to a runnable .exe Windows program?

Understand that every 'freezing' application for Python ...READ MORE

answered Oct 12, 2018 in Python by SDeb
• 11,100 points
39 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.