题目是访问一个文件,每次显示25行,并提示按任意键继续。
下面是我写的代码,测试通过,缺点是需要建一个比较大的列表,在文件非常大的时候占用内存过多。
import os f = open(os.path.expanduser('~/PycharmProjects/MyPython/test.conf')) flist = f.readlines() list = [x for x in range(0, len(flist), 25)] for i in range(1,len(list)): for x in flist[list[i - 1]:list[i]]: print x, print 'press any key to continue' os.system('read -s -n 1') for x in flist[int(list[-1]):len(flist) - 1]: print x, f.close()
下面这个是来自运维生存时间的代码,比上面的要好
import os fobj = open(os.path.expanduser('~/PycharmProjects/MyPython/test.conf')) count = 0 for eachline in fobj: print eachline, count += 1 if count%25 == 0: print "Press any key to continue" os.system('read -s -n 1') #print continue fobj.close()