I am convinced it is something simply syntactic - I however can not figure out why my code:
import os
from collections import Counter
d = {}
for filename in os.listdir('testfilefolder'):
f = open(filename,'r')
d = (f.read()).lower()
freqs = Counter(d)
print(freqs)
will not work - it apparently can see in to the 'testfilefolder' folder and tell me that the the file is there i.e. an error message 'file2.txt' is not found. So it can find it to tell me that it is not found...
I however get this piece of code to work:
from collections import Counter
d = {}
f = open("testfilefolder/file2.txt",'r')
d = (f.read()).lower()
freqs = Counter(d)
print(freqs)
Bonus - is this a good way of doing what I am trying to do (read from file and count the frequencies of words)? This is my first day with Python (although I have some amounts of programming exp.)
I have to say that I am liking Python!
Thanks,
Brian
Change:
f = open(filename,'r')
To:
f = open(os.path.join('testfilefolder',filename),'r')
Which is effectively what you are doing in:
f = open("testfilefolder/file2.txt",'r')
Reason: you are listing the files in 'testfilefolder' (a subdirectory of your current directory) but then trying to open the file in your current directory.