简单版more的实现,简单版more实现,more.py#!/us
简单版more的实现,简单版more实现,more.py#!/us
more.py
#!/usr/bin/env pyton# _*_ coding: gbk _*_# 思路参考Unix/Linux编程实践教程from sys import argv, exit, stdout, stdinfrom os import path, isattyfrom getopt import getopt, GetoptErrorhelp_info = ["more.py [-h] file list...", "\t-h\t显示帮助信息"]#下面_Getch代码来源:#http://code.activestate.com/recipes/134892/class _Getch: """Gets a single character from standard input. Does not echo to the screen.""" def __init__(self): try: self.impl = _GetchWindows() except ImportError, err: self.impl = _GetchUnix() def __call__(self): return self.impl()class _GetchUnix: def __init__(self): import tty, sys def __call__(self): import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return chclass _GetchWindows: def __init__(self): import msvcrt def __call__(self): import msvcrt return msvcrt.getch()getch = _Getch()def usage(): for l in help_info: print ldef do_more(lines): num_of_lines = 0 i = 0 while i < len(lines): num_of_lines = num_of_lines + 1 stdout.write(lines[i]) i += 1 if num_of_lines >= 12: num_of_lines = see_more(num_of_lines) if num_of_lines < 0: i += num_of_lines if i < 0: i = 0def see_more(num_of_lines): answer = getch() if answer== " ": return 0 elif answer == "\r": return (num_of_lines - 1) elif answer == "q": exit(1) elif answer == "b": return (-num_of_lines) else: return 0if __name__ == "__main__": try: opts, args = getopt(argv[1:], "h") except GetoptError, err: print str(err) usage() exit(2) for o, a in opts: if o == "-h": usage() exit(0) else: print "Unkown options" usage() exit(2) if len(args) == 0: usage() else: for file in args: if not path.exists(file): print file + ": No such file or directory" else: print file + ":" fp = open(file, 'r') lines = fp.readlines() do_more(lines)
相关内容
- 端口复用,,端口复用及线程操作#co
- 多用户,多房间全双工聊天,多用户房间全双工,mcs.p
- 多用户全双工聊天。,多用户全双工聊天,cs.py#!/usr/
- 你还在为选她还是她而纠结吗?我有办法!,还在纠结
- 一个有趣的罪犯与盒子的问题,罪犯盒子,[Python]代码
- 使MySQLdb的SQL查询中支持?以及:xxx的查询方式,mysqldb查询
- 3行Python代码解简单的一元一次方程,3行python,[Python]代
- python删除文件及文件夹,python删除文件,import os#删除
- 批量下载图片 - Python,,#!/usr/bin/e
- 自动下载并更新壁纸(Linux可用),更新壁纸linux可用,代码
评论关闭