求文件的m至n行

时间:2023-03-09 15:23:35
求文件的m至n行
#!/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,