Is It A Directory

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Nov 29 05:54:42 CET 2009

When doing recursive directory traversal, sometimes you want to follow 
symlinks that point at other directories, and sometimes you don’t. Here’s a 
routine that you can use to check whether a path specifies a directory, with 
the option to treat a symlink to a directory as a directory, or not:

import os
import stat

def isdir(path, followsymlink) :
    """returns true iff path specifies a directory. A symlink is followed
    iff followsymlink."""                                                
    return stat.S_ISDIR((os.lstat, os.stat)[followsymlink](path).st_mode)
#end isdir                                                                   

More information about the Python-list mailing list