Hello I am new to python and have a question about the best/pythonic way to do nested loops.
I want to go put each directory in an array with a nested array of the file contained in a that directory.
I have been looking at pythons arrays, dicts, sets and tupples and not sure of the best way to do this
[ Note I just want to do this for one level not recursively through all directories ]
Currently I have a function that adds all the files of sub-directories to an array, but now I need to return their parent folder too.
Thanks in advance
def getffdirs():
filedirs = []
path = os.curdir
for d in os.listdir(path):
if os.path.isdir(d):
print("Entering " + d)
curPath = os.path.join(path, d)
for f in os.listdir(curPath):
if os.path.isfile(f):
print("file " + f)
filedirs.append(f)
return filedirs
i’d use a dictionary for this purpose, the keys would be directories and the values lists of files:
to access the data: