码迷,mamicode.com
首页 > 编程语言 > 详细

每天一个 Python 小程序--0004

时间:2015-01-23 00:44:07      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

 

# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-07
# Python 3.4

"""

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

"""

import sys


def 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_num

if __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 counted

Issues and pull requests

Issues 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.
Commits

Commits 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

标签:

原文地址:http://www.cnblogs.com/mazi12/p/4242982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!