首页 > 其他 > 详细

使用while循环遍历文件

时间:2016-09-05 17:11:53      阅读:216      评论:0      收藏:0      [点我收藏+]
/* 使用while循环遍历文件*/
[root@localhost test1]# vim 17.py
//add
#!/usr/bin/python

ll = open(‘/tmp/1.txt‘)
while True:
    line = ll.readline()
    if not line:
        break
    print line,

[root@localhost test1]# python 17.py
abc
sjdh

 

/* 另外一种写法,(常用),省去了去把变量关闭 */
[root@localhost test1]# vim 17.py
//add
#!/usr/bin/python

with open(‘/tmp/1.txt‘) as ll:
    while True:
      line = ll.readline()
      if not line:
          break
      print line,

[root@localhost test1]# python 17.py
abc
sjdh

 

使用while循环遍历文件

原文:http://www.cnblogs.com/frankielf0921/p/5842691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!