python中的count()函数,从字面上可以知道,他具有统计功能
功能:
读取一个文件'c:\\test.txt',统计出该文件中出现字符'a'的次数
#python open
import os
number = 0
def get_number_of_char(filePath, c):
'''
from path, the system will get the file
finally return the number char: c
'''
if os.path.exists(filePath):
global number
with open(filePath, 'r') as fp:
for line in fp:
a = line.count(c)
number = number + a
else:
print('the path:[{}] is not exist!'.format(filePath))
def main():
file_path = 'c:\\test.txt'
get_number_of_char(file_path, 'a')
print(number)
if __name__ == '__main__':
main()