Python code Write a function named wordCount that counts how many words there ar
ID: 3833792 • Letter: P
Question
Python code
Write a function named wordCount that counts how many words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when wordCount is called. wordCount creates the output file. Input. The function wordCount takes two parameters: i. inFile, a string that is the name of a text file that is to be read and analyzed. The file that inFile refers to contains only upper and lower case letters and white space (no punctuation marks or other special characters). ii. outFile, a string that is the name of the file to which wordCount writes its output. The input file is in the current working directory and you should create the output file to that directory. Output. For each line of inFile, wordCount should write a corresponding line to outFile containing a single integer: the number of words on the line. If the content of the file catInTheHat.txt is below, The sun did not shine It was too wet to play So we sat in the house All that cold wet day I sat there with Sally We sat there we two And I said How I wish We had something to do the function call wordCount('catInTheHat.txt', 'catInTheHatOut.txt') should create a file named catInTheHatOut.txt with this content:
5
6
6
6
5
5
6
5
Explanation / Answer
def wordCount('catInTheHat.txt', 'catInTheHatOut.txt'):
filename = '"catInTheHat.txt "
filename1 = "catInTheHatOut.txt"
with open(filename, 'r') as f:
for line in f.readlines():
words = len(line.split(' '))
with open('filename1', 'w') as f1:
filename1.close()
filename.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.