Python计算文件中单词个数

功能描述:

在linux命令行运行Python程序WordCount

1.如果只输入单个文件无命令则顺序输出此文件中单词个数、文本行数、字节数。

2.如果输入多个文件无命令则顺序输出这两个文件中单词个数、文本行数、字节数,并且计算每一列的总和total。

3.如1,2所示那样,只不过如果输入带有参数命令的则根据参数命令分别打印相应的文本,命令-w打印单词个数,-l打印文本行数,-c打印文本字节数。

代码如下:

程序名称为WordCount的缩写,wc,不要理解错误。

def wc():
    import sys



    wcount = 0
    lcount = 0
    bcount = 0

    wtotal = 0
    ltotal = 0
    btotal = 0

    command = sys.argv[1:]
    print(command)


    for i in sys.argv[1:]:
        i = str(i)
        #print(command)

        if ".txt" in i or ".doc" in i: 
             try:
                   file = open(i,'r')
                   text = file.read()
                   file.close()
                   
                   wcount = len(text.split())
                   lcount = len(text.split("\n"))
                   bcount = len(text)
                   wtotal = wtotal+wcount
                   ltotal = ltotal+lcount
                   btotal = btotal+bcount
                   
                   if len(sys.argv[0:])< 100:
                        if "-c" in command:
                        #print("-c")
                            if "-l" in command:
                            #print("-l")
                                if "-w" in command:
                                #print("-w")
                                    print(wcount,lcount,bcount,i)
                                    continue
                                else:
                                    print(lcount,bcount,i)
                                    continue
                            else:
                                print (bcount,i)
                                continue
                        elif "-l" not in command and "-w" not in command and "-c" not in command:
                           print(wcount,lcount,bcount,i)
                           continue
                        if "-w" in command:
                             #print("-w")
                             if "-l" in command:
                                 #print("-l")
                                 if "-c" in command:
                                     #print("-c")
                                     print(wcount,lcount,bcount,i)
                                     continue
                                 else:
                                     print(lcount,wcount,i)
                                     continue
                             else:
                                 print(wcount,i)
                                 continue
                        elif "-l" not in command and "-w" not in command and "-c" not in command:
                           print(wcount,lcount,bcount,i)
                           continue
                        if "-l" in command:
                             #print("-l")
                             if "-w" in command:
                                 #print("-w")
                                 if "-c" in command:
                                     #print("-c")
                                     print(wcount,lcount,bcount,i)
                                     continue
                                 else:
                                     print(lcount,wcount,i)
                                     continue
                             else:
                                 print(lcount)
                                 continue
                        elif "-l" not in command and "-w" not in command and "-c" not in command:
                           print(wcount,lcount,bcount,i)
                           continue
                            #print(wtotal,ltotal,btotal,"total")
                       
             except IOError:
                   print('wrong file')
                


    if "-c" in command and "-l" in command and "-w" in command and (wtotal != wcount or ltotal != lcount or btotal != bcount ):
        print(wtotal, ltotal, btotal, "total")
    elif "-c" in command and "-l" in command and "-w" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(ltotal,btotal,"total")                                                                       
    elif "-c" in command and "-w" in command and "-l" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(wtotal,btotal,"total")                                                                      
    elif "-l" in command and "-w" in command and "-c" not in command  and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(wtotal,ltotal,"total")

    elif "-l" in command and "-w" not in command and "-c" not in command  and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(ltotal,"total")
    elif "-l" not in command and "-w" in command and "-c" not in command  and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(wtotal,"total")
    elif "-l" not in command and "-w" not in command and "-c" in command  and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
        print(bcount,"total")

    elif wtotal != wcount or ltotal != lcount or btotal != bcount :
        print(wtotal, ltotal, btotal, "total")
wc()

在命令行格式化输入的格式如下:

多个文件:


单个文件:


其中,1.txt , 2.txt , 3.txt 是与.py文件相同路径下的3个文件,测验所用。

1.txt的内容为

one

2.txt的内容为

onetwo

3.txt的内容为

one

two

three

代码简单易懂,注释就没写。





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值