pros and cons between os.path.exists vs os.path.isdir

user1834048 picture user1834048 · Feb 25, 2013 · Viewed 114.7k times · Source

I'm checking to see if a directory exists, but I noticed I'm using os.path.exists instead of os.path.isdir. Both work just fine, but I'm curious as to what the advantages are for using isdir instead of exists.

Answer

Pavel Anossov picture Pavel Anossov · Feb 25, 2013

os.path.exists will also return True if there's a regular file with that name.

os.path.isdir will only return True if that path exists and is a directory, or a symbolic link to a directory.