Sunday, May 21, 2006

os.listdir iterator

I found an iterator version of os.listdir for POSIX(?) systems, but I needed one for Windows. Specifically to check if a directory has subdirectories.

import win32file
import pywintypes
def has_subdirectories(path):
try:
for info in win32file.FindFilesIterator(os.path.join(path, "*")):
if info[0] & win32file.FILE_ATTRIBUTE_DIRECTORY and info[-2] not in [".", ".."]:
return True
except pywintypes.error: # access denied
pass
return False

No comments: