Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 556
user1834048 Avatar asked Feb 25 '13 22:02

user1834048


People also ask

What is os path Isdir?

path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows symbolic link, that means if the specified path is a symbolic link pointing to a directory then the method will return True. Syntax: os.path.isdir(path)

What does os path exists do?

path. exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not.

What does os path exists return?

os. The path. exist() function is a built-in function provided by the os module. This function takes the path file and returns 'true' if the file is present.

What is the use of os path Dirname?

path. dirname() method in Python is used to get the directory name from the specified path.


1 Answers

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.

like image 128
Pavel Anossov Avatar answered Sep 23 '22 14:09

Pavel Anossov