Read line number 4 from the following file

Python Work with Files (Article) Work with Files (Program)

167

Given Input: test.txt file: Create a test.txt file and add the below content to it.

line1
line2
line3
line4
line5
line6
line7

Expected Output:


Program:

# read file
with open("test.txt", "r") as fp:
    # read all lines from a file
    lines = fp.readlines()
    # get line number 3
    print(lines[2])

Output:


                                                                     
                              

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.