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
os.walkis the only correct solution. One level or 100 levels.os.walkwill return agenerator object(very cool/efficient python tool) For examplefoo = os.walk()to get thegenerator objectandlevelOne = foo.next()for the first level list. Two lines of code... not bad.