Skip to content

Instantly share code, notes, and snippets.

@sumeet
Created July 11, 2010 17:47
Show Gist options
  • Save sumeet/471706 to your computer and use it in GitHub Desktop.
Save sumeet/471706 to your computer and use it in GitHub Desktop.
Python find
import os
import re
def find(expression, path='.', type='df'):
"""
Find files or directories.
>>> list(find(r'.*\.py$', type='f'))
['./find.py']
"""
for base, directories, files in os.walk(path):
if 'd' in type:
for directory in directories:
if re.match(expression, os.path.join(base, directory)):
yield os.path.join(base, directory)
if 'f' in type:
for file in files:
if re.match(expression, os.path.join(base, file)):
yield os.path.join(base, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment