python基础:案例:阅读电子书:自动翻页和手动翻页

阅读电子书
Auto=True 自动翻页,每隔2秒进行翻页读取下一页内容。
Auto=False 手动翻页,用户输入N后进行读取下一页内容。

import time
def read_book(path,line=3,auto=False):
    with open(path,mode='r') as f:
        '''获取文件的总长度'''

        f.seek(0,2)  #先跳到文件末尾
        end_position=f.tell()
        f.seek(0, 0)  #将光标移动到文件开头
        # print(end_position)
        while True:
            if auto==True:   #自动读取,自动翻页
                for i in range(line):
                    print(f.readline(),end='')

                time.sleep(2)
                now_position=f.tell()
                if now_position==end_position:
                    break



            else:          #手动翻页
                num=input('请输入N阅读下一页:')
                if num=='N':
                    for i in range(line):
                        print(f.readline(), end='')

                    now_position = f.tell()
                    if now_position == end_position:
                        print()
                        print('亲,好勤奋啊,读完了哦')
                        break

                else:
                    print('输错了,请重新输入')


read_book('b.txt',auto=False)

猜你喜欢

转载自blog.csdn.net/weixin_44239541/article/details/86255035