Question: I'm trying to make a program file_stats.py that, when run on the command line, accepts a text file name as an argument and outputs the

I'm trying to make a program "file_stats.py" that, when run on the command line, accepts a text file name as an argument and outputs the number of characters(e.g., size of the file), words, lines, and the length (in characters) of the longest line in the file

$python file_stats.py beautiful_soup_by_Lewis_Carroll.txt Characters: 553 Words: 81 Lines: 21 Longest line: 38 $ python file_stats.py books.csv Characters: 557 Words: 45 Lines: 6 Longest line: 120 

The program should have a function stats that takes the filename as an input and returns the statistics (in the same order) as a tuple, so it can also be used in the REPL

 >>> from file_stats import stats >>> c, w, l, ll = stats('beautiful_soup_by_Lewis_Carroll.txt') >>> print(f"characters: {c}, words: {w}, lines: {l}, longest: {ll}") characters: 553, words: 81, lines: 21, longest: 38 

What I have so far:

import sys

def stats(filename):

# Do the calculations and return the data

pass

if __name__ == '__main__':

# call stats(filename) and print the results shown in instructions.

pass

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!