面试题答案
一键面试def count_specific_word(file_path, target_word):
try:
word_count = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
words = line.strip().split()
for word in words:
if word.lower() == target_word.lower():
word_count += 1
return word_count
except FileNotFoundError:
print(f"文件 '{file_path}' 未找到。")
return 0
# 示例用法
file_path = 'your_file.txt'
target_word = 'example'
count = count_specific_word(file_path, target_word)
print(f"单词 '{target_word}' 出现的次数为: {count}")