Skip to content

Instantly share code, notes, and snippets.

@lene
Last active January 30, 2018 14:41
Show Gist options
  • Save lene/04768a4a70d8123b6b0eaf9350345382 to your computer and use it in GitHub Desktop.
Save lene/04768a4a70d8123b6b0eaf9350345382 to your computer and use it in GitHub Desktop.
Emulating UNIX command find(1) in Python
def find_files(base_path, condition):
return [
os.path.join(root, file)
for root, _, files in os.walk(base_path)
for file in files
if os.path.exists(os.path.join(root, file)) # probably redundant, just making sure
if condition(os.path.join(root, file))
]
# e.g. find -f -ctime -N
find_files(base_path, lambda file: time() - os.path.getctime(file) < 24*60*60*N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment