我对编码和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'
是什么原因导致了这种错误?
以下是我的
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'
是什么导致了这种错误?
此错误最可能的原因是没有这样的文件或目录,即文件或目录/Users/k/files/Code/exo
不存在。