前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python版wc

python版wc

作者头像
py3study
发布2020-01-06 14:06:54
7330
发布2020-01-06 14:06:54
举报
文章被收录于专栏:python3

#!/usr/bin/env python

#coding:utf8

from optparse import OptionParser

import sys, os

#parser = OptionParser()

def set():

   parser = OptionParser("Usage: %prog [file1] [file2]...")

   parser.add_option("-c",

       "--chars",

       dest="characters",

       action="store_true",

       default=False,

       help="only count characters",

       )

   parser.add_option("-w",

       "--words",

       dest="words",

       action="store_true",

       default=False,

       help="only count words",

       )

   parser.add_option("-l",

       "--lines",

       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="only count not total",

       )

   return parser.parse_args()

class Count_file(object):

   l,w,le,lens,wo,lines=0,0,0,0,0,0

   nu=" "*5

   def get_count(self):

       self.lens=len(data)

       self.wo=len(data.split())

       self.lines=data.count("\n")

   def print_wc(self):

       if options.characters:

           print self.lines,

           self.l += self.lines

       if options.words:

           print self.wo,

           self.w += self.wo

       if options.lines:

           print self.lens,

           self.le += self.lens

       try:

           print args[i]

       except:

           print "stdin"

   def print_total(self):

       if options.nototal:

           pass

       else:

           if len(args)>1:

               for i in (self.l,self.w,self.le):

                   if i ==0:

                       pass

                   else:

                       print i,

               print "总用量"

   def print_fi_pa(self):

       print __file__,":",args[i],":"+"没有那个文件或目录!"

   def print_path(self):

       print __file__,":",args[i],":"+"是一个目录"

       print self.nu,self.l,self.nu,self.w,self.nu,self.le,args[i]

if __name__ =="__main__":

   options,args = set()

   test=Count_file()

   if (options.characters or options.words or options.lines):

       pass

   else:

       options.characters, options.words, options.lines= True, True, True

   if args:

       for i in xrange(len(args)):

           if os.path.isfile(args[i]):

               data=file(args[i]).read()

               test.get_count()

               test.print_wc()

           elif os.path.isdir(args[i]):

               test.print_path()

           else:

               test.print_fi_pa()

       test.print_total()

   else:

       data=sys.stdin.read()

       test.get_count()

       test.print_wc()

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档