描述
Python文件方法next()當文件用作迭代器時使用,通常在循環中,重複調用 next() 方法。此方法返回下一個輸入行,或在命中 EOF 時引發 StopIteration。
將 next() 方法與其他文件方法(如 readline())結合使用是行不通的。但是,將文件重新定位到絕對位置的 usingseek() 將刷新 read-ahead 緩衝區。
用法
以下是語法next()方法≫
fileObject.next();
參數
NA
返回值
此方法返回下一個輸入行。
示例
下麵的例子展示了 next() 方法的用法。
This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file:", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
for index in range(5):
line = fo.next()
print "Line No %d - %s" % (index, line)
# Close opend file
fo.close()
當我們運行上麵的程序時,它會產生以下結果——
Name of the file: foo.txt Line No 0 - This is 1st line Line No 1 - This is 2nd line Line No 2 - This is 3rd line Line No 3 - This is 4th line Line No 4 - This is 5th line
相關用法
- Python File tell()用法及代碼示例
- Python File seek()用法及代碼示例
- Python File flush()用法及代碼示例
- Python File write()用法及代碼示例
- Python File read()用法及代碼示例
- Python File truncate()用法及代碼示例
- Python File fileno()用法及代碼示例
- Python File open()用法及代碼示例
- Python File isatty()用法及代碼示例
- Python File writable()用法及代碼示例
- Python File writelines()用法及代碼示例
- Python File readable()用法及代碼示例
- Python File readlines()用法及代碼示例
- Python File close()用法及代碼示例
- Python File readline()用法及代碼示例
- Python File seekable()用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python Sympy Permutation.list()用法及代碼示例
- Python Matplotlib.figure.Figure.subplots_adjust()用法及代碼示例
- Python numpy.tril()用法及代碼示例
注:本文由純淨天空篩選整理自 Python File next() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。