눈가락
[Python] directory 내 모든 파일의 글자 빈도수 계산하기
눈가락
2021. 2. 8. 11:53
가령 /home/eye/dir 위치 내에 a.txt, b.txt, c.txt 가 있다고 하자.
위의 세 개의 파일들을 순서대로 읽고,
파일 내의 글자들의 빈도수를 계산하는 코드를 만들어본다.
(여기서 white space(공백)과 줄바꿈은 제외함)
from os import listdir
from os.path import isfile, join
mypath = "/home/eye/dir"
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
frequency = {}
for file in files:
document_text = open(file, 'r')
text_string = document_text.read()
for word in text_string:
if(word==' ' or word=='\n'):
continue
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for words in frequency_list:
print (words, frequency[words])
참고 stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory