[Python-talk] {Python-talk] Running off the end of a file during read?
Christopher Schmidt
crschmidt at crschmidt.net
Sun Jun 14 12:57:25 EDT 2009
On Sun, Jun 14, 2009 at 12:24:05PM -0400, Bruce Labitt wrote:
> Folks, I have been coding a binary datalog file reader for a colleague
> of mine in the UK. He bought a really cool RS232 datalogger that he
> managed to hook up to an ECU (engine control unit). I do a read(56)
> because I am looking for a particular sequence. My method works ok for
> a file that contains multiple sessions, but fails on the last session (I
> suspect) or on a file with ony one session.
>
> Basically I read 56 bytes then look for a sequence in it. If I don't
> find it I reset the file pointer so that it is incremented by 1 from the
> start of the last 56 byte read. The program keeps going until it finds
> the right sequence.
>
> Obviously, if either the sequence is not there, or one hits end of file
> things would go haywire (program hang in my case).
>
> Does f.read(n) error if the function hits eof? If not how can I
> determine when I hit eof?
>>> f = open("123.txt")
>>> s = f.read(2); len(s)
2
>>> s = f.read(2); len(s)
2
>>> s = f.read(2); len(s)
0
Check your len(), compare it to your read() length, see if the numbers differ?
or is there something I'm missing here?
Regards,
--
Christopher Schmidt
Web Developer
More information about the Python-talk
mailing list