Dewagg
Dewagg

Reputation: 145

Python alternative for PHP feof

PHP code:

...
fseek($filesrc, 40);
while ( !feof($filesrc) ) {
    $slen_1 = fread($filesrc, 4);
...

$slen_1 for example prints "Stop route"

Python code:

with open(filename, 'rb') as filesrc:
    filesrc.seek(40)
    while True:
        b = filesrc.read(1)
        if not b:
            break

        slen_1 = filesrc.read(4)

But. This python code prints "Sop rote". And I know why. Because of this line b = filesrc.read(1). Any ides how can I check for end of file in python of binary file? Thanks in advance.

Is the best way is i = 0... i = i+1 while filesrc.read(1) and then read again and do loop to known i?

Upvotes: 0

Views: 154

Answers (1)

Błotosmętek
Błotosmętek

Reputation: 12927

Just do:

with open(filename, 'rb') as filesrc:
    filesrc.seek(40)
    while True:
        slen_1 = filesrc.read(4)
        if not slen_1:
            break

Upvotes: 1

Related Questions