Write two functions, so that we can find the total number of lines in a collection of text files.
a) The first one takes in the name of a text file as its only parameter, reads the file line by line, and returns the number of lines in the file.
b) The second function repeatedly prompts for a file name until the user signifies no more file names by pressing the Enter key with no characters typed in. For each file name, this function calls the one in (a) above and uses the returned value to accumulates a variable. Once there is no more file name entered, the function returns the accumulated value.
Invoke the second function, from function called main().
1
Expert's answer
2015-05-01T12:41:44-0400
def workOnFile(fileName): counter = 0; with open(fileName) as fid: for line in fid: counter += 1 return counter def calculateLinesCount(): linesCount = 0 fileName = input("Enter a file name: ") while len(fileName) > 0: linesCount += workOnFile(fileName) fileName = input("Enter a file name: ") return linesCount
def main(): print('Total lines count is ' + str(calculateLinesCount())) if __name__ == "__main__": main()
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment