Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python Dict help.

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

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/
(May 5, 2020 07:14 AM)CodeAssassin Wrote:  I think this may be what i want?: https://thispointer.com/python-how-to-ge...rectories/

Hey mate,

Isn't what you want achieved in the link you've posted? Or I have not understood the question?
(May 5, 2020 07:47 AM)SIM-4378207 Wrote:  Hey mate,

Isn't what you want achieved in the link you've posted? Or I have to understood the question?

No your totally right I was way over complicating it...

[Image: lESkCf.jpg]

This info helped me realise this: https://www.bogotobogo.com/python/python...s_walk.php

Thank you still :)
(May 5, 2020 08:04 AM)CodeAssassin Wrote:  No your totally right I was way over complicating it...

[Image: lESkCf.jpg]

This info helped me realise this: https://www.bogotobogo.com/python/python...s_walk.php

Thank you still :)

Glad you figured it out.

Good luck mate, I am also studying Python. Currently making an email client as an uni project.
(May 5, 2020 08:51 AM)SIM-4378207 Wrote:  Glad you figured it out.

Good luck mate, I am also studying Python. Currently making an email client as a uni project.

Hey man,

That's pretty cool, I would love to when you can (if you can) see the GitHub to help me see how apps in python are made. Else I wish you all the best with the project my friend :)

Thank you for the green to!!
(May 5, 2020 11:58 AM)CodeAssassin Wrote:  Hey man,

That's pretty cool, I would love to when you can (if you can) see the GitHub to help me see how apps in python are made. Else I wish you all the best with the project my friend :)

Thank you for the green to!!

Sure, will share once finished. It's pretty basic. I am using wxPython for GUI which is kinda ugly but our professor wants us to use it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by degadoMar 5, 2020 10:43 AM 2 121 Mar 5, 2020 10:43 AM
Last Post: degado
[Python] by lamadamadooDec 17, 2017 09:24 AM 2 106 Dec 17, 2017 09:24 AM
Last Post: lamadamadoo