DIGIT-ARC read-only archive · snapshot: July 23, 2026

Python os.scandir() returns file or directory not found

1 post · originally from https://geek.digit.in/community/threads/python-os-scandir-returns-file-or-directory-not-found.211409/

P
paulspavan Original post #1
Jul 16, 2022 at 6:06 PM
I am developing a flask app that scans a directory on local disk and performs some file operations. It works very well in Windows, but when I deploy to DigitalOcean (Linux) it gives the following error:

for file in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/my/directory/path'

The directory created in my Windows is:

D:\my\directory\path

So when I use the UNIX path for it /my/directory/path because my project is on my D disk so it considers it as root, it works perfectly.

On Linux, I literally just created the same path /my/directory/path, and it says directory not found.

Here is my python code:

def get_files(path):
print("CHECKING THE PATH: ", path)
for file in os.scandir(path):
if os.path.isfile(os.path.join(path, file)):
print("FILE YIELDED IS: ", file)
yield file