Advertisement
here2share

### jump_to_a_text_file_line_addr.py

Sep 9th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. ### jump_to_a_text_file_line_addr.py
  2.  
  3. # You can't jump ahead without reading in the file at least once, since you don't know where the line breaks are. You could do something like:
  4.  
  5. # Read in the file once and build a list of line offsets
  6. line_offset = []
  7. offset = 0
  8. for line in file:
  9.     line_offset.append(offset)
  10.     offset += len(line)
  11. file.seek(0)
  12.  
  13. # Now, to skip to line n (with the first line being line 0), just do
  14. file.seek(line_offset[n])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement