{"id":10007,"date":"2021-09-30T14:00:56","date_gmt":"2021-09-30T08:30:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=10007"},"modified":"2021-11-22T18:34:34","modified_gmt":"2021-11-22T13:04:34","slug":"python-program-to-count-the-occurrences-of-a-word-in-a-text-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-occurrences-of-a-word-in-a-text-file\/","title":{"rendered":"Python Program to Count the Occurrences of a Word in a Text File"},"content":{"rendered":"
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Files and File Handling in Python:<\/strong><\/p>\n A file is a piece of information or data that is saved in computer storage devices. One of the most crucial aspects of any language is file handling. The Python programming language allows two types of files. The first is a text file, which stores data in the form of text that humans and computers can read. The second type is a binary file, which stores binary data and is only readable by a computer. I<\/p>\n File handling is the process of managing files on a file system. Each operating system has its own method of storing files.<\/p>\n Python file handling comes in useful when working with files in our projects. We don’t need to be concerned about the underlying operating system or its file system rules and operations.<\/p>\n Below is the full approach to calculate the number of occurrences of the given word in the given text file in python<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n hello.txt<\/strong><\/p>\n Google Colab Images:<\/strong><\/p>\n Files and Code:<\/strong><\/p>\n <\/p>\n Code:<\/strong><\/p>\n <\/p>\n Output Image:<\/strong><\/p>\n <\/p>\n Hello.txt<\/strong><\/p>\n Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial. Files and File Handling in Python: A file is a piece of information or data that is saved in computer storage devices. One of the most crucial aspects of any language is file handling. …<\/p>\nProgram to Count the Occurrences of a Word in a Text File in Python<\/h2>\n
\n
# Take a variable say wordCount that stores the calculate the\r\n# count of given word in a given file and initialize it to 0.\r\nwordCount = 0\r\n# Scan the given word by user input using the int(input()) function and store it in a variable.\r\ngivenword = input('Enter the given word = ')\r\n# Enter the file's name into the input() function and save it to a variable.\r\nfilename = input(\"Enter the file name = \")\r\n# Open the file with the entered file name in read mode.\r\nwith open(filename, 'r') as givenfile:\r\n # Traverse the lines in the file using a For loop.\r\n for fileline in givenfile:\r\n # Using the split() method, split the line into words.\r\n wordslist = fileline.split()\r\n # To traverse the words in the list, use a For loop.\r\n for words in wordslist:\r\n # If the iterator value is equal to the given word then increase the wordCount by 1.\r\n if(words == givenword):\r\n wordCount = wordCount+1\r\n# Print the wordCount.\r\nprint('The total count of the given word {',\r\n givenword, '} in the given file = ', wordCount)\r\n<\/pre>\n
Enter the given word = btechgeeks Enter the file name = hello.txt \r\nThe total count of the given word { btechgeeks } in the given file = 3<\/pre>\n
\n
hello this is btechgeeks python programming learning platform btechgeeks btechgeeks btechgeeks.<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n