Python3 File next () method
Outline
Python 3 in the File object does not supportnext () method.Python 3 built-in function next () iterator by calling __next __ () method returns the next item. In the cycle, next () method will be called in each cycle, the method returns the next line of the file, if you reach the end (EOF), the triggerStopIteration
grammar
next () method has the following syntax:
next(iterator[,default])
parameter
no
return value
Returns the file the next line.
Examples
The following example demonstrates the next () method of use:
W3big.txt content file as follows:
这是第一行 这是第二行 这是第三行 这是第四行 这是第五行
Loop reads the contents of the file:
#!/usr/bin/python3 # 打开文件 fo = open("w3big.txt", "r") print ("文件名为: ", fo.name) for index in range(5): line = next(fo) print ("第 %d 行 - %s" % (index, line)) # 关闭文件 fo.close()
The above example output is:
文件名为: w3big.txt 第 0 行 - 这是第一行 第 1 行 - 这是第二行 第 2 行 - 这是第三行 第 3 行 - 这是第四行 第 4 行 - 这是第五行