Skip to content

Instantly share code, notes, and snippets.

@jmtd
Created October 7, 2016 15:20
Show Gist options
  • Save jmtd/f1526ff8b77a2fda78a49733005fd5c6 to your computer and use it in GitHub Desktop.
Save jmtd/f1526ff8b77a2fda78a49733005fd5c6 to your computer and use it in GitHub Desktop.
import os
import sys
def foo(dirname, dirtest, fileact):
for c in os.listdir(dirname):
cc = os.path.join(dirname, c)
if os.path.isdir(cc) and dirtest(c):
foo(cc, dirtest, fileact)
elif os.path.isfile(cc):
fileact(cc)
foo(".",
lambda x: len(x) > 0 and x[0] != '.' and "tests" != x[:len("tests")],
lambda x: sys.stdout.write("{}\n".format(x))
)
# Or, using a Functor
class WalkFunctor:
def __init__(self, dirtest, fileact):
self.dirtest = dirtest
self.fileact = fileact
def __call__(self,dirname):
for c in os.listdir(dirname):
cc = os.path.join(dirname, c)
if os.path.isdir(cc) and self.dirtest(c):
self(cc)
elif os.path.isfile(cc):
self.fileact(cc)
f = WalkFunctor(
lambda x: len(x) > 0 and x[0] != '.' and "tests" != x[:len("tests")],
lambda x: sys.stdout.write("{}\n".format(x))
)
f(".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment