I wish I could explain my problem better, but I simply don't know why my code is not working. I suspect it has to do with scoping, but my searches haven't availed me of a clear solution. I'm generally a Javascript/Java programmer, so perhaps you can understand my confusion.
def ls(directory, times=None):
if times is None:
times = 0
for root, dirs, files in os.walk(directory):
for dirname in dirs:
print(' ' * times + dirname)
ls(dirname, times + 1)
for filename in files:
print(' ' * times + filename)
Which should ideally print any given directory structure like so:
a
b
c
d
e
However, the space indents aren't included because for some reason, subsequent recursive calls to ls() show that the variable times is reset to 0 in the for loop for os.walk(directory). Why is that variable being "reset"?