python 统计一个文件的行数单词数字符数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 统计一个文件的行数单词数字符数相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

def wordCount(s):
    chars = len(s)
    words = len(s.split())
    lines = s.count(\n)
    print lines, words, chars

s = open(/etc/passwd).read()
wordCount(s)

 

 

 

 

 

    

以上是关于python 统计一个文件的行数单词数字符数的主要内容,如果未能解决你的问题,请参考以下文章