This is the code i used. Why cant os walk handle ?
import os, re
cwd = os.getcwd()
directory= 'Box II'
dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M)
for root, dirs, files in os.walk(os.path.abspath(cwd)):
if dirpattern.search(root):
match = dirpattern.search(root)
match=match.group(0)
print match #OUTPUT = D:\dir1\dir2\dir3 (the directory i searched for)
for root, dirs, files in os.walk(os.path.abspath(match)):
print files #OUTPUT = nothing
why cant i use the matched folder for a new os.walk loop?
I think you want to use a generator to yield each match so that you can walk over the things that match your expression. Here is a snippet of code that I use regularly.
You can use it like this.
Hope this helps.
It should be fairly simple for you to replace the fnmatch with re.