
#!/usr/bin/env python def read_file(file_name,start,stop): start_line = 0 try: with open(file_name) as f: for line in f: start_line += 1 #计数循环 if start <= start_line <= stop: yield line except IOError, e: raise e reslut = read_file('/etc/passwd',2,10) for lines in reslut: print lines,