--- a/src/eric7/SystemUtilities/FileSystemUtilities.py Fri Dec 23 11:37:49 2022 +0100 +++ b/src/eric7/SystemUtilities/FileSystemUtilities.py Sat Dec 24 17:31:46 2022 +0100 @@ -462,41 +462,42 @@ ".ropeproject", ".eric7project", ".jedi", + "__pycache__", ] if ignore is not None: ignoreList.extend(ignore) - # TODO: replace os.listdir() with os.scandir() - try: - entries = os.listdir(path) - for entry in entries: + with contextlib.suppress(OSError, UnicodeDecodeError), os.scandir( + path + ) as dirEntriesIterator: + for dirEntry in dirEntriesIterator: if checkStop and checkStop(): break - if entry in ignoreList: + if dirEntry.name in ignoreList: continue - fentry = os.path.join(path, entry) if ( pattern - and not os.path.isdir(fentry) - and not any(fnmatch.fnmatch(entry, p) for p in patterns) + and not dirEntry.is_dir() + and not any(fnmatch.fnmatch(dirEntry.name, p) for p in patterns) ): # entry doesn't fit the given pattern continue - if os.path.isdir(fentry): - if os.path.islink(fentry) and not followsymlinks: + if dirEntry.is_dir(): + if dirEntry.is_symlink() and not followsymlinks: continue files += direntries( - fentry, filesonly, pattern, followsymlinks, checkStop + dirEntry.path, + filesonly=filesonly, + pattern=pattern, + followsymlinks=followsymlinks, + checkStop=checkStop, + ignore=ignore, ) else: - files.append(fentry) - except OSError: - pass - except UnicodeDecodeError: - pass + files.append(dirEntry.path) return files @@ -508,31 +509,21 @@ @param excludeDirs basename of directories to ignore @return list of all directories found """ - # TODO: replace os.listdir() with os.scandir() try: - names = os.listdir(path) + dirs = [] + with os.scandir(path) as dirEntriesIterator: + for dirEntry in dirEntriesIterator: + if ( + dirEntry.is_dir() + and not dirEntry.is_symlink() + and dirEntry.name not in excludeDirs + ): + dirs.append(dirEntry.path) + dirs.extend(getDirs(dirEntry.path, excludeDirs)) + return dirs except OSError: return [] - dirs = [] - for name in names: - if os.path.isdir(os.path.join(path, name)) and not os.path.islink( - os.path.join(path, name) - ): - exclude = 0 - for e in excludeDirs: - if name.split(os.sep, 1)[0] == e: - exclude = 1 - break - if not exclude: - dirs.append(os.path.join(path, name)) - - for name in dirs[:]: - if not os.path.islink(name): - dirs += getDirs(name, excludeDirs) - - return dirs - def findVolume(volumeName, findAll=False): """