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
files_with_full_path = [f.path for f in os.scandir(dir) if f.is_file()]
. In case you need only the filenames usef.name
instead off.path
. This is the fastest solution and much faster than anywalk
orlistdir
, see https://mcmap.net/q/40694/-getting-a-list-of-all-subdirectories-in-the-current-directory. – Grier