Python: Full version WC

Source: Internet
Author: User
Tags python script


This Python script basically implements the full functionality of the WC command in a Linux system

Vim wc.py

#!/usr/bin/python


Import OS

Import Sys

From 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", "--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", "--nototal",

Dest= "Nototal",

Action= "Store_true",

Default=false,

Help= "Show total or not")

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.lines, Options.chars = 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:

If not options.nototal:

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)


Main ()


This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1783267

Python: Full 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.