Search code examples
pythonfilereadlines

Python read text file from second line to fifteenth


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!


Solution

  • Use itertools.islice:

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