[python] 파일에서 특정 단어의 개수 세기
def count_word_occurrences(file_path, target_word):
    try:
        with open(file_path, 'r') as file:
            text = file.read()
            occurrences = text.count(target_word)
            return occurrences
    except FileNotFoundError:
        print(f"File '{file_path}' not found.")
        return -1

file_path = 'example.txt'
target_word = 'python'
result = count_word_occurrences(file_path, target_word)
print(f"The word '{target_word}' occurs {result} times in the file.")

위 코드에서 count_word_occurrences 함수는 파일 경로와 찾고자 하는 단어를 인자로 받아 해당 파일에서 단어의 출현 횟수를 반환합니다. 코드를 실행하면 지정된 파일에서 특정 단어가 나타나는 횟수를 확인할 수 있습니다.

이 예제는 파일에서 단어의 출현 횟수를 세는 방법을 보여주며, 실제 적용 시 파일이나 단어에 따라 세부적으로 코드를 조정할 수 있습니다.