68 473
Assignments Done
98,8%
Successfully Done
In December 2018

Answer to Question #52370 in Python for Oz

Question #52370
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().
Expert's answer
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions