85

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.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
user1834048
  • 987
  • 1
  • 7
  • 8

5 Answers5

144

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.

GKFX
  • 1,386
  • 1
  • 11
  • 30
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    `os.path.isdir` is returning true for me on a symlink. – Kaz Apr 21 '17 at 23:31
  • I am getting `os.path.isdir('.ipynb_checkpoints')` returning true – Little Bobby Tables May 09 '17 at 13:31
  • In Python 2.7 os.path.isdir returns true for symlinks to directories and false for symlinks to files (as expected). However this can cause issues with os.rmdir because it will fail on a symlink even if it is a symlink to a directory. – Halsafar Jun 04 '19 at 21:49
6

Just like it sounds like: if the path exists, but is a file and not a directory, isdir will return False. Meanwhile, exists will return True in both cases.

Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61
4

os.path.isdir() checks if the path exists and is a directory and returns TRUE for the case.

Similarly, os.path.isfile() checks if the path exists and is a file and returns TRUE for the case.

And, os.path.exists() checks if the path exists and doesn’t care if the path points to a file or a directory and returns TRUE in either of the cases.

Manoz
  • 41
  • 2
0

Most of the time, it is the same.

But, path can exist physically whereas path.exists() returns False. This is the case if os.stat() returns False for this file.

If path exists physically, then path.isdir() will always return True. This does not depend on platform.

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • Not clear what that last paragraph means. If a directory exists physically on a remote server, but I have no network connection where I'm running the script, then surely `isdir()` can't know whether the directory exists. – LarsH Feb 04 '14 at 16:53
  • If you are checking if a directory exists on a UNC path, os.path.exists can return false when it actually exists (even with the connection working), but os.path.isdir will return the correct value. For a file in the simlar UNC path case you should also use os.path.isfile instead of os.path.exists for the same reason. – miigotu Mar 10 '17 at 02:42
0

os.path.exists(path) Returns True if path refers to an existing path. An existing path can be regular files (http://en.wikipedia.org/wiki/Unix_file_types#Regular_file), but also special files (e.g. a directory). So in essence this function returns true if the path provided exists in the filesystem in whatever form (notwithstanding a few exceptions such as broken symlinks).

os.path.isdir(path) in turn will only return true when the path points to a directory

Jan Kunigk
  • 11
  • 1
  • 1
    This doesn't really answer the OP's question, who is asking for differences between the two commands. You have only detailed one. – esqew Jul 16 '14 at 20:28