Answers for "python quickly goto line in file"

0

python quickly goto line in file

# Read in the file once and build a list of line offsets
line_offset = []
offset = 0
for 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 do
file.seek(line_offset[n])
Posted by: Guest on February-22-2022

Python Answers by Framework

Browse Popular Code Answers by Language