Why is this recursive function returning more than 1 dictionary aka printing them each time it cycles why isn’t it adding new findings to the existing dictionary.
And no i dont want to use os.walk and yes i am aware theres no return
def retFiles(dir):
data = {}
root = set()
os.chdir(dir)
cwd = os.getcwd()
for i in os.listdir(cwd):
if os.path.isfile(i):
data.setdefault(i, set())
root.add(os.path.relpath(dir).replace("\\", "/"))
data[i] = root
else:
retFiles(i)
print(data)
Either pass your dictionnary as a parameter to keep the reference to the same dictionary, or use
returnto use the dictionary resulting of the previous call.Either ways, your recursive function, as all recursive calls, need to work on the same data, otherwise it’s useless.