当前位置:Gxlcms > Python > Python计算一个文件里字数的方法

Python计算一个文件里字数的方法

时间:2021-07-01 10:21:17 帮助过:23人阅读

本文实例讲述了Python计算一个文件里字数的方法。分享给大家供大家参考。具体如下:

这段程序从所给文件中找出字数来。

  1. from string import *
  2. def countWords(s):
  3. words=split(s)
  4. return len(words)
  5. #returns the number of words
  6. filename=open("welcome.txt",'r')
  7. #open an file in reading mode
  8. total_words=0
  9. for line in filename:
  10. total_words=total_words + countWords(line)
  11. #counts the total words
  12. print total_words

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行