How to use python os.walk, but first get the subfolders and then the files as XML file
Question: I'm python beginner and started to work on the below script. It already works,
but in the wrong way. Now i get stuck and I would like some help. I use
os.walk in order to get an index as a XML file of a filepath in Windows. I
also added the current result of the script, and what I need to get out.

The difference between the result and what i need, is that the subfolders are
before the files.

    
    
    ##This Script creates an Index as XML file of a filepath in Windows
    import os
    
    #variable
    CrawlingStartpoint = r"D:\DATA\WorldDem"
    XMLfile = r"xml_index.xml"
    XMLLocation = r"D:\DATA\\"
    XMLFileLocation = XMLLocation + XMLfile
    text = ""
    
    #Standard Starting text for the XML file
    text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ShoeBox>\n<Version>2011</Version>\n<FileGroup>\n<Title>ShoeBox: "
    text += XMLfile + "</Title>\n<Description>" + CrawlingStartpoint + "</Description>\n<Expanded>false</Expanded>\n<AddOutputs>false</AddOutputs>\n"
    
    #Scan all folders, subfolders and files. It writes foldernames, subfolder names and files.
    #It write a closegroup for every subfolder  to build the filestructure in a XML file.
    startdept = CrawlingStartpoint.split('\\')
    startdept = len(startdept) - 1
    old = startdept - 1
    for root, dirs, files in os.walk(CrawlingStartpoint, topdown=True):
        path = root.split('\\')
        if (len(path) - 1) < old:
                text += (((old - (len(path) - 1))+1)*'</FileGroup>\n')
                old = (len(path) -1)
        elif (len(path) - 1) == old:
            text += "</FileGroup>\n"
        if os.path.join(root) <> CrawlingStartpoint:
            text += "<FileGroup>\n<Title>" + os.path.basename(root) + "</Title>\n<Description>noop</Description>\n<Expanded>false</Expanded>\n<AddOutputs>false</AddOutputs>\n"
        for filename in files:
            if filename.endswith(".img") or filename.endswith(".jp2") or filename.endswith(".tif"):
                text += "<File>\n<Path>" + os.path.join(root, filename) + "</Path>\n<Type>raster</Type>\n<Description></Description>\n</File>\n"
            elif filename.endswith(".shp") or filename.endswith(".dxf"):
                text += "<File>\n<Path>" + os.path.join(root, filename) + "</Path>\n<Type>vector</Type>\n<Description></Description>\n</File>\n"
        if (len(path) - 1) > old:
            old = old + 1
    text += (((len(path) -1) - startdept)*'</FileGroup>\n')
    
    #Standard Closing text for the XML file
    text += "</FileGroup>" + "\n" + "</ShoeBox>"
    
    #Write the filelocations stored in text in the textfile
    myfile = open(XMLFileLocation,'a')
    myfile.write(text)
    myfile.close()
    

## Result of my script :

    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ShoeBox>
                    <Version>2011</Version>
                    <FileGroup>
                                   <Title>ShoeBox: xml_index.xml</Title>
                                   <Description>D:\DATA\WorldDem</Description>
                                   <Expanded>false</Expanded>
                                   <AddOutputs>false</AddOutputs>
                                   <FileGroup>
                                                   <Title>srtm_geotiff_download</Title>
                                                   <Description>noop</Description>
                                                   <Expanded>false</Expanded>
                                                   <AddOutputs>false</AddOutputs>
                                                                   <File>
                                                                   <Path>D:\DATA\WorldDem\srtm_geotiff_download\srtm1_90m_v41.img</Path>
                                                                   <Type>raster</Type>
                                                                   <Description></Description>
                                                                   </File>
                                   </FileGroup>
                                   <FileGroup>
                                                   <Title>viewfinderpanoramas</Title>
                                                   <Description>noop</Description>
                                                   <Expanded>false</Expanded>
                                                   <AddOutputs>false</AddOutputs>
                                                   <File>
                                                                   <Path>D:\DATA\WorldDem\viewfinderpanoramas\mosaic_vfp_90m_bewerkt_20140211.img</Path>
                                                                   <Type>raster</Type>
                                                                   <Description></Description>
                                                   </File>
                                                   <FileGroup>
                                                                   <Title>Compressed</Title>
                                                                   <Description>noop</Description>
                                                                   <Expanded>false</Expanded>
                                                                   <AddOutputs>false</AddOutputs>
                                                   </FileGroup>
                                                   <FileGroup>
                                                                   <Title>Modellen</Title>
                                                                   <Description>noop</Description>
                                                                   <Expanded>false</Expanded>
                                                                   <AddOutputs>false</AddOutputs>
                                                   </FileGroup>
                                   </FileGroup>
                    </FileGroup>
    </ShoeBox>
    

## Result needed :

    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ShoeBox>
                    <Version>2011</Version>
                    <FileGroup>
                                   <Title>ShoeBox: xml_index.xml</Title>
                                   <Description>D:\DATA\WorldDem</Description>
                                   <Expanded>false</Expanded>
                                   <AddOutputs>false</AddOutputs>
                                   <FileGroup>
                                                   <Title>srtm_geotiff_download</Title>
                                                   <Description>noop</Description>
                                                   <Expanded>false</Expanded>
                                                   <AddOutputs>false</AddOutputs>
                                                                   <File>
                                                                   <Path>D:\DATA\WorldDem\srtm_geotiff_download\srtm1_90m_v41.img</Path>
                                                                   <Type>raster</Type>
                                                                   <Description></Description>
                                                                   </File>
                                   </FileGroup>
                                   <FileGroup>
                                                   <Title>viewfinderpanoramas</Title>
                                                   <Description>noop</Description>
                                                   <Expanded>false</Expanded>
                                                   <AddOutputs>false</AddOutputs>
                                                   <FileGroup>
                                                                   <Title>Compressed</Title>
                                                                   <Description>noop</Description>
                                                                   <Expanded>false</Expanded>
                                                                   <AddOutputs>false</AddOutputs>
                                                   </FileGroup>
                                                   <FileGroup>
                                                                   <Title>Modellen</Title>
                                                                   <Description>noop</Description>
                                                                   <Expanded>false</Expanded>
                                                                   <AddOutputs>false</AddOutputs>
                                                   </FileGroup>
                                                   <File>
                                                                   <Path>D:\DATA\WorldDem\viewfinderpanoramas\mosaic_vfp_90m_bewerkt_20140211.img</Path>
                                                                   <Type>raster</Type>
                                                                   <Description></Description>
                                                   </File>
                                   </FileGroup>
                    </FileGroup>
    </ShoeBox>
    


Answer: I don't think there's any easy way to do what you want. At least, there isn't
if you're writing your XML by hand.

The reason is that you need to build your XML document from the root up,
starting with the outermost directory, producing the top-most `<FileGroup>`
tag. On the other hand, you want to dive into the subfolders before you write
out the files from each folder, so that files in the leaf directories will
appear first, and those in the root directory will be last. Those two goals
are in tension with each other.

I think there are two strategies that you could take to solving the problem.

The first is to use a more sophisticated approach to writing your XML file.
For instance, if you use a library that lets you dynamically create the
elements of the document, rather than just using string manipulations, you may
be able to insert the subfolder information above the file information, even
though you're process them in the same order you do now (files first). I don't
know Python's XML libaries very well, so I don't have any specific suggestions
here, but probably the place to start is the documentation on the
[`xml`](http://docs.python.org/3/library/xml.html) package in the standard
library (you'll probably just want a submodule like `xml.etree.Etree`, but I'm
not sure if that's the best one for your task, so check the package out and
read up on whichever parts look appropriate).

The other option would be to change up how you get your data. If you dropped
the `topdown=True` argument to `os.walk`, it would start with some deeper
folders first, then come back to the higher level ones, which is exactly the
order you want to be showing their files. The tricky part about this is that
you'd need to build the higher level `<FileGroup>` tags before you get around
to processing the folders they are for. You'll need to deduce the depth you're
at based on the paths of the folders, but you're already doing some of this,
so it probably won't be too much more complicated. (By the way, you really
should be using the functions in the `os.path` module for file path
manipulations, rather than doing things like `my_path.split("\\")`.)

