# -*- coding: utf-8 -*-#python 27#xiaodeng#统计一个文件中出现字符'a'的次数#http://www.cnblogs.com/hongten/p/hongten_python_count.htmlimport osnumber=0def getNumber(filePath,c): 'c---->the word numbers' #统计一个文件中出现字符'a'的次数 if os.path.exists(filePath): global number with open(filePath,'r') as fp: for line in fp: countNumber=line.count(c) number=number+countNumber else: print 'the path:[{}] is not exist!'.format(filePath) #返回number数目 #return number if __name__=='__main__': file_path='out.txt' print getNumber(file_path,'xiaodeng') #number为全局变量,可以直接进行输出操作 print number#将上面的案例进行一些修改操作:print '**'*20def getNumber(filePath,c): import os if os.path.exists(filePath): number=0 with open(filePath,'r') as fp: for line in fp: countNumber=line.count(c) number=number+countNumber return number else: print 'the path:[{}] is not exist!'.format(filePath)if __name__=='__main__': file_path='out.txt' print getNumber(file_path,'xiaodeng')