由于你不知道换行符在哪里,因此无法至少一次不读入文件就无法跳转。你可以执行以下操作:
# Read in the file once and build a list of line offsetsline_offset = []offset = 0for line in file: line_offset.append(offset) offset += len(line)file.seek(0)# Now, to skip to line n (with the first line being line 0), just dofile.seek(line_offset[n])



