Skip to content
April 16, 2026

uuprogram.com

  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
Newsletter
Random News
  • Home
  • Program in python
  • Write a Python Program Read a File Line by Line Into a List
  • Program in python

Write a Python Program Read a File Line by Line Into a List

Akash Gupta2 months ago2 months ago02 mins
Python Program Read a File Line by Line Into a List

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program Read a File Line by Line Into a List
Python Program Read a File Line by Line Into a List

Write a Python Program Read a File Line by Line Into a List

PROGRAM: Python Program Read a File Line by Line Into a List

/* Python Program Read a File Line by Line Into a List */

1: Using readlines()

Let the content of the file data_file.txt be

honda 1948
mercedes 1926
ford 1903

Source code 

with open(“data_file.txt”) as f:
content_list = f.readlines()

# print the list
print(content_list)

# remove new line characters
content_list = [x.strip() for x in content_list]
print(content_list)

OUTPUT:-
['honda 1948\n', 'mercedes 1926\n', 'ford 1903']
['honda 1948', 'mercedes 1926', 'ford 1903']

2: Using for loop and list comprehension

with open(‘data_file.txt’) as f:
content_list = [line for line in f]

print(content_list)

# removing the characters
with open(‘data_file.txt’) as f:
content_list = [line.rstrip() for line in f]

print(content_list)

OUTPUT:-
['honda 1948\n', 'mercedes 1926\n', 'ford 1903']
['honda 1948', 'mercedes 1926', 'ford 1903']
Tagged: Python Program Read a File Line by Line Into a List

Post navigation

Previous: Write a Python Program to Print Output Without a Newline
Next: Write a Python Program to Randomly Select an Element From the List

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Python Program to Count the Occurrence of an Item in a List

Write a Python Program to Count the Occurrence of an Item in a List

Akash Gupta1 month ago2 months ago 0
Python Program to Check If a String Is a Number (Float)

Write a Python Program to Check If a String Is a Number (Float)

Akash Gupta1 month ago2 months ago 0

Recent Posts

  • Write a Python Program to Count the Occurrence of an Item in a List
  • Write a Python Program to Check If a String Is a Number (Float)
  • Write a Python Program to Randomly Select an Element From the List
  • Write a Python Program Read a File Line by Line Into a List
  • Write a Python Program to Print Output Without a Newline
No comments to show.

Archives

  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • August 2025
  • July 2025

Categories

  • Array Programs
  • Basic Programs
  • Conditional Programs
  • File handling
  • Java Program
  • Loop Programs
  • Matrix Programs
  • Program in C
  • program in C++
  • Program in python
  • String Programs
  • Uncategorized

Recent Posts

  • Write a Python Program to Count the Occurrence of an Item in a List
  • Write a Python Program to Check If a String Is a Number (Float)
  • Write a Python Program to Randomly Select an Element From the List
  • Write a Python Program Read a File Line by Line Into a List
  • Write a Python Program to Print Output Without a Newline

Archives

  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • August 2025
  • July 2025

Categories

  • Array Programs
  • Basic Programs
  • Conditional Programs
  • File handling
  • Java Program
  • Loop Programs
  • Matrix Programs
  • Program in C
  • program in C++
  • Program in python
  • String Programs
  • Uncategorized
uuprogram.com 2026. Powered By BlazeThemes.