How to Check end of file in Python


Checking end of file in python is one of the trickiest parts. Since Python does not provide any such function or parameter to find out the end of file(EOF) function or return any such marker 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 programs but Python has some functions that automatically check end of file in Python.  Whereas in some very specific cases, we need to check end of the file.

In this tutorial, we are going to explain, how you can find out the end of the file in a Text file in Python. Python provides us basically three functions to read the contents of a text file
1. read( ) function
2. readline( ) function
3. readlines( ) functions

Read() function

read() function read the whole content of the given text file if used without arguments ( so in this case it is checking end of file automatically). Whereas it reads only that number of characters from the file if an integer number is passed in the reading () function as an argument.- So if you are planning to print or process a single character at a time from the text file, In that case, eof of file marker is required.

sample.txt file contains the following data

This is a sample file for text file handling in python
here we will learn how to
read text data character wise
word wise and whole file in a

This type of files can be generated using any text editor like notepad
python idle or even msword or any other editor like vscode and many more like that.
hope you will find this file useful for the practice work.

The  read( ) function returns the content of the whole file as a string as its return type value.

file =open('sample.txt','r')
data = file.read()
print(data)
file.close()

The above code will print all the content of the text file on the screen. There is no need to check the end of the file in this case.

Everything is required when we want to read one character at a time from the text file and then display the read character on the screen.

Sample code for checking end of file in Python

file =open('sample.txt','r')
while True:
   data = file.read(1)
   if data=='':
     print('EOF') 
     break  
   print(data)
file.close()

The above code read one character at a time from the file stream and check if the returned value is an empty string or not. if it is an empty string then, we consider it as an end of the file and stop reading the next character from the stream.

NOTE: After reading a single character from the file stream. read() function move its reading head on the next character automatically.

Readlines() functions

Readlines() functions read all the available lines of the above text file in a list of strings. So in this case again there is no need to check the end of the file. All we need to process the list of strings and that’s it.

whereas

The readline() function reads one line at a time so we need to check end of file. The readline( ) function also returns the read line as a string and returns an empty string when it encounters the end of the line. Following code is implementing end of file concenpt in sample.txt file

file =open('sample.txt')
while True:
  data = file.readline()
  if data=='':
     print('EOF')  
     break
  print(data,end='')

Hope You would like this solution. if you have any query regarding this solution. please feel free to add your voice in comments/

Print Friendly, PDF & Email

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.