JohnDoeTheOne
JohnDoeTheOne

Reputation: 151

Get file(name) in the dictionary as key and all directories(if file is in) as the value

I have directories and subdirectories, I want to get all the files in the dictionary and also directories into dictionary... Like this:

{'randomfile': '.\map\randomdirectory' , '\map\map2' , \somerandom\map\map3'}

Where key is file(name) and all the directories where this file exsist are in value.

I have my project saved in specific map -> there are also those maps which I want to search for files and folders.., lets say I want to search just the maps where I have saved my project.. How do I do it, .. I know that I do it with recursion but it gets tricky.

And yeah,.. I cant use os.walk.

Thanks for potential answer.

Upvotes: 0

Views: 1720

Answers (1)

kev
kev

Reputation: 161954

$ python3
>>> import os
>>> d = {}
>>> for x,y,z in os.walk('/path/to/dir'):
...   for f in z:
...     if f not in d:
...        d[f] = []
...     d[f].append(x)
...

Upvotes: 1

Related Questions