Counts the number of occurrences of a character ' a ' in a file

Source: Internet
Author: User

#-*-coding:utf-8-*-#python#Xiaodeng#counts the number of occurrences of a character ' a ' in a file#http://www.cnblogs.com/hongten/p/hongten_python_count.htmlImportOsnumber=0defGetNumber (filepath,c):'c---->the Word numbers'    #counts the number of occurrences of a character ' a ' in a file    ifos.path.exists (filePath):GlobalNumber with open (FilePath,'R') as FP: forLineinchFp:countnumber=Line.count (c) number=number+CountNumberElse:        Print 'The path:[{}] is not exist!'. Format (filePath)#number of numbers returned    #return numberif __name__=='__main__': File_path='OUT.txt'    PrintGetNumber (File_path,'Xiaodeng')    #Number is a global variable and can be output directly    Print Number#make some modifications to the above case:Print '**'*20defGetNumber (filepath,c):ImportOSifos.path.exists (FilePath): number=0 with open (FilePath,'R') as FP: forLineinchFp:countnumber=Line.count (c) number=number+CountNumberreturn NumberElse:        Print 'The path:[{}] is not exist!'. Format (filePath)if __name__=='__main__': File_path='OUT.txt'    PrintGetNumber (File_path,'Xiaodeng')

Counts the number of occurrences of a character ' a ' in a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.