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
.
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)
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.
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.
path. dirname() method in Python is used to get the directory name from the specified path.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With