/* 使用while循环遍历文件*/ [[email protected] test1]# vim 17.py //add #!/usr/bin/python ll = open(‘/tmp/1.txt‘) while True: line = ll.readline() if not line: break print line, [[email protected] test1]# python 17.py abc sjdh
/* 另外一种写法,(常用),省去了去把变量关闭 */ [[email protected] 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, [[email protected] test1]# python 17.py abc sjdh
时间: 2024-11-08 20:16:56