python编写more命令文本阅读器

好久之前接触了一下python,但是没有实际的项目需求,所以也闲置了,最近正好看到一个适合新手的练手项目,自己编写一个more命令文本阅读器,就趁机分享一下。

一、 编写工具PyCharm

我使用的pycharm工具,确实很好用,调试什么的也很方便,具体安装的教程请大家自行百度,可以免费试用30天,如果后续想继续使用只能自己购买或者其他途径了。https://www.jetbrains.com/pycharm/download/#section=windows

二、基本思路

  1. 打开文本文件
if __name__ == '__main__':
    file_path = peak_file()
    mode = 'r+'
    with open(file_path, 'rb') as frb:
        # 检测编码方式
        cur_encoding = chardet.detect(frb.read()[0:100])['encoding']
    with open(file_path, mode, encoding=cur_encoding, errors='ignore') as f:
        data = f.readlines()
import tkinter as tk
import chardet
from tkinter import filedialog


def peak_file():
    root = tk.Tk()
    root.withdraw()
    return filedialog.askopenfilename()

这里peek_file的目的是为了弹出弹窗选择文件,需要用自带的tkinter模块,同时采用chardet模块的detect函数来自动识别文本的编码。
利用with open 来打开文本,把所有数据读取到data文件中

  1. 显示文本
    显示文本采用print函数
# data 读取的文本文件 start显示起始行数 count显示多少行
def print_some_line(data, start, count):
    start = start % len(data)
    for i in range(0, count):
        if start + i >= len(data):
            start = start - len(data)
        print(start + i, end="  ")
        print(data[start + i], end="")

  1. 命令操作
    在while循环中不断检测命令,input函数检测输入的数据,回车确定。
        while 1:
            in_command = input(r"-----%.1f%%-----" % (now_line / datalen * 100))
            if len(in_command) == 0:
                print_some_line(data, now_line, 3)
                now_line = now_line + 3
                continue
            # q 表示退出循环
            if (in_command == 'q') or (in_command == 'Q'):
                print("退出,q!")
                break

            # 空格(space) 显示下一个屏幕
            if in_command == " ":
                print_some_line(data, now_line, each_screen)
                now_line = now_line + each_screen

            # b 表示回退上一个屏幕
            if (in_command == 'b') or (in_command == 'B'):
                print_some_line(data, now_line - each_screen * 2, each_screen)
                now_line = now_line - each_screen

            # space+123 表示继续从后123行开始显示
            if in_command[0] == ' ':
                if in_command[1:].isdigit():
                    now_line = now_line + int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # n123 表示直接跳转到123行
            if in_command[0] == 'n':
                if in_command[1:].isdigit():
                    now_line = int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # b12表示屏幕回滚12行
            if in_command[0] == 'b':
                if in_command[1:].isdigit():
                    now_line = now_line - int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # e12表示一屏幕显示12行
            if in_command[0] == 'e':
                if in_command[1:].isdigit():
                    each_screen = int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # r35表示达到35%处
            if in_command[0] == 'r':
                if in_command[1:].isdigit():
                    now_line = int(int(in_command[1:], 10) * datalen / 100)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            #
            now_line = now_line % datalen

三、全部功能和代码

全部功能:

  1. 输入q退出
  2. 输入空格显示下一个屏幕
  3. b 表示回退上一个屏幕
  4. space+123 表示继续从后123行开始显示
  5. n123 表示直接跳转到第123行
  6. b12表示屏幕向前回滚12行
  7. e12表示一屏幕显示12行,默认5行
  8. r35表示达到35%处
import tkinter as tk
import chardet
from tkinter import filedialog


def peak_file():
    root = tk.Tk()
    root.withdraw()
    return filedialog.askopenfilename()


def print_some_line(data, start, count):
    start = start % len(data)
    for i in range(0, count):
        if start + i >= len(data):
            start = start - len(data)
        print(start + i, end="  ")
        print(data[start + i], end="")


if __name__ == '__main__':   
    file_path = peak_file()
    mode = 'r+'
    with open(file_path, 'rb') as frb:
        # 检测编码方式
        cur_encoding = chardet.detect(frb.read()[0:100])['encoding']
    with open(file_path, mode, encoding=cur_encoding, errors='ignore') as f:
        data = f.readlines()
    datalen = len(data)
    if datalen <= 5:
        print_some_line(data, 0, datalen)
    else:
        each_screen = 5
        print_some_line(data, 0, each_screen)
        now_line = each_screen
        while 1:
            in_command = input(r"-----%.1f%%-----" % (now_line / datalen * 100))
            if len(in_command) == 0:
                print_some_line(data, now_line, 3)
                now_line = now_line + 3
                continue
            # q 表示退出循环
            if (in_command == 'q') or (in_command == 'Q'):
                print("退出,q!")
                break

            # 空格(space) 显示下一个屏幕
            if in_command == " ":
                print_some_line(data, now_line, each_screen)
                now_line = now_line + each_screen

            # b 表示回退上一个屏幕
            if (in_command == 'b') or (in_command == 'B'):
                print_some_line(data, now_line - each_screen * 2, each_screen)
                now_line = now_line - each_screen

            # space+123 表示继续从后123行开始显示
            if in_command[0] == ' ':
                if in_command[1:].isdigit():
                    now_line = now_line + int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # n123 表示直接跳转到123行
            if in_command[0] == 'n':
                if in_command[1:].isdigit():
                    now_line = int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # b12表示屏幕回滚12行
            if in_command[0] == 'b':
                if in_command[1:].isdigit():
                    now_line = now_line - int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # e12表示一屏幕显示12行
            if in_command[0] == 'e':
                if in_command[1:].isdigit():
                    each_screen = int(in_command[1:], 10)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            # r35表示达到35%处
            if in_command[0] == 'r':
                if in_command[1:].isdigit():
                    now_line = int(int(in_command[1:], 10) * datalen / 100)
                    print_some_line(data, now_line, each_screen)
                    now_line = now_line + each_screen

            #
            now_line = now_line % datalen

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值