programming-examples/python/Input_Output/Python program to read first n lines of a file.py

6 lines
241 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
def file_read_from_head(fname, nlines):
from itertools import islice
with open(fname) as f:
for line in islice(f, nlines):
print(line)
file_read_from_head('test.txt',2)