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.
python - pros and cons between os.path.exists vs os.path.isdir - Stack Overflow
os.path.isdir is always returning false
What is fast way to check 'isDir' in python? - Stack Overflow
`os.path.isdir()` devuelve falso
Videos
Im creating a script to convert multiple image files in folder A and saving to folder B. But if folder B doesn't exist then i need to creat a folder. In searching for solutions i came across two ways to check if a folder exists, being from os library os.path.isdir and from pathlib Path.isdir. Whats the difference. They both seem to do the same thing.
Which bring me to another question, is the .isdir the same method being applied to two different functions in two different libraries? How do we the determine which library would be best for whatever problem is being solved.
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.
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.
os.path.isdir seems to be the fastest implementation:
from pathlib import Path
import os
import win32api
import glob
import timeit
def os_isDir(files):
return [os.path.isdir(fullPath) for fullPath in files]
def os_NOT_isFile(files):
return [not os.path.isfile(fullPath) for fullPath in files]
def pathlib_isDir(files):
return [Path(fullPath).is_dir() for fullPath in files]
def winapi_GetFileAttributesA(files):
return [win32api.GetFileAttributes(fullPath) == 16 for fullPath in files]
files = glob.glob('C:\\Windows\\System32\\*')
for func in ("os_isDir", "os_NOT_isFile", "pathlib_isDir", "winapi_GetFileAttributesA"):
t1 = timeit.Timer(f"{func}({files})", f"from __main__ import {func}")
print(f"Evaluation {len(files)} paths using {func} took: {t1.timeit(number=5)} seconds\n")
Out (on a Win7 VM):
Evaluation 3294 paths using os_isDir took: 2.1250754 seconds
Evaluation 3294 paths using os_NOT_isFile took: 2.7074766999999995 seconds
Evaluation 3294 paths using pathlib_isDir took: 3.4624453000000006 seconds
Evaluation 3294 paths using winapi_GetFileAttributesA took: 2.2789655999999994 seconds
Perhaps the fastest way to check if something is a directory is to not have to check at all. If you wish to iterate all files in a directory tree (you haven't actually specified whether this is the case), you should consider using os.walk:
os.walk(top, topdown=True, onerror=None, followlinks=False)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
The 3rd element of the tuple (filenames) is guaranteed to not be a directory. This is how you would print all file names in a directory tree:
import os
def walk_directory(directory_name):
for dirpath, dirnames, filenames in os.walk(directory_name):
for filename in filenames:
fullpathname = os.path.join(dirpath, filename)
print(fullpathname)
Otherwise, you should be using an isDir method call. For example, if you just wanted to iterate all the files in a single directory, then one way is as follows:
from pathlib import Path
def list_directory(directory_name):
# non-recursive
for p in Path(directory_name).glob('*'):
if not p.is_dir():
#filename = str(p) # to convert Path to a string
print(p)