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
If you have more complex requirements than just the top directory (eg ignore VCS dirs etc), you can also modify the list of directories to prevent os.walk recursing through them.
ie:
Note - be careful to mutate the list, rather than just rebind it. Obviously os.walk doesn't know about the external rebinding.
Use the
walklevel
function.It works just like
os.walk
, but you can pass it alevel
parameter that indicates how deep the recursion will go.There is a catch when using listdir. The os.path.isdir(identifier) must be an absolute path. To pick subdirectories you do:
The alternative is to change to the directory to do the testing without the os.path.join().
You could use
os.listdir()
which returns a list of names (for both files and directories) in a given directory. If you need to distinguish between files and directories, callos.stat()
on each name.Felt like throwing my 2 pence in.