Raw new at this. Simply trying to run Google's solution to 'wordcount.py'

828 views
Skip to first unread message

Stuart

unread,
Feb 2, 2014, 12:40:04 PM2/2/14
to python-g...@googlegroups.com
Working my way through python 101.  I am attempting to merely run Google's solution to python exercise "wordcount".  What arguments is the command line expecting?  I'm in Powershell, in the right directory, typed in "python wordcount.py bone_number.txt" (which is the txt file I want the code to work on).  Always the same message: " usage: ./wordcount.py {--count | --topcount} file
Exit code:  1"  - - - which I understand is trying to guide me to the light.  Can anyone please further enlighten me?  And please don't feel awkward about 'dumbing down' your answer.





testwordcount.py

Robert Mandić

unread,
Feb 2, 2014, 1:45:50 PM2/2/14
to python-g...@googlegroups.com
python wordcount.py --count bone_number.txt
python wordcount.py --topcount bone_number.txt




--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Lp, Robert

Stuart

unread,
Feb 2, 2014, 4:40:06 PM2/2/14
to python-g...@googlegroups.com
Thank you for responding.  I typed :  python wordcount.py --count bone_number.txt     but this is the error msg I got on Powershell.  The rest is just proving to myself  that yeah, I'm where I think I'm supposed to be:   

 PS C:\Documents and Settings\Rick\mystuff\google-python-exercises\basic\solution> dir


    Directory: C:\Documents and Settings\Rick\mystuff\google-python-exercises\basic\solution


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          2/2/2014  10:13 AM        436 bone_number.txt
-a---          2/2/2014   2:55 PM        991 directory_solution.txt
-a---         1/18/2014   5:43 PM       3656 list1.py
-a---         1/18/2014   5:43 PM       2774 list2.py
-a---         1/18/2014   5:43 PM       2995 mimic.py
-a---         1/18/2014   5:43 PM       4081 string1.py
-a---         1/18/2014   5:43 PM       3094 string2.py
-a---          2/2/2014  10:17 AM       3660 testwordcount.py
-a---          2/2/2014  10:18 AM       3163 testwordcount.pyc
-a---          2/2/2014  11:27 AM       3554 testwordcount1.py
-a---          2/2/2014   2:37 PM       3557 wordcount.py
-a---          2/1/2014   7:22 PM       3016 wordcount.pyc


PS C:\Documents and Settings\Rick\mystuff\google-python-exercises\basic\solution> python wordcount.py --count bone_numbe
r.txt
Traceback (most recent call last):
  File "wordcount.py", line 94, in <module>
    word_count_dict(filename)
NameError: name 'filename' is not defined
PS C:\Documents and Settings\Rick\mystuff\google-python-exercises\basic\solution>

Robert Mandić

unread,
Feb 2, 2014, 5:33:07 PM2/2/14
to python-g...@googlegroups.com
A you haven't modified wordcount.py in this folder?
Because it should work:

$ python wordcount.py  --count /etc/hosts
# 1
127.0.0.1 1
127.0.1.1 1
192.168.0.200 1
192.168.0.250 1
...

Stuart

unread,
Feb 2, 2014, 6:26:23 PM2/2/14
to python-g...@googlegroups.com
Works  now.  I'm too new to programming to know why.  I added the line:  filename = 'bone_number.txt'   before the first definition and that did the trick.  I was trying to import the txt file as an argument in PS.    I still don't know what "--count" or "--topcount" means in this context in the command line.  Looks like this is going to keep me busy for a while.  Thank you for your assistance.

Robert Mandić

unread,
Feb 3, 2014, 3:26:02 AM2/3/14
to python-g...@googlegroups.com
Maybe I can explain.
When you call your program, it receives command line arguments stored in a list named sys.argv 
You can try importing sys module and just print it out to test it.

sys.argv[0] is always the name of the program: 'wordcount.py' in this case.
sys.argv[1] is the first argument: '--count' in this case.
sys.argv[2] is the second argument: 'bone_number.txt' in this case.​

The program is checking for the first argument to decide which function it will call later. If you pass '--count' it will execute one function, but if you pass '--topcount' it will call another function.
In your case, the variable filename would be populated with value of sys.argv[2], which holds a value of 'bone_number.txt'.


Reply all
Reply to author
Forward
0 new messages