DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

List All Files In A Special Directory (including Sub-directories)

05.17.2009
| 7488 views |
  • submit to reddit
        this function can return all files name (including those ones that are in subdirectories) as a list object.

import os

def hlist(path_to):
	result=[]
	temp=os.listdir(path_to)
	for a in temp:
		try:
			if os.path.isfile(path_to+'\\'+a):
				result.append(path_to+'\\'+a)
			if os.path.isdir(path_to+'\\'+a):
				result.extend(hlist(path_to+'\\'+a))
		except:
			pass
	return(result)


for example:
a=hlist('d:')
b=hlist('f:\\photos\\')

but i donno why it doesnt work on my c: !! 

any suggestion?