Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Last active July 14, 2016 00:38
Show Gist options
  • Save dmyersturnbull/aae128f884bb6e5517cd13f5d23153a0 to your computer and use it in GitHub Desktop.
Save dmyersturnbull/aae128f884bb6e5517cd13f5d23153a0 to your computer and use it in GitHub Desktop.
List the full path of every meaningful file in a directory recursively.
import os
from typing import Iterator
def is_proper_file(path: str) -> bool:
name = os.path.split(path)[1]
return len(name) > 0 and name[0] not in {'.', '~', '_'}
def scantree(path: str, follow_symlinks: bool=False) -> Iterator[str]:
"""List the full path of every file not beginning with '.', '~', or '_' in a directory, recursively."""
for entry in os.scandir(path):
if entry.is_dir(follow_symlinks=follow_symlinks):
yield from scantree(entry.path)
elif is_proper_file(entry.path):
yield entry.path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment