Jellicle
Jellicle

Reputation: 30206

Python os.path.isdir is True but os.listdir yields "No such file or directory"

My python script raises an error on os.listdir but os.path.isdir indicates that the target location is indeed a path:

import os
path = '/Volumes/WD Elements/photofix/organized-photos/2003'
os.path.isdir(path) # => True
os.listdir(path) # => OSError: [Errno 2] No such file or directory: '/Volumes/WD Elements/photofix/organized-photos/2003'

When I ls the path on bash, the command succeeds without error:

ls '/Volumes/WD Elements/photofix/organized-photos/2003'
echo $? # => 0
ls -ld '/Volumes/WD Elements/photofix/organized-photos/2003'
# => drwxr-xr-x  29 JellicleCat  staff  986 Apr  4 22:19 /Volumes/WD Elements/photofix/organized-photos/2003

Is this indicative of a Python bug? A hardware failure? An OS bug?

Python 2.7.16 on MacOS High Sierra

Upvotes: 1

Views: 637

Answers (1)

RAM
RAM

Reputation: 2749

I've stumbled upon the exact same problem, whith triggered the following error message:

[ERROR]FileNotFoundError raised while getting the package builder: [WinError 3] The system cannot find the path specified:

The following script is a minimal working example that reproduced the error.

import os

some_dir = "C:\<WHATEVER PATH IS CAUSING YOU GRIEF>"
some_dir = os.path.normpath(some_dir)
print(f'some_dir: {some_dir}')
if os.path.exists(some_dir):
    some_dir_files = os.listdir(some_dir)
    print(f"files: {','.join(some_dir_files)}")

After some digging, I've noticed that a) some_dir was pointing to a symlink to a dir, b) the symlink was missing some permissions.

Upvotes: 1

Related Questions