4

Possible Duplicate:
python: how to jump to a particular line in a huge text file?

I'm trying to read various lines out of a large (250Mb) file.

The header tells me where certain parts are, i.e. the history subsection of the file starts at byte 241817341.

So is there a way to read the file only starting at that byte, without having to go through the rest of the file first? Something like:

file = open(file_name,'r')
history_line = file.readline(241817341)
while history_line != 'End':
    history_line = file.readline()
    [Do something with that line]

Is that sort of thing feasible?

Community
  • 1
  • 1
EddyTheB
  • 3,100
  • 4
  • 23
  • 32
  • 1
    file.seek I think is what you want ... and maybe file.tell also – Joran Beasley Jun 26 '12 at 23:28
  • 1
    @DarX: That's _close_ -- but when you know you want to start at a specific line number. There's no good way to handle that except reading the whole stupid thing in first. This is starting at a known _byte_, which is different enough that it can be done quickly. – sarnold Jun 26 '12 at 23:35
  • Cheers guys, seek works great. – EddyTheB Jun 26 '12 at 23:43

1 Answers1

8
f.seek(0)
print f.readline()
>>> Hello, world!

f.seek(4)
print f.readline()
>>> o, world!
Ant P
  • 24,820
  • 5
  • 68
  • 105