Use Python to implement the Linux system WC command with the same effect

Source: Internet
Author: User

The code is as follows:

#!/usr/bin/python#*-*coding:utf8*-*import sysimport osfrom optparse import  Optionparser "" "Define Parameter" "" Parser = optionparser () parser.add_option ("-L",  "--line",                   dest= "Lines",                    action= "Store_true",                   default=False,                   help= "Only count lines") parser.add_option ("-W",  "--word",                   dest= " Words ",                   action= "Store_true",                   default= false,                   help= "Only count words") parser.add_option ("-C",  "--char",                   dest= "Chars",                   action= "Store_true",                    default=false,                   help= "Only count chars") parser.add_option ("-N",  "--nototal",                   dest= "Nototal",                   action= "Store_true",                   default= false,                   help= "No count total") Options, args = parser.parse_args () "" "displays different values according to the specified different options" " Def display (l, w, c):     global total_l    total_l  += l    global total_w    total_w += w     global total_c    total_c += c    if  not  (Options.words or options.chars or options.lines):         print (L),         print (W),       &nbsP; print (c),    if options.lines:         print (L),    if options.words:         Print (w),     if options.chars:        print (c ), "" "for file special processing, if it is 1 files above then you need to enter a total sum" "" Def dir (data):    if not  Os.path.exists (data):         sys.stderr.write ("%s no such  file or directory\n " %data)         return  False    if os.path.isdir (data):         Sys.stderr.write ("%s is a directory\n"  %data)          return False    return True         def readfile (data):   &Nbsp; for f in data:        b = dir (f)         if b:             with open (f)  as files:                 fd = files.read ()                  l = fd.count ("\ n")                  w = len (Fd.split ())                 c =  len (FD)                  Display (L, W, C)                  print (f)         else:             continue    if  (len (args)  > 1)  and  (not  Options.nototal):        l = total_l         w = total_w        c =  Total_c        display (l, w, c)          print ("Total") total_l = 0total_w = 0total_c = 0if  Len (args)  == 0:    data = sys.stdin.read ()     l  = data.count ("\ n")     w = len (Data.split ())     c  = len (data)     display (l, w, c) else:    data  = args     ReadFile (data) 


The results of the implementation are as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/96/EF/wKioL1km3eOirkrdAAC1PkSzHb4361.png-wh_500x0-wm_ 3-wmp_4-s_2255134754.png "title=" p}mtn{{s8) 5uk0tznug9in8.png "alt=" wkiol1km3eoirkrdaac1pkszhb4361.png-wh_50 "/ >


Use Python to implement the Linux system WC command with the same effect

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.