Read contents of the file using readline() method in Python

Here, we are going to learn how to read contents of the file using readline() method in Python?
Submitted by Shivang Yadav, on February 11, 2021

Program Statement: Python program to read contents of the file using readline() method.

Program Description: We will read the characters of a files line by line using the readline() method in python.

We will use the concepts of file handling in python to read the contents of a file line by line using readline() method.

Steps to read contents of file:

  • Step 1: Open the file in read mode using 'r'.
  • Step 2: Read the data from the file line by line.
    • Step 2.1: Extract each line, we will use readline().
    • Step 2.2: Print each line.

Program to illustrate the solution of the problem

import time

F=open("names.dat","r")

while(True):
	data=F.readline()
	if(data==""):break

	print(data,end='')

	time.sleep(1)
F.close()

Output:

File Handling in Python Programming Language
Reading files using readLine 

In the above code, we will have opened the file named 'names.dat' using 'r' mode. We will read the contents of the file line by line using the readline() method and print it.

Python file handling programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.