在 Python3 中文件對象不支持 next()方法。Python3 的內置函數 next() ,它通過調用 __next__() 方法從迭代器讀取下一個專案。如果 default 給定,如果迭代器用盡它被返回,否則引發 StopIteration 異常。 這種方法可用於讀取來自檔對象下一個輸入行。
	
語法
		以下是 next()方法的語法 -
	
next(iterator[,default])
參數
- 
			
iterator : 從中要讀取行的檔對象
 - 
			
default : 如果迭代耗盡則返回。如果沒有給出則將引發StopIteration異常
 
返回值
		此方法返回下一輸入行。
	
	示例
		下麵的示例演示 next()方法的使用。
	
Assuming that 'foo.txt' contains following lines C++ Java Python Perl PHP
#!/usr/bin/python3
# Open a file
fo = open("foo.txt", "r")
print ("Name of the file: ", fo.name)
for index in range(5):
   line = next(fo)
   print ("Line No %d - %s" % (index, line))
# Close opened file
fo.close()
	
		當我們運行上面的程式,會產生以下結果 -
	
Name of the file: foo.txt Line No 0 - C++ Line No 1 - Java Line No 2 - Python Line No 3 - Perl Line No 4 - PHP
						上一篇:
								Python3模組
												下一篇:
								Python3檔方法
					
					