["python"]
How to read first N lines of a file?

We have a large raw data file that we would like to trim to a specified size. How would I go about getting the first N lines of a text file in python? Will the OS being used have any effect on the implementation?
Python 3: with open(path_to_file) as input_file: head = [next(input_file) for _ in range(lines_number)] print(head) Python 2: with open(path_to_file) as input_file: head = [next(input_file) for _ in xrange(lines_number)] print head Here's another way (both Python 2 & 3): from itertools import islice with open(path_to_file) as input_file: head = list(islice(input_file, lines_number)) print(head)