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().
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()

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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS