I'm messing around with file lookups in python on a large hard disk. I've been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size directories).
Has anyone got any experience with them both and could say which is more efficient? As I say, glob seems to be slower, but you can use wildcards etc, were as with walk, you have to filter results. Here is an example of looking up core dumps.
core = re.compile(r"core\.\d*")
for root, dirs, files in os.walk("/path/to/dir/")
for file in files:
if core.search(file):
path = os.path.join(root,file)
print "Deleting: " + path
os.remove(path)
Or
for file in iglob("/path/to/dir/core.*")
print "Deleting: " + file
os.remove(file)
os.listdir
andos.isdir
, so my gut tells me you won't gain much one way or the other. (However, as pointed out in two of the answers below, theos.walk
recurses over subdirectories andglob.iglob
doesn't, so it doesn't make sense to compare). If you do end up with a performance issue, profile a couple of approaches. Otherwise, just write clear code. – Herzig