May 5, 2020 07:14 AM
(This post was last modified: May 5, 2020 07:23 AM by CodeAssassin.)
Hello,
After a little help or understanding (granted this is day 2 of me learning python)
Problem.
I would like to do something simple... List files and folders in a (list or dict)
I would like it as shown in the tree below after searching.
Dir-
*Subfolder-
Files-
Files-
Dir-
*SubFolder-
**File-
Dir-
*Subfolder
*Subfolder
Currently its Dir, sub, file (repeat).
I would like to know how to do this in the best way. I assumed a nested directory would be the one but really not sure how to achieve this. I will continue to learn on my own but any help would be greatly appreciated here.
Thank you
I know i need to make a Dict = {} and not [] (list)
I have removed the dict parts and left the working list for you to help me out a bit easier.
I'm just not sure how to add the right files or Subfolders to the right dict here.
Thank you in advance.
I think this may be what i want?: https://thispointer.com/python-how-to-ge...rectories/
After a little help or understanding (granted this is day 2 of me learning python)
Problem.
I would like to do something simple... List files and folders in a (list or dict)
I would like it as shown in the tree below after searching.
Dir-
*Subfolder-
Files-
Files-
Dir-
*SubFolder-
**File-
Dir-
*Subfolder
*Subfolder
Currently its Dir, sub, file (repeat).
I would like to know how to do this in the best way. I assumed a nested directory would be the one but really not sure how to achieve this. I will continue to learn on my own but any help would be greatly appreciated here.
Thank you
Code
#!/usr/bin/python
import os
allfiles = []
dict = {}
Dirs = {}
Sub = {}
Fle = {}
print(dict)
def checkfiles():
global allfiles
root = os.path.expanduser("~")
root = os.path.join(root, "Documents")
print ("Root:", root)
for dirlist, subdirlist, filelist in os.walk(root):
allfiles.append(dirlist)
for subs in subdirlist:
allfiles.append(subs)
for files in filelist:
allfiles.append(files)
list = checkfiles()
print("Dictionary: ", allfiles)I know i need to make a Dict = {} and not [] (list)
I have removed the dict parts and left the working list for you to help me out a bit easier.
I'm just not sure how to add the right files or Subfolders to the right dict here.
Thank you in advance.
I think this may be what i want?: https://thispointer.com/python-how-to-ge...rectories/


![[Image: lESkCf.jpg]](https://snipboard.io/lESkCf.jpg)