Check end of file in Python Last Updated : 28 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, checking the end of a file is easy and can be done using different methods. One of the simplest ways to check the end of a file is by reading the file's content in chunks. When read() method reaches the end, it returns an empty string. Python f = open("file.txt", "r") # Read the entire content of the file into the variable 'content' content = f.read() # Check if the file content is empty if content == "": print("End of file reached") f.close() Output:End of file reachedOther methods which we can use are:Table of ContentUsing for LoopUsing file.seek() and file.tell()Using for LoopPython provides a cleaner way to read through files using a for loop. The for loop automatically stops when it reaches the end of the file. Python f = open("file.txt", "r") # Iterate through each line in the file for line in f: print(line, end="") print("End of file reached") f.close() Output: hello worldEnd of file reachedUsing file.seek() and file.tell()In some cases, we may want to check the file position manually using seek() and tell(). The tell() function gives the current position of the file pointer, and seek() moves it to a specific location. Python f = open("file.txt", "r") # Move the pointer to the end of the file f.seek(0, 2) if f.tell() == 0: print("The file is empty") else: print("The file is not empty") f.close() Output:The file is not emptyExplanation:This method checks the file size by moving the pointer to the end of the file. Since file.txt contains text, the tell() function will return a position greater than 0, so the message "The file is not empty" is printed. Comment More infoAdvertise with us Next Article Check If File is Readable in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-io Practice Tags : pythonpython-io Similar Reads Check if a File Exists in Python When working with files in Python, we often need to check if a file exists before performing any operations like reading or writing. by using some simple methods we can check if a file exists in Python without tackling any error. Using pathlib.Path.exists (Recommended Method)Starting with Python 3.4 3 min read Check a File is Opened or Closed in Python In computer programming, working with files is something we often do. Python, a programming language, gives us useful tools to handle files. One important thing to know when dealing with files is whether a file is currently open or closed. This is crucial to avoid problems and make sure the data sta 4 min read Check If a Text File Empty in Python Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.Check if a 4 min read Close a File in Python In Python, a file object (often denoted as fp) is a representation of an open file. When working with files, it is essential to close the file properly to release system resources and ensure data integrity. Closing a file is crucial to avoid potential issues like data corruption and resource leaks. 2 min read Check If File is Readable in Python We are given a file and we have to check whether the file is readable in Python or not. In this article, we will see how we can check if a file is readable or not by using different approaches in Python. How to Check if a File is Readable in PythonBelow, are the methods of How to Check If a File Is 3 min read Check If A File is Writable in Python When it comes to Python programming it is essential to work with files. One important aspect is making sure that a file can be written before you try to make any changes. In this article, we will see how we can check if a file is writable in Python. Check If a File Is Writable in PythonBelow are som 3 min read Like