ls files --> list packer
Magnus Lycka
lycka at carmen.se
Mon Feb 27 12:58:45 EST 2006
kpp9c wrote:
> that is nice.... but the little further wrinkle, which i have no idea
> how to do, would be to have the contents of each directory packed into
> a different list.... since you have no idea before hand how many lists
> you will need (how many subdirs you will enounter) ... well that is
> where the hairy part comes in...
What's the problem? If you'll get an unknown bundle of objects in a
program, you just put them in a container. A list or a dict will do
fine. Have a look at the Python tutorial.
You get a file list for each directory from os.walk. You either keep
a list called "directories" and for each turn in the loop, you do
"directories.append((dir, sound_files))", or you have a dict called
"directories", and do "directories[dir] = sound_files" in the loop.
Something like this untested code:
def isSoundFile(x):
# home work
dirs = {}
root = raw_input('start directory')
for dir, dummy, files in os.walk(root):
dirs[dir] = [x for x in files if isSoundFile(x)]
for dir in sorted(dirs):
print dir
for fn in dirs[dir]:
print "\t", fn
print
More information about the Python-list
mailing list