Sean Jean
Sean Jean

Reputation: 27

Python Word Frequency

I am messing around with Python trying to write a program that inputs a list of words from a text file and outputs the number of times each word appears. I am very close but for some reason cannot get this program to run. I am utilizing a counter and a dictionary to count the numbers of appearances for each word and storing them as the values and the keys

Can anyone spot where my program is getting held up? The program doesn't run it freezes and then after I click enter it says:

./wordFrequency.py: line 3: syntax error near unexpected token `('
./wordFrequency.py: line 3: `def main():'

I am very confused. Code below:

import collections

def main():
    mylist = open("C:/path/files/csc223/assignment1/words.dat") as f:
        lines = f.read().splitlines()
    mycount = []
    for i in range(len(mylist)):
        mycount.append(myinp.count(mylist[i]))

    mydict = dict(zip(mylist, mycount))
    print (mydict)

main()

Upvotes: 0

Views: 288

Answers (1)

u354356007
u354356007

Reputation: 3215

Looks like you're calling bash interpreter for your script. Try using python wordFrequency.py instead of ./wordFrequency.py.

Upvotes: 2

Related Questions