I've been getting some very inconsistent results while using os.walk. My aim is to use python to programmatically locate another python file on my system. This works fine when os.walk doesn't have much work to do and is only searching one or two folders:
import os
from os.path import join
lookfor = "new_id17.py"
for root, dirs, files in os.walk('L:\\HWYS\\D_GROUP6\\STAFF\\myname\\Python'):
for name in files:
print(os.path.join(root, name))
if lookfor in files:
print "found: %s" % join(root, lookfor)
break
With the above code os.walk easily finds my file in a short space of time. However, if I ask it to search for the same file at a higher point in the folder directory it can't seem to find the file:
import os
from os.path import join
lookfor = "new_id17.py"
for root, dirs, files in os.walk("L:\\HWYS"):
for name in files:
print(os.path.join(root, name))
if lookfor in files:
print "found: %s" % join(root, lookfor)
break
Does anyone have any idea where I'm going wrong? Are there any known issues with os.walk? Or is it just not very good at searching a large number of folders and files.