Q:

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

belongs to collection: Python file handling programs

0

Python program to read contents of the file using readlines() method.

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

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

  • The readlines() method is used to return a list that contains lines of the file. Each line is an item of the list.

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 readlines() which will be stored in a list.
    • Step 2.2: print the list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to illustrate the solution of the problem

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

data=F.readlines()
print(data)

F.close()

Output:

['File Handling in Python Programming Language\n', 'Reading files using readLine ']

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Check if the record is present in the file using i... >>
<< Read contents of a file using readline() method an...