Skip to content
Advertisement

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

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement