[Tutor] wordcount.py provided exercise solution not working?

Mats Wichmann mats at wichmann.us
Wed Sep 20 19:08:28 EDT 2023


On 9/20/23 16:41, Matthew Borkenhagen wrote:
> Hello,
> 
> In working through Google's Python Class (
> https://developers.google.com/edu/python) I just ran the provided solution
> from Google for the exercise on dicts and files: wordcount.py
> When I run this program while in the proper directory at the command
> prompt, the program only prints *this result*:
> 
> *usage: ./wordcount.py {--count | --topcount} file*
> 
> My question is this--Is this provided solution ever reading a file?  (What
> is the filename that it should be reading called?   Where would this file
> be located?  In the directory google-python-exercises\basic\solution or
> would I need to create a file myself for it to read in order to test the
> program?)

The filename is whatever you tell it. But, apparently, you *have* to 
select one of the options too (it's not "optional").

def main():
    if len(sys.argv) != 3:
      print('usage: ./wordcount.py {--count | --topcount} file')
      sys.exit(1)

    option = sys.argv[1]
    filename = sys.argv[2]

This says: if the script is not called with three arguments, print an 
error message and quit. By convention, the name of the program you just 
ran is sys.argv[0], so you have to supply two more arguments on the 
command line.  The first will be the option you selected, and the second 
will be the filename to count the words in.




More information about the Tutor mailing list