Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.isdir() returns false on unaccessible, but existing directory

Tags:

python

unix

Lets say I have directories like:

foo/bar/

bar is chmod 777 and foo is 000.

When I call os.path.isdir('foo/bar') it returns just False, without any Permission Denied Exception or anything, why is it like that? Shouldn't it return True?

like image 980
Ginko Avatar asked Oct 10 '17 12:10

Ginko


2 Answers

If you are not root then you cannot access foo. Therefore you can't check if foo/bar exists and it returns False because it cannot find a directory with that name (as it cannot access the parent directory).

like image 173
mrCarnivore Avatar answered Sep 28 '22 05:09

mrCarnivore


os.path.isdir can return True or False, but cannot raise an exception.

So if the directory cannot be accessed (because parent directory doesn't have traversing rights), it returns False.

If you want an exception, try using os.chdir or os.listdir that are designed to raise exceptions.

like image 43
Jean-François Fabre Avatar answered Sep 28 '22 03:09

Jean-François Fabre