1

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
asked Jan 25, 2012 at 0:45
6
  • 3
    If you're going to use this for anything other than learning I would suggest using os.walk Commented Jan 25, 2012 at 0:53
  • In general, I agree, but what if you only want a single level? Commented Jan 25, 2012 at 1:29
  • Hey @jeff As I said I was new to python, I looked at os.walk but thought it might be overkill for one directory level. Would os.walk be better ? Commented Jan 25, 2012 at 8:18
  • 1
    @Keet: os.walk is the only correct solution. One level or 100 levels. Commented Jan 25, 2012 at 13:24
  • 1
    @Keet no it's not overkill because os.walk will return a generator object (very cool/efficient python tool) For example foo = os.walk() to get the generator object and levelOne = foo.next() for the first level list. Two lines of code... not bad. Commented Jan 25, 2012 at 16:54

2 Answers 2

1

i'd use a dictionary for this purpose, the keys would be directories and the values lists of files:

def getffdirs():
 dirs = {}
 path = os.curdir
 for d in os.listdir(path): 
 if os.path.isdir(d):
 print("Entering " + d)
 dirs[d] = [] # add directory with empty list
 curPath = os.path.join(path, d)
 for f in os.listdir(curPath): 
 if os.path.isfile(f):
 print("file " + f) 
 dirs[d].append(f) # add files to appropriate directory
 return dirs

to access the data:

for dir,files in dirs.items(): # notice the call to dirs.items(), thats what was missing.
 print "directory: ",dir
 print "files:"
 for f in files:
 print f
answered Jan 25, 2012 at 0:49
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for that. That seems like what I am after but I get a 'ValueError: too many values to unpack' error. Any ideas ?
1

EDIT: If you want a dictionary,

EDIT 2: If you want to omit hidden directories,

from os import listdir, curdir
from os.path import isfile, isdir, join
filedirs = dict((d, [f for f in listdir(d) if isfile(join(d,f))]) 
 for d in listdir(curdir) if isdir(d) and d[0] != '.')

Original:

Is this what you're looking for?

filedirs = [[f for f in os.listdir(d) if os.path.isfile(os.path.join(d,f))]
 for d in os.listdir('.') if os.path.isdir(d)]
answered Jan 25, 2012 at 0:53

1 Comment

Thanks for that, your original solution only returned the files as I had already, but both were interesting approaches ! I think you're right, dictionary's are the way to go.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.