Here's a function (credit to user Abbot, for providing it in another question)
def traverse(ftp):
level = {}
for entry in (path for path in ftp.nlst() if path not in ('.', '..')):
ftp.cwd(entry)
level[entry] = traverse(ftp)
ftp.cwd('..')
return level
Here's what I don't understand: When python enters the function it creates an empty dictionary (level
). In the for loop, it stores a directory name as a key in the dictionary. as for the that key's value, python enters the function again and searches for a directory and it becomes that key's value.
But, how is the level dictionary remembering the values inside? I mean, shouldn't it be reset/emptied everytime python enters the function?