File Next
# Python2.x Python File next() Method
[ Python File Methods](#)
* * *
### Overview
The **next()** method is used when iterating over a file. In a loop, the next() method is called in each iteration. It returns the next line of the file. If the end of the file (EOF) is reached, it raises a _StopIteration_ exception.
### Syntax
The syntax for the next() method is as follows:
fileObject.next();
### 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 .txt is as follows:
This is the first lineThis is the second lineThis is the third lineThis is the fourth lineThis is the fifth line
Loop to read the file content:
## Example (Python 2.0+)
#!/usr/bin/python# -*- coding: UTF-8 -*-# Open the file fo = open(".txt", "r+")print"File name: ", fo.name for index in range(5): line = fo.next()print"Line %d - %s" % (index, line)# Close the file fo.close()
The output of the above example is:
File name: .txt Line 0 - This is the first lineLine 1 - This is the second lineLine 2 - This is the third lineLine 3 - This is the fourth lineLine 4 - This is the fifth line
[ Python File Methods](#)
YouTip