import glob, os.path
ndir = nfile = 0

def traverse(dir, depth):
(space)global ndir, nfile
(space)for obj in glob.glob(dir + ‘/*’):
(space)(space)if depth == 0:
(space)(space)(space)prefix = ‘|–‘
(space)(space)else:
(space)(space)(space)prefix = ‘|’ + ‘ ‘ * depth + ‘|–‘
(space)(space)if os.path.isdir(obj):
(space)(space)(space)ndir += 1
(space)(space)(space)print(prefix + os.path.basename(obj))
(space)(space)(space)traverse(obj, depth + 1)
(space)(space)elif os.path.isfile(obj):
(space)(space)(space)nfile += 1
(space)(space)(space)print(prefix + os.path.basename(obj))
(space)(space)else:
(space)(space)(space)print(prefix + ‘unknown object :’, obj)

if __name__ == ‘__main__’:
(space)traverse(‘.’, 0)
(space)print(‘\n’, ndir, ‘directories,’, nfile, ‘files’)

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *