0

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!

DarkMatter
  • 323
  • 3
  • 16
  • What do you want to achieve? – user1767754 Feb 03 '17 at 18:42
  • I had tried to explain it as best as I could, but what it boils down to is this. I want the initial list of file paths to be converted into a list that has the structure of a file tree. Instead of having all of these full paths, I'd like them all to be nested based on what is contained in each folder if that makes sense. – DarkMatter Feb 03 '17 at 18:44

2 Answers2

0

The os.walk builtin does what you want. You can play around with the output yourself to get it into the format you want.

Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • Could you possibly give me a quick example? I've tried a few things but haven't successfully gotten it to work. – DarkMatter Feb 03 '17 at 15:09
0

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.

Community
  • 1
  • 1
DarkMatter
  • 323
  • 3
  • 16