我如何根据 Python 中匹配的文件夹路径将文件路径字符串嵌套到列表中 [英] How would I nest file path strings into a list based upon matching folder paths in Python
问题描述
我刚刚问了一个类似的问题,但是这有点不同.
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
非常感谢所有帮助!
推荐答案
Resmar 是正确的,但我会更详细地说明这是如何完成的.收到他的回复后,我问了一个类似的问题,并在这里得到了正确的答案:https://stackoverflow.com/a/42031496/4043494
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.
这篇关于我如何根据 Python 中匹配的文件夹路径将文件路径字符串嵌套到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!