Python nested loop -



Python nested loop -

hello new python , have question best/pythonic way nested loops.

i want go set each directory in array nested array of file contained in directory.

i have been looking @ pythons arrays, dicts, sets , tupples , not sure of best way this

[ note want 1 level not recursively through directories ]

currently have function adds files of sub-directories array, need homecoming parent folder too.

thanks in advance

def getffdirs(): filedirs = [] path = os.curdir d in os.listdir(path): if os.path.isdir(d): print("entering " + d) curpath = os.path.join(path, d) f in os.listdir(curpath): if os.path.isfile(f): print("file " + f) filedirs.append(f) homecoming filedirs

i'd utilize dictionary purpose, keys directories , values lists of files:

def getffdirs(): dirs = {} path = os.curdir d in os.listdir(path): if os.path.isdir(d): print("entering " + d) dirs[d] = [] # add together directory empty list curpath = os.path.join(path, d) f in os.listdir(curpath): if os.path.isfile(f): print("file " + f) dirs[d].append(f) # add together files appropriate directory homecoming dirs

to access data:

for dir,files in dirs.items(): # notice phone call dirs.items(), thats missing. print "directory: ",dir print "files:" f in files: print f

python nested

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -