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?
1 Antwort
+ 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.