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?
#1
Question 
Let's say that I have the following folders in a path called C:\Users\Test

[Image: iv4My.png]

Every single one of them contains only images (In PNG format) with a different name (also none of them contains subfolders).

Right now, I know how to do some stuff with those files but only after I've typed manually every single one of them as shown in the code below:

Color = ['1.png', '2.png', '3.png']
Cuerpo = ['Body.png']
Fondo = ['Background.png']
Ojos = ['eyes1.png', 'eyes2.png', 'eyes3.png']
Pinzas = ['a.png', 'b.png', 'c.png']
Puas = ['x.png', 'y.png'. 'z.png']
How could I build a program that creates a data structure for the folders above in C:\Users\Test that looks like the one below after double clicking it?

struct the_linked_list{
    
    Color = ['1.png', '2.png', '3.png']
    Cuerpo = ['Body.png']
    Fondo = ['Background.png']
    Ojos = ['eyes1.png', 'eyes2.png', 'eyes3.png']
    Pinzas = ['a.png', 'b.png', 'c.png']
    Puas = ['x.png', 'y.png'. 'z.png']
    }
-It can be assumed that the names of the filenames within those folders are those provided in the example above, respectively.

-The created data structure must have set the names of its nodes as the names of the folders, respectively.
Reply
#2
Hi @noahverner1995 ,
Welcome to the forum. We will help you but you must understand: we don't write programs for you. We will only help you creating them.

So you want to create a dictionary. The key of the dictionary is the name of the folder. The value of the dictionary is a list of files in this folder.
(The title of the thread suggests you want to create a "linked list" but that is something completely different.)

For a start you must write a program to show the folders in the (current) folder. Show us how you do that. Please use the correct tags. See: BBcode how to do that.
Reply
#3
Are you just looking for a dictionary like this?
the_linked_list = {
    'Color' : ('1.png', '2.png', '3.png'),
    'Cuerpo' : ('Body.png'),
    'Fondo' : ('Background.png'),
    'Ojos' : ('eyes1.png', 'eyes2.png', 'eyes3.png'),
    'Pinzas' : ('a.png', 'b.png', 'c.png'),
    'Puas' : ('x.png', 'y.png', 'z.png')
    }

print (the_linked_list ['Color'])
Reply
#4
(Dec-24-2021, 09:30 AM)ibreeden Wrote: Hi @noahverner1995 ,
Welcome to the forum. We will help you but you must understand: we don't write programs for you. We will only help you creating them.

So you want to create a dictionary. The key of the dictionary is the name of the folder. The value of the dictionary is a list of files in this folder.
(The title of the thread suggests you want to create a "linked list" but that is something completely different.)

For a start you must write a program to show the folders in the (current) folder. Show us how you do that. Please use the correct tags. See: BBcode how to do that.

Ok, so far I have managed to do this program which basically iterates over the folders and its contents for the current path, but I don't understand why isn't it actually storing all the data in the linked list rather than just the filenames of the last folder?

import os
    
class Node:
    def __init__(self, name, files):
        self.dir_name = name
        self.files = files
        self.next = None
    
def add(head, new_node):
    '''Adds new node to end of linked list'''
    curr_node = head
    while (curr_node.next != None):
        curr_node = curr_node.next
    
    curr_node.next = new_node
    
# Main method

for name in os.listdir("."):
    if os.path.isdir(name):
        path = os.path.abspath(name)
        print(path)
        list_of_file_contents = os.listdir(path)
        print(list_of_file_contents)
        head = None

        # Get files and add directory to LL
        for dir in list_of_file_contents:
            path_to_dir = f"{path}\{dir}"

            if (os.path.isdir(path_to_dir)):
                file_list = os.listdir(path_to_dir)

                if (head == None):
                    head = Node(dir, file_list)
                else:
                    add(head, Node(dir, file_list))
Output:
C:\Users\Test\Color
['1.png', '2.png', '3.png']
C:\Users\Test\Cuerpo
['Body.png']
C:\Users\Test\Fondo
['Background.png']
C:\Users\Test\Ojos
['eyes1.png', 'eyes2.png', 'eyes3.png']
C:\Users\Test\Pinzas
['a.png', 'b.png', 'c.png']
C:\Users\Test\Puas
['x.png', 'y.png'. 'z.png']
Reply
#5
(Dec-24-2021, 10:27 PM)BashBedlam Wrote: Are you just looking for a dictionary like this?
the_linked_list = {
    'Color' : ('1.png', '2.png', '3.png'),
    'Cuerpo' : ('Body.png'),
    'Fondo' : ('Background.png'),
    'Ojos' : ('eyes1.png', 'eyes2.png', 'eyes3.png'),
    'Pinzas' : ('a.png', 'b.png', 'c.png'),
    'Puas' : ('x.png', 'y.png', 'z.png')
    }

print (the_linked_list ['Color'])

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?
Reply
#6
(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.
noahverner1995 likes this post
Reply
#7
But now for the real answer. In your code I see you really mean a linked list, consisting of nodes:
class Node:
    def __init__(self, name, files):
        self.dir_name = name
        self.files = files
        self.next = None
Each node contains the name of the folder and a list of files. And a link to the next node.
Again you have the data to fill a node in line 24. But now you have to decide if the node is the head, or a linked node. So first create an empty head node. Then when you have the first folder, make this the head node, else add the folder to the linked list.
head = None
for name in os.listdir("."):
    if os.path.isdir(name):
        path = os.path.abspath(name)
        print(path)
        list_of_file_contents = os.listdir(path)
        print(list_of_file_contents)
        current_node = Node(path, list_of_file_contents)
        if head == None:    # Does the list contain a node?
            head = current_node
        else:               # Add a node.
            add(head, current_node)
Does this make sense? How do you check at the end if the linked list contains everything you need?
noahverner1995 likes this post
Reply
#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
#9
(Ignore this post, I replied twice and could not delete the second one)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Little help with my code - Linked List SantiagoPB 1 2,156 Mar-14-2021, 01:20 PM
Last Post: BashBedlam
  Linked List - Ordering by largest population with country name. PaleHorse 2 2,952 Jun-16-2020, 09:04 PM
Last Post: jefsummers
  iterating through all files from a folder gonzo620 10 8,246 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,389 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