Skip to content
March 2, 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 Gupta6 hours ago5 days 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

Leave a Reply Cancel reply

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

Related News

Python Program to Print Output Without a Newline

Write a Python Program to Print Output Without a Newline

Akash Gupta1 day ago5 days ago 0
Python Program to Get a Substring of a String

Write a Python Program to Get a Substring of a String

Akash Gupta2 days ago5 days ago 0

Recent Posts

  • Write a Python Program Read a File Line by Line Into a List
  • Write a Python Program to Print Output Without a Newline
  • Write a Python Program to Get a Substring of a String
  • Write a Python Program to Get the Last Element of the List
  • Write a Python Program to Convert String to Datetime
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 Read a File Line by Line Into a List
  • Write a Python Program to Print Output Without a Newline
  • Write a Python Program to Get a Substring of a String
  • Write a Python Program to Get the Last Element of the List
  • Write a Python Program to Convert String to Datetime

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.