wc功能实现

一.项目相关要求

      wc.exe是一个常见的工具,它能统计文本文件的字符数,单词和行数。这个项目要求写一个命令行程序,模仿已有wc.exe的功能,并加以扩充,给出某程序设计语言的字符数、单词数和行数。

二.基本功能

-c   统计文件中字符的数量。

-w   统计文件中单词的数量。

-l     统计文件的总行数。

基本功能都已实现,高级功能未实现。

 

三.PSP

 

 

四.解题思路

利用optparse 里的OptionParser 模块,自定义选项。这里定义-l,-c,-w 三种命令,对应wc 命令的-l,-w,-c 三个命令,分别是统计行数,单词数和字符数。通过OptionParser 模块自定义的命令,python 版本的wc 命令也可以达到linux 命令wc 的效果。

 

五.功能实现

 

import os

import sys

from optparse import OptionParser

  def opt():

    parser = OptionParser()

      parser.add_option("-c", "--chars",

                         dest = "chars",                      

                         action = "store_true",

                         default = False,

                      help = "only count chars.")    

 parser.add_option("-w", "--words",

                      dest = "words",

                      action = "store_true",

                      default = False,

                      help = "only count words.")    

 parser.add_option("-l", "--lines",

                      dest = "lines",

                      action = "store_true",

                      default = False,

                      help = "only count lines.")    

 options, args = parser.parse_args()      

 return options, args  

def get_Count(data):

    chars = len(data)

    words = len(data.split())

    lines = data.count(‘\n‘)

    return lines, words, chars  

def print_wc(options, lines, words, chars, fn):

    if options.lines:

        print lines,

    if options.words:

        print words,

    if options.chars:

        print chars,    

   print fn  

def main():

    options, args = opt()

    if not (options.chars or options.words or options.lines):         options.chars, options.words, options.lines = True, True, True

    if args:

        total_lines, total_words, total_chars = 0, 0, 0

        for fn in args:

            if os.path.isfile(fn):

                with open(fn) as fd:

                    data = fd.read()

                    lines, words, chars = get_Count(data)

                    print_wc(options, lines, words, chars, fn)

                    total_lines += lines

                    total_words += words

                    total_chars += chars

            elif os.path.isdir(fn):

                print >> sys.stderr, "%s: is a directory." % fn

            else:

                sys.stderr.write("%s: No such file or directory.\n" % fn)

        if len(args) >1:

            print_wc(options, total_lines, total_words, total_chars, ‘total‘)

      else:

        data = sys.stdin.read()

        fn = ""

        lines, words, chars = get_Count(data)

        print_wc(options, lines, words, chars, fn)

  if __name__ == ‘__main__‘:

    main()

 

python脚本运行结果:

 

 六.总结

 

此次是第一次用python写一个项目,还有许多不懂得地方,高级功能里面的知识还未学习深入。

一开始以为这个项目的工程量不大,题目看似不复杂,但是实现起来还是有许许多多的问题,今后要多学习相关的知识。

这次项目对于我来说还是有许多缺陷的,例如代码可能不够简洁,高级功能没有实现,写程序的时候也不够熟练,出现了一些bug,效率不够高

但是这次的实现项目对于我来说是帮助很大的,也是动手实践的一次很棒的经历。

转载于:https://www.cnblogs.com/Boeoooo/p/9649706.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值