Python Forum
How can I create a linked list that contains each folder with its files?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I create a linked list that contains each folder with its files?
#8
(Dec-25-2021, 09:42 AM)ibreeden Wrote:
(Dec-25-2021, 07:55 AM)noahverner1995 Wrote: Well, that would work indeed, but how can a program manage to get the data from the folders in the path provided into that linked list?
Well, that is easy. You have done a good job. In line 24 (print(list_of_file_contents)) you have all the data you need.
So just after "# Main method" you add a line to create the dictionary:
the_linked_list = {}
... And after line "print(list_of_file_contents)" you fill the dictionary:
the_linked_list[path] = list_of_file_contents
That should do it. You don't need the lines after "# Get files and add directory to LL".
And again: this is not a "linked list", it is a dictionary.

I see, I think I managed to deal with my problem using a dictionary, I rewrote the code for the case of wanting to apply it only in the current directory (where this program would be found).

import os
# Main method

the_dictionary_list = {}

for subdir in os.listdir("."):
    if os.path.isdir(subdir):
        path = os.path.abspath(subdir)
        print(f'\u001b[45m{path}\033[0m')
        list_of_file_contents = os.listdir(path)
        print(f'\033[46m{list_of_file_contents}')
        the_dictionary_list[subdir] = list_of_file_contents
        print('\n')
print('\033[1;37;40mThe dictionary list:\033[0m')
for subdir in the_dictionary_list:
    print('\u001b[43m'+subdir+'\033[0m')
    for archivo in the_dictionary_list[subdir]:
        print("    ", archivo)
print('\n')
print(the_dictionary_list)
In my case, the code above prints the following output:

Quote:Color
['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png']


Cuerpo
['Cuerpo_cangrejo.png']


Fondo
['Oceano.png']


Ojos
['Antenas.png', 'Pico.png', 'Verticales.png']


Pinzas
['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png']


Puas
['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']


the_dictionary_list:
{'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

Now it's only left the linked list, If I manage to do it that way, I will post it here...
ibreeden likes this post
Reply


Messages In This Thread
RE: How can I create a linked list that contains each folder with its files? - by noahverner1995 - Dec-25-2021, 10:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star Little help with my code - Linked List SantiagoPB 1 2,226 Mar-14-2021, 01:20 PM
Last Post: BashBedlam
  Linked List - Ordering by largest population with country name. PaleHorse 2 3,079 Jun-16-2020, 09:04 PM
Last Post: jefsummers
  iterating through all files from a folder gonzo620 10 8,482 Nov-06-2018, 01:48 AM
Last Post: gonzo620
  create a 20 digit string, and cast to a list then add all the digits as integers nikhilkumar 2 6,479 Jul-19-2017, 04:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020