'How to iterate through and store various images that live in various folders under a root directory in Python?
I have 5 folders f1, f2, f3, f4, f5 and I have several images in each folder.
What I am trying to do is make a dictionary object that holds all images based on their key (which is the folder name) and their value (which is the file names living under that specific folder).
The way I have written this is using os.walk():
import os
dict_object = {}
for root, dirs, files in os.walk(".", topdown=False):
for file_name in files:
for dir_name in dirs:
dict_object[dir_name] = [file_name]
But the output is not quite what I am after as it does not list the file names (values) under the appropriate folder names (keys):
dict_object
{'.DS_Store': [],
'f1': ['.DS_Store'],
'.ipynb_checkpoints': ['.DS_Store'],
'f2': ['.DS_Store'],
'f3': ['.DS_Store'],
'f4': ['.DS_Store'],
'f5': ['.DS_Store'],
'.': ['.DS_Store']}
And I also do not want to include '.DS_Store': [] and '.': ['.DS_Store'] in the dictionary.
Solution 1:[1]
This is how I ended up doing this:
# Define various folders
folders = ["f1", "f2", "f3", "f4", "f5"]
paths = []
file_paths = {}
# Put all paths together
for folder in range(len(folders)):
paths.append('parentfolder/'+folders[folder]+'/')
temp_file_paths = [os.path.join(paths[folder], f) for f in os.listdir(paths[folder])]
if paths[folder] + '.DS_Store' in temp_file_paths:
temp_file_paths.remove(paths[folder] + '.DS_Store')
file_paths[folders[folder]] = temp_file_paths
.DS_Store: While it appears to be a useless addition to a folder, a .DS_Store file (the “DS” stands for “Desktop Services”) is important in helping Mac work out how to display folders when you open them. Read more here
Solution 2:[2]
You can accomplish the wanted result with a simple recursive function. For unwanted files, you can either filter the dictionary after the function invocation or change the if statement.
def recursive_read(path=".", dict_result={}):
for root, dirs, files in os.walk(path, topdown=True):
if (root == '.'):
continue
if (root in dict_result):
dict_result[root].append(files)
else:
dict_result[root] = files
for dir in dirs:
recursive_read(os.path.join(root, dir))
return dict_result
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amir Charkhi |
| Solution 2 | Arshia Gholami |
