首页 > 代码库 > 每天一个 Python 小程序--0004

每天一个 Python 小程序--0004

第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。

 

---------------------------------------------------------------------------

 

# Source:https://github.com/Show-Me-the-Code/show-me-the-code# Author:renzongxian# Date:2014-12-07# Python 3.4"""第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。"""import sysdef word_count(file_path):    file_object = open(file_path, r)    word_num = 0    for line in file_object:        line_list = line.split()        word_num += len(line_list)    file_object.close()    return word_numif __name__ == "__main__":    if len(sys.argv) <= 1:        print("Need at least 1 parameter. Try to execute ‘python 0004.py $image_path‘")    else:        for infile in sys.argv[1:]:            try:                print("The total number of words is ", word_count(infile))            except IOError:                print("Can‘t open file!")                pass

 

test

Why are my contributions not showing up on my profile?Your profile contributions graph is a record of contributions you‘ve made to GitHub repositories. Contributions are only counted if they meet certain criteria. In some cases, we may need to rebuild your graph in order for contributions to appear.Contributions that are countedIssues and pull requestsIssues and pull requests will appear on your contributions graph if they meet both of these conditions:They were opened within the past year.They were opened in a standalone repository, not a fork.CommitsCommits will appear on your contributions graph if they meet all of the following conditions:The commits were made within the past year.The email address used for the commits is associated with your GitHub account.The commits were made in a standalone repository, not a fork.The commits were made:In the repository‘s default branch (usually master)In the gh-pages branch (for repositories with Project Pages sites)In addition, at least one of the following must be true:You are a collaborator on the repository or are a member of the organization that owns the repository.You have forked the repository.You have opened a pull request or issue in the repository.You have starred the repository.

 

每天一个 Python 小程序--0004