关于python统计文件的字符


前言 用python统计文件中的字符出现次数,打印出来,

运用到python读取函数,写一个函数计算出现次数,循环,
注明一下,chdir后是我的python文件根目录,读取的文件名是name.txt


#-*-coding:utf8;-*-
from os import chdir,system
chdir("/home/yangzheng/Desktop/python")

text = open('/home/yangzheng/Desktop/name.txt').read() 
text = str(text).lower()

import re        
chars = re.findall(r'w', text)

char_dict = dict([(a, 0) for a in chars]) 


for char in chars:
    char_dict[char] = char_dict[char] + 1 

print char_dict