Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that will count the number of lines, words, and characters in a file:

def count_file(file_name):
    with open(file_name, 'r') as file:
        lines = file.readlines()
        line_count = len(lines)
        word_count = sum(len(line.split()) for line in lines)
        char_count = sum(len(line) for line in lines)
    return line_count, word_count, char_count

file_name = input("Enter file name: ")
line_count, word_count, char_count = count_file(file_name)

print(f"Line count = {line_count}")
print(f"Word count = {word_count}")
print(f"Char count = {char_count}")

This program works by first opening the file with the given name. It then reads all the lines in the file. The number of lines is simply the length of the list of lines. The number of words is the sum of the lengths of the lists of words in each line, where a word is defined as a sequence of characters separated by whitespace. The number of characters is the sum of the lengths of the lines.

This problem has been solved

Similar Questions

Problem StatementWrite a program to count the lines, words and characters in a given text.Input FormatInput the text (Ctrl+D on a new line to end input)Output FormatPrint the no of lines, words, charactersSample Input 1This is a sample text.It has multiple lines.Each line contains words.Sample Output 1Lines: 3Words: 12Characters: 80Sample Input 2Hello WorldHappy CodingSample OutputLines: 2Words: 4Characters: 23

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

Find the count of letters and words in a file.

Program to count characters spaces lines in file content

File Handling:●Write a program that reads a text file and prints the number of words in it

1/3

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.