Question: Word count. A common utility on Unix/Linux systems is a small program call wc. This program analyzes a file to determine the number of lines,

Word count. A common utility on Unix/Linux systems is a small program call "wc." This program analyzes a file to determine the number of lines, words, and characters contained therein. WRite your own version of wc. The program should accept a file name as input and then print three numbers showing the count of lines, words, and characters in the file.

I wrote my code in python but my code unable to count the words, lines, and character, please help.

def main(): fname = input("Enter filename: ") infile = open(fname, "r") data = infile.read() print(data)

wordCount = 0 characterCount = 0 lineCount = 0

for line in infile: lineCount += 1 characterCount += len(line) wordCount += len(line.split()) print("The file contains ", lineCount, "line", wordCount, "words, and", characterCount, "characters")

if __name__ == '__main__': main() # Call the main procedure declared above

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!