print(f"The word '{word}' appears {count} times in the file '{filename}'.")
I added utf-8 first since it's the most common encoding. otherwise I got an error UnicodeDecodeError caused by a mismatch between the encoding used by the book.txt file and the default encoding used by Python
Thank you so much PhilW. I was having this problem from so many days, and i could not understand, what the error could be, because during the training, i did not find anything like this.
This is my code :
filename = "book.txt"
word = input("Enter the word to count: ")
# Use the correct encoding (utf-8 is common)
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
words = content.split()
count = words.count(word)
print(f"The word '{word}' appears {count} times in the file '{filename}'.")
I added utf-8 first since it's the most common encoding. otherwise I got an error UnicodeDecodeError caused by a mismatch between the encoding used by the book.txt file and the default encoding used by Python
Thank you so much PhilW. I was having this problem from so many days, and i could not understand, what the error could be, because during the training, i did not find anything like this.