#!/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