#!/usr/bin/pythonimport sysimport osfrom optparse import optionparser def opt (): parser = optionparser () parser.add_option ("-C", "--char", dest= "Chars", action= "Store_true", default=False, help= "Only count chars") parser.add_option ("-W", "--work", 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= "Total", action= " Store_true ", default=False, help= "Only count total") 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.lines or options.words or options.chars): options.lines, options.words, options.chars = True, True, True if args: file = 0 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 file += 1 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 file > 1 and not options.total:              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 ()
[[email protected] ~]# python wc100.py-wcl 1.txt data.sql nginx.sh//Specific usage-W statistic words-c count Word-l count rows-N Cancel President Metering
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/97/7A/wKiom1kutDKQU1mrAAAKs_3kVYk484.png-wh_500x0-wm_ 3-wmp_4-s_4108337622.png "style=" Float:none; "title=" QQ picture 20170531201445.png "alt=" Wkiom1kutdkqu1mraaaks_ 3kvyk484.png-wh_50 "/>
[Email protected] ~]# WC-WCL 1.txt data.sql nginx.sh
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/97/7B/wKioL1kutDLAPYtGAAAJ-Lj1Gqc499.png-wh_500x0-wm_ 3-wmp_4-s_2104075932.png "title=" qq picture 20170531201506.png "style=" Float:none; "alt=" Wkiol1kutdlapytgaaaj-lj1gqc499.png-wh_50 "/>
Pytohn implementing the WC command in the Linux shell