I need to write for loop for which I need end of file character in python. Any help is welcome.
Thanks in advance.
I need to write for loop for which I need end of file character in python. Any help is welcome.
Thanks in advance.
read
of files returns an empty string when EOF
is encountered.
while True:
chunk = fp.read(1)
if chunk == '':
break
If you're trying to output a line break character print
would send the EOF
automatically. So you don't have to worry about it. Just use print
in the for
loop.
If you're reading from a file fp.read
returns None
when EOF
is reached.
while True:
data = fp.read()
if not data: break