Python wrote a WC command.

Source: Internet
Author: User

Python writes a WC command for sending and receiving data, and more learning communication please add 365534424 , directly on the code

#!/usr/bin/env python# coding:utf-8# author: 51reboot.com# qq Group:365534424from  OPTPARSE IMPORT OPTIONPARSERIMPORT OSCLASS WC (object):     def __ Init__ (self):        self.n = 0               # line num         self.error =  '         def run (self):         self.options = main ()          if self.options.l:             # wc -l            if  Self.get_line ():                 print self.n, self.options.filename            else:                 print self.error     def get_line (self):        if  Self.options.filename and os.path.exists (Self.options.filename)  :             try:                 with open (Self.options.filename)  as f:                     for  line in f:                         self.n +=1                 return True             except IOError:                 self.error =  ' File not readable '                  return False        else:             self.error =  ' file does not exist '              return False    def  Main ():     parser = optionparser ()     parser.add_option ('-l ') , help= ' Print the length of the longest line ', action= ' store_true ')       (Options,args)  = parser.parse_args ()     if len (args)  ==  1:        options.filename = args[0]    else:         options.filename =  '     return  optionsif __name__ ==  ' __main__ ':     WC = WC ()      wc.run ()


This article is from the "Reboot DevOps Development" blog, please be sure to keep this source http://opsdev.blog.51cto.com/2180875/1569081

Python wrote a WC command.

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.