I’m very new to coding and Python so I’m really confused by this error. Here’s my code from an exercise where I need to find the most used word into a directory with multiples files:
JavaScript
x
31
31
1
import pathlib
2
3
directory = pathlib.Path('/Users/karl/files/Code')
4
5
stats ={}
6
7
for path in directory.iterdir():
8
file = open(str(path))
9
text = file.read().lower()
10
11
punctuation = (";", ".")
12
for mark in punctuation:
13
text = text.replace(mark, "")
14
15
16
for word in text.split:
17
if word in stats:
18
19
stats[word] = stats[word] + 1
20
else:
21
stats[word] = 1
22
23
most_used_word = None
24
score_max = 0
25
for word, score in stats.items():
26
if score > score_max:
27
score_max = score
28
most_used_word = word
29
30
print(word,"The most used word is : ", score_max)
31
Here’s the error I get:
JavaScript
1
5
1
for path in directory.iterdir():
2
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1113, in iterdir
3
for name in self._accessor.listdir(self):
4
FileNotFoundError: [Errno 2] No such file or directory: '/Users/k/files/Code/exo'
5
What could cause this kind of error?
Advertisement
Answer
here’s what i guet
JavaScript151for path in directory.iterdir():
2File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1113, in iterdir
3for name in self._accessor.listdir(self):
4FileNotFoundError: [Errno 2] No such file or directory: '/Users/k/files/Code/exo'
5
what could cause this kind of error ?
The most likely cause for this error is that there is no such file or directory, i.e. that the file or directory /Users/k/files/Code/exo
does not exist.