YouTip LogoYouTip

Python3 File Next

# Python3.x Python3 File next() Method [![Image 3: Python3 File(Files) Method](#) Python3 File(Files) Method](#) * * * ### Overview **The File object in Python 3 does not support the next() method.** The built-in function next() in Python 3 calls the __next__() method on an iterator to return the next item. In a loop, the next() method is called in each iteration, returning the next line of the file. If the end of the file (EOF) is reached, a _StopIteration_ exception is raised. ### Syntax The syntax for the next() method is as follows: next(iterator[,default]) ### Parameters * **None** ### Return Value Returns the next line of the file. ### Example The following example demonstrates the use of the next() method: The content of the file tutorial.txt is as follows: This is line one line This is line two line This is line three line This is line four line This is line 5 Looping through the file content: #!/usr/bin/python3# Open Files fo = open("tutorial.txt", "r")print ("Filesnamed: ", fo.name)for index in range(5): line = next(fo) print ("Line %d line - %s" % (index, line))# Close Files fo.close() The output of the above example is: Filesnamed: tutorial.txt Line 0 - This is line 1 - This is line 2 - This is line 3 - This is line 4 - This is line 5 * * Python3 File(Files) Method](#)
← Python3 File FilenoPython3 File Fileno β†’