Reading specific lines only

后端 未结 28 2163
天命终不由人
天命终不由人 2020-11-22 05:08

I\'m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?

Thanks

28条回答
  •  青春惊慌失措
    2020-11-22 05:42

    Reading from specific line:

    n = 4   # for reading from 5th line
    with open("write.txt",'r') as t:
         for i,line in enumerate(t):
             if i >= n:             # i == n-1 for nth line
                print(line)
    

提交回复
热议问题