All pastes #386915 Raw Edit

Someone

public text v1 · immutable
·published 2007-03-08 23:34 UTC
rendered paste body
def walk_tree(top = ".", depth_first = True):
	"""Walk the directory tree, starting from top. Credit to Noah Spurrier and Doug Fort."""
	
	names = os.listdir(os.path.join(top))
	
	if not depth_first:
		yield top, names
	
	for name in names:
		try:
			file = os.path.join(top, name)
			st = os.lstat(file)
		except os.error:
			continue

		if stat.S_ISDIR(st.st_mode):
			for (newtop, children) in walk_tree(os.path.join(top, name), depth_first):
				yield newtop, children
	if depth_first:
		yield top, names

stack trace:

Traceback (most recent call last):
  File "index_music2.py", line 185, in ?
    print ''.join([     '<html>',
  File "index_music2.py", line 85, in scan_armoury
    for top, names in walk_tree(path_base):
  File "index_music2.py", line 41, in walk_tree
    for (newtop, children) in walk_tree(os.path.join(top, name), depth_first):
  File "index_music2.py", line 35, in walk_tree
    file = os.path.join(top, name)
  File "/usr/local/lib/python2.4/posixpath.py", line 65, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 7: ordinal not in range(128)