1

I need to write for loop for which I need end of file character in python. Any help is welcome.

Thanks in advance.

Parisa Rai
  • 155
  • 2
  • 4
  • 9

2 Answers2

4

read of files returns an empty string when EOF is encountered.

while True:
    chunk = fp.read(1)
    if chunk == '':
        break
Assem
  • 11,574
  • 5
  • 59
  • 97
2

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
masnun
  • 11,635
  • 4
  • 39
  • 50