0

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:

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) 

Here's the error I get:

 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?

bouteillebleu
  • 2,456
  • 23
  • 32
karl97100
  • 5
  • 1
  • 3
  • I notice that your code says `'/Users/karl/files/Code'` but the error says `''/Users/k/files/Code/exo'` (k not karl). It seems like the posted code did not generate the error. If your real code uses that second directory and it doesn't exist, you'd get this error. – tdelaney May 01 '20 at 05:29

1 Answers1

0

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 ?

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.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653