Python 3.6 Problem 2. Write a function named file_stats that takes one string pa
ID: 3728671 • Letter: P
Question
Python 3.6
Problem 2. Write a function named file_stats that takes one string parameter (in file) that is the name of an existing text file. The function file_stats should calculate three statistics about in_file: the number of lines it contains, the number of words and the number of characters, and print the three statistics on separate lines. For example, the following would be be correct input and output. (Hint: the number of characters may vary depending on what platform you are working.) >>>file stats('created egual.txt) lines 2 words 1.3 characters 72Explanation / Answer
#code.py def file_stats(fileName): chars = 0 words = 0 lines = 0 try: f = open(fileName) print(" The content of file was") for x in f: lines += 1 words += len(x.split()) chars += len(x) f.close() print("lines",lines) print("words",words) print("characters",chars) except FileNotFoundError: print(" File",fileName,"could not found. Try again.") except Exception: print("Exception..")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.