问题
I just asked a similar question, however this is a bit different.
This time I'm trying to basically create a nested file tree structure in a list. Lets say I have this list:
files = [
'user/hey.jpg',
'user/folder1/1.txt',
'user/folder1/folder2/random.txt,'
'user/folder1/blah.txt',
'user/folder3/folder4/folder5/1.txt',
'user/folder3/folder4/folder5/3.txt',
'user/folder3/folder4/folder5/2.txt',
'user/1.jpg'
]
Im looking to get this output (not concerned about the order), I may not have formatted this list correctly but here is a general concept:
['user'['1.jpg','hey.jpg','folder1'['1.txt','blah.txt','folder2'['random.txt']],'folder3'['folder4'['folder5'['1.txt','2.txt','3.txt']]]]]
I'd like to essentially make a list which represents a file tree. For example:
user
-folder1
--folder2
---random.txt
--1.txt
--blah.txt
-folder3
--folder4
---folder5
----1.txt
----2.txt
----3.txt
-1.jpg
-hey.jpg
All help is greatly appreciated!
回答1:
The os.walk builtin does what you want. You can play around with the output yourself to get it into the format you want.
回答2:
Resmar was correct, but I'll give some more detail as to how this was done. I asked a similar question after receiving his response and was given the correct answer here: https://stackoverflow.com/a/42031496/4043494
I'll post the actual code when I'm not on mobile.
来源:https://stackoverflow.com/questions/42017560/how-would-i-nest-file-path-strings-into-a-list-based-upon-matching-folder-paths