what could cause this error : FileNotFoundError: [Errno 2] No such file or directory

what could cause this error : FileNotFoundError: [Errno 2] No such file or directory

我对编码还很陌生 Python 所以我对这个错误感到很困惑。 这是我的练习代码,我需要在包含多个文件的目录中找到最常用的词:

import pathlib

directory = pathlib.Path('/Users/karl/files/Code')

stats ={}

for path in directory.iterdir():
    file = open(str(path))
    text = file.read().lower()

    punctuation  = (";", ".")
    for mark in punctuation:
        text = text.replace(mark, "")


    for word in text.split:
        if word in stats:

            stats[word] = stats[word] + 1
        else:
            stats[word] = 1

most_used_word = None
score_max = 0
for word, score in stats.items():
    if score > score_max:
        score_max = score
        most_used_word = word

print(word,"The most used word is : ", score_max) 

这是我得到的错误:

 for path in directory.iterdir():
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1113, in iterdir
    for name in self._accessor.listdir(self):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/k/files/Code/exo'

什么会导致这种错误?

here's what i guet

for path in directory.iterdir():
 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1113, in iterdir
   for name in self._accessor.listdir(self):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/k/files/Code/exo'

what could cause this kind of error ?

此错误最可能的原因是没有这样的文件或目录,即文件或目录 /Users/k/files/Code/exo 不存在。