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
Since Python 3.5 you can use os.scandir instead of os.listdir. Instead of strings you get an iterator of DirEntry objects in return. From the docs:
Using
scandir()instead oflistdir()can significantly increase the performance of code that also needs file type or file attribute information, becauseDirEntryobjects expose this information if the operating system provides it when scanning a directory. AllDirEntrymethods may perform a system call, butis_dir()andis_file()usually only require a system call for symbolic links;DirEntry.stat()always requires a system call on Unix but only requires one for symbolic links on Windows.
You can access the name of the object via DirEntry.name which is then equivalent to the output of os.listdir