All pastes #807097 Raw Edit

get_dirnames

public python v1 · immutable
#807097 ·published 2007-12-06 06:11 UTC
rendered paste body
from os import pathdef get_dirlist(folder, depth=0, include=None, exclude=None):	"""	Return a list of absolute paths in folder	include, exclude: a function returning a boolean	def include(filename):		return ShouldInclude	"""	from os import walk	paths = []	def include_file(file):		return (not include or include(file)) and (not exclude or not exclude(file))			for dirname, dirnames, fnames in walk(folder):		# skip deep directories		print "in", dirname		head, dp = dirname, 0		while not path.samefile(head, folder):			head, tail = path.split(head)			dp += 1		print "at depth", dp, depth		print dp > depth		if dp > depth:			print "at depth", dp, "in", dirname			print "don'tgo:", dirnames			del dirnames[:]			continue				excl_dir = []		for dir in dirnames:			if not include_file(dir):				excl_dir.append(dir)				continue			abspath = path.join(dirname, dir)			paths.append(abspath)				for file in fnames:			if not include_file(file):				continue			abspath = path.join(dirname, file)			paths.append(abspath)		for dir in reversed(excl_dir):			dirnames.remove(dir)	return paths