Posts: 18
Threads: 8
Joined: Dec 2021
Dec-24-2021, 05:41 AM
(This post was last modified: Dec-24-2021, 05:41 AM by noahverner1995.)
Let's say that I have the following folders in a path called C:\Users\Test
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.
Posts: 582
Threads: 1
Joined: Aug 2019
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.
Posts: 379
Threads: 2
Joined: Jan 2021
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'])
Posts: 18
Threads: 8
Joined: Dec 2021
Dec-25-2021, 07:53 AM
(This post was last modified: Dec-25-2021, 08:35 AM by noahverner1995.)
(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']
Posts: 18
Threads: 8
Joined: Dec 2021
(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?
Posts: 582
Threads: 1
Joined: Aug 2019
(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
Posts: 582
Threads: 1
Joined: Aug 2019
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
Posts: 18
Threads: 8
Joined: Dec 2021
Dec-25-2021, 10:35 PM
(This post was last modified: Dec-25-2021, 10:42 PM by noahverner1995.)
(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...
Posts: 18
Threads: 8
Joined: Dec 2021
Dec-25-2021, 10:46 PM
(This post was last modified: Dec-25-2021, 10:47 PM by noahverner1995.)
(Ignore this post, I replied twice and could not delete the second one)
|