How to read folder structure and assign it to datastructure?
Question: I'm only starting with python, and I'm trying to accomplish following:

I have a folder structure (simplified):

    
    
    -folder1
    ---file1
    ---file2
    -folder2
    ---file3
    -folder2
    ---file4
    ---file5
    ---file6
    

I'd like to read filenames into some kind of a datastructure, that is able to
distinguish which files are from the same folder. I've used glob in a one
folder case, but is it possible to get for example following datastructure
using glob?

    
    
    files = [{file1, folder1}, {file2, folder1}, {file3, folder2}...]
    


Answer: I assume you'd rather get this kind of structure:

    
    
    files = {folder1: [file1, file2], folder2: [file3], ...}
    

The following code will do the trick:

    
    
    import os
    
    rootDir = '.'
    files = {}
    for dirName, subdirList, fileList in os.walk(rootDir):
        files[dirName] = fileList
    

