The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 Os.Path.Isdir() Returns False On Unaccessible, But Existing Directory
00:31 Answer 1 Score 2
00:55 Accepted Answer Score 3
01:11 Answer 3 Score 2
01:21 Thank you

--

Full question
https://stackoverflow.com/questions/4666...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #unix

#avk47



ACCEPTED ANSWER

Score 3


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).




ANSWER 2

Score 2


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.




ANSWER 3

Score 2


You could implement a try/except block:

import os

path = '/foo/bar'

if os.path.exists(path):
    try:
        os.chdir(path)
    except PermissionError:
        print ("Access Denied To:", path)