A Python program I\'m writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I\'m doing s
import itertools
header_lines = list(itertools.islice(file_handle, header_len))
# or
header = "".join(itertools.islice(file_handle, header_len))
Note that with the first, the newline chars will still be present, to strip them:
header_lines = list(n.rstrip("\n")
for n in itertools.islice(file_handle, header_len))