How do I limit os.walk
to only return files in the directory I provide it?
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
return outputList
create a list of excludes, use fnmatch to skip the directory structure and do the process
same as for 'includes':
In Python 3, I was able to do this:
Why not simply use a
range
andos.walk
combined with thezip
? Is not the best solution, but would work too.For example like this:
Works for me on python 3.
Also: A
break
is simpler too btw. (Look at the answer from @Pieter)You can use this snippet
The suggestion to use
listdir
is a good one. The direct answer to your question isroot, dirs, files = os.walk(dir_name).next()
The same idea with
listdir
, but shorter: