Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Read file by line from a point midway through (by byte) [duplicate]

Tags:

python

file

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?

like image 640
EddyTheB Avatar asked Feb 20 '23 01:02

EddyTheB


1 Answers

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

f.seek(4)
print f.readline()
>>> o, world!
like image 103
Ant P Avatar answered Feb 25 '23 14:02

Ant P