next(iterator[,default])
iterator : 從中要讀取行的檔案物件
default : 如果疊代耗盡則返回。如果沒有給出則將引發StopIteration異常
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