What is the most efficient way to get first and last line of a text file?
问题 I have a text file which contains a time stamp on each line. My goal is to find the time range. All the times are in order so the first line will be the earliest time and the last line will be the latest time. I only need the very first and very last line. What would be the most efficient way to get these lines in python? Note: These files are relatively large in length, about 1-2 million lines each and I have to do this for several hundred files. 回答1: docs for io module with open(fname, 'rb'