How to check whether it is the end of file in Python

The end of file, or EOF, is the point at which no more data can be read. For example, in a text file containing “abc”, the EOF is reached after “c”.

Solution for How to check whether it is the end of file in Python : You can use file.read() to check for EOF Call open(file, mode) with “r” as mode to open file for reading. Call file.read() to read the contents of file. If file.read() returns an empty str, it is the EOF.

Other Solutions

Use for to check for EOF

  • If using a for-loop to read a file, the loop will automatically terminate once reaching the EOF.
  • Further reading: See documentation for file reading here.


how-to-check-whether-it-is-the-end-of-file-in-python