Python version WC

Source: Internet
Author: User

#!/usr/bin/env python#coding:utf8from optparse import optionparserimport sys,osdef  opt ():     parser = optionparser ()     parser.add_option ("-C", "--char",                       dest= "Chars",                       action= "Store_true",                        default=False,                       help= "Only count chars")          parser.add_option ("-W", "--word",                       dest= "Words",                       action= "Store_true",                        default=False,                       help= "Only count words")          parser.add_option ("-L", "--line",                       dest= "Lines",                        action= "Store_true",                       default=false,                       help= "Only count lines ")      parser.add_option ("-N ","--no-total ",                       dest= "Nototal",                        action= "Store_true",                       default=False,                       help= "No  total ")     options,args = parser.parse_args ()       return options, args "parameter module" def get_count (data):     chars = len (data)      words = len (Data.split ())     lines = data.count (' \ n ')      return lines,words,chars ' statistical module '     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 ' Judging parameters ' ' Def main ():     options,args = opt ()     if not  (options.lines or  options.words or options.chars):         options.lines  , options.words , options.chars = true,true,true     "" To determine if there are parameters, if no parameters, then output all parameters ' '      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  direcotry\n " %fn)         if len (args)  > 1  And not options.nototal:&nbsP;           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 ()


This article is from the "Wesley" blog, please be sure to keep this source http://szk5043.blog.51cto.com/8456440/1875872

Python version WC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.