Python read text file from second line to fifteenth

Den1al picture Den1al · Aug 24, 2013 · Viewed 44.9k times · Source

I have a text file and I need to read from the seconds line to to 15th line including. I've tried some methods but no method worked for me... I'd be happy if anyone could help me ... thanks a lot!

Answer

Jon Clements picture Jon Clements · Aug 24, 2013

Use itertools.islice:

from itertools import islice
with open('filename') as fin:
    for line in islice(fin, 1, 16):
        print line