国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

在Python中使用next()方法操作文件的教程

2019-11-25 17:23:21
字體:
來源:轉載
供稿:網友

 next()方法當一個文件被用作迭代器,典型例子是在一個循環中被使用,next()方法被反復調用。此方法返回下一個輸入行,或引發StopIteration異常EOF時被命中。

與其它文件的方法,如ReadLine()相結合next()方法工作不正常。然而,usingseek()將文件重新定位到一個絕對位置將刷新預讀緩沖器。
語法

以下是next()方法的語法:

fileObject.next(); 

參數

  •     NA

返回值

此方法返回下一個輸入行。
例子

下面的示例演示next()方法的使用。

#!/usr/bin/python# Open a filefo = 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 linefor index in range(5):  line = fo.next()  print "Line No %d - %s" % (index, line)# Close opend filefo.close()

當我們運行上面的程序,它會產生以下結果:

Name of the file: foo.txtLine No 0 - This is 1st lineLine No 1 - This is 2nd lineLine No 2 - This is 3rd lineLine No 3 - This is 4th lineLine No 4 - This is 5th line

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖边县| 辽源市| 东阿县| 长白| 额济纳旗| 乌鲁木齐市| 龙胜| 望城县| 吉安市| 嫩江县| 沙洋县| 吉隆县| 同仁县| 大化| 泾源县| 探索| 巴林右旗| 延边| 桂东县| 桃源县| 林芝县| 中江县| 江安县| 苏州市| 民和| 桓台县| 蓝田县| 吉木萨尔县| 绵阳市| 彩票| 博乐市| 垣曲县| 老河口市| 卢龙县| 沾益县| 武义县| 黎川县| 巴南区| 金川县| 巧家县| 石城县|