Last active
August 29, 2015 13:56
-
-
Save Micka33/8939214 to your computer and use it in GitHub Desktop.
Iterate through folders and files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import os | |
def callfn(fns, name, path, dry): | |
if isinstance(fns, list): | |
for fn in fns: | |
if hasattr(fn, '__call__'): | |
fn(name, path, dry) | |
elif hasattr(fns, '__call__'): | |
fns(name, path, dry) | |
def walkThrough(folderpath, fnfile=None, fndir=None, dry=False): | |
import os | |
for dirname, dirnames, filenames in os.walk(folderpath): | |
if len(filenames) > 0: | |
callfn(fnfile, filenames, dirname, dry) | |
for dirn in dirnames: | |
walkThrough(os.path.join(dirname, dirn), fnfile, fndir, dry) | |
callfn(fndir, dirn, dirname, dry) | |
break | |
# Usage: | |
# walkThrough('./folder', filesCallback, folderCallback, shouldrundry) | |
def printit(name, path, dry=False): | |
print name | |
walkThrough('./folder', printit, None) | |
walkThrough('./folder', printit, printit) | |
walkThrough('./folder', [printit,printit], None, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment