玩蛇网提供最新Python编程技术信息以及Python资源下载!

python简单实现more的特性

python 培训

这是python练习题,完成python简单实现more的特性。思路参考Unix/Linux编程实践,python教程

#!/usr/bin/env pyton
# _*_ coding: gbk _*_

from sys import argv, exit, stdout, stdin
from os import path, isatty
from getopt import getopt, GetoptError

help_info = ["more.py [-h] file list...",
		"\t-h\t显示帮助信息"]

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 ch

class _GetchWindows:
	def __init__(self):
		import msvcrt

	def __call__(self):
		import msvcrt
		return msvcrt.getch()

getch = _Getch()

def usage():
	for l in help_info:
		print l

def 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 = 0

def 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 0


if __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()

#www.iplaypython.com

	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)

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2438.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-08 16:44 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部