Hack Forums
[Python] Python Dict help. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Python Dict help. (/showthread.php?tid=6074922)



Python Dict help. - CodeAssassin - May 5, 2020

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-get-list-of-files-in-directory-and-sub-directories/


RE: Python Dict help. - SIM-4378207 - May 5, 2020

(May 5, 2020 07:14 AM)CodeAssassin Wrote:  I think this may be what i want?: https://thispointer.com/python-how-to-get-list-of-files-in-directory-and-sub-directories/

Hey mate,

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


RE: Python Dict help. - CodeAssassin - May 5, 2020

(May 5, 2020 07:47 AM)Wortox 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_traversing_directory_tree_recursively_os_walk.php

Thank you still :)


RE: Python Dict help. - SIM-4378207 - May 5, 2020

(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_traversing_directory_tree_recursively_os_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.


RE: Python Dict help. - CodeAssassin - May 5, 2020

(May 5, 2020 08:51 AM)Wortox 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!!


RE: Python Dict help. - SIM-4378207 - May 6, 2020

(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.