Use pathlib
If you want all directory entries, including symbolic links, etc remove the conditional for dirs
and eliminate the file search but then you will have to look at individual entries to get type.
If you want all directory entries, including symbolic links, etc remove the conditional for dirs
and eliminate the file search but then you will have to look at individual entries to get type.
from pathlib import Path def plib_walk(dir): dirs = [x for x in dir.iterdir() if x.is_dir()] files = [x for x in dir.iterdir() if x.is_file()] print (f'\nDirectory: {dir.resolve()}') for file in files: print(f' file: {file}') for pdir in dirs: plib_walk(pdir) def tryit(): home = Path('.') # Start a level higher to make it interesting start = home / '..' print(f'starting with: {start.resolve()}') plib_walk(start) if __name__ == '__main__': tryit()If you want to open files to search within, use:
with file.open() as f: read(f)