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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started