Hi all!
During the course of my wordcount exercise, I was left with a couple questions. My first question is which file am I supposed to be converting, and how exactly do I call this file to read it. I have tried every different way of calling the file that I can think of as well.
I attempted to run the solution, and that didn't produce any results for me either. I think I am missing something here. I have included my code below.
Thanks in advance for all of your help!
Bleys
import sys
# +++your code here+++
def print_words(filename):
file=open(r'C;\users\bfocht\bleys\python\google\basic\alice.txt', 'r+')
wordcount={}
#word=str.split()
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word]+=1
return file
sys.exit(0)
I have only worked on the above function.
# Define print_words(filename) and print_top(filename) functions.
# You could write a helper utility function that reads a file
# and builds and returns a word/count dict for it.
# Then print_words() and print_top() can just call the utility function.
###
# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
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]
if option == '--count':
print_words(filename)
elif option == '--topcount':
print_top(filename)
else:
print 'unknown option: ' + option
sys.exit(1)
if __name__ == '__main__':
main()