How to Check EOF in Python

Checking EOF in python is one of the trickiest parts. Since Python does not provide any such function or parameter to find out EOF file as we used to use in other programming languages like C  and C++.  So how do we read and find out the end of a file in Python while reading any file?

Eof is required in almost all the file handling program in Python whether you are using Text File or Binary File.

Binary File handling is a little bit different from text File handling. We have character reading, line reading functions in Python that return either string or list of string but when any one of then encounter with EOF then it return nothing and that we are going to explore.

file =open('data.txt','r')
pos =0
while True:
  data = file.read(1)
  if not data:
    break
  pos=pos+1
  file.seek(pos)
  print(data, end='')

file.close()

Hope You would like this solution.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.