File Handling:●Write a program that reads a text file and prints the number of words in it
Question
File Handling:●Write a program that reads a text file and prints the number of words in it
Solution
Sure, here is a simple Python program that reads a text file and prints the number of words in it:
def count_words_in_file(file_name):
try:
with open(file_name, 'r') as file:
text = file.read()
except FileNotFoundError:
print(f"{file_name} not
Similar Questions
Print the number of lines, words, and characters in a file.Write a program to print the number of lines, words, and characters in a file.Note: Take the input file name from the user.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtLine·count·=·7Word·count·=·51Char·count·=·306
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
Which option is used for counting the number of words in a file only?
Find the count of letters and words in a file.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code? FILE *fp; fp = fopen("NOTES.TXT", "r+"); ans. Appending Reading Read and Write Writing
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.