Read only some lines of a file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Read only some lines of a file

How to read only specific lines of a file? For example I want to read from the third to the fourth line only...what could I do?

6th Sep 2016, 6:57 PM
Hymn
Hymn - avatar
1 Answer
+ 1
I think you have already found the way how to read it. Following is my coding: f = open("1.py") #1.py is the filename you want to open li = f.readlines() #read all lines in file into a list named li print(li[2], li[3]) #output the third and fourth line. you`d better to check whether list li has more the 4 items. (if len(li)>=4) Good luck, enjoy the coding.
3rd Oct 2016, 4:02 PM
Marisa
Marisa - avatar