How to get Python to look at Sub-Folders?
Question: I am trying to create a python script that will look in a series of sub-
folders and delete empty shapefiles. I have successfully created the part of
the script that will delete the empty files in one folder, but there are a
total of 70 folders within the "Project" folder. While I could just copy and
paste the code 69 times I'm sure must be a way to get it to look at each sub-
folder and run the code for each of those sub-folders. Below is the what I
have so far. Any ideas? I'm very new to this and I have simply edited an
existing code to get this far. Thanks!

    
    
    import os
    
    # Set the working directory
    os.chdir ("C:/Naview/Platypus/Project")
    
    # Get the list of only files in the current directory
    file = filter(os.path.isfile, os.listdir('C:/Naview/Platypus/Project'))
    # For each file in directory
    for shp in file:
        # Get only the files that end in ".shp"
        if shp.endswith(".shp"):
            # Get the size of the ".shp" file.
            # NOTE: The ".dbf" file can vary is size whereas
            #       the shp & shx are always the same when "empty".
            size = os.path.getsize(shp)
            print "\nChecking " + shp + "'s file size..."
    
            #If the file size is greater than 100 bytes, leave it alone.                  
            if size > 100:
                print "File is " + str(size) + " bytes"
                print shp + " will NOT be deleted \n"
    
            #If the file size is equal to 100 bytes, delete it.      
            if size == 100:
                # Convert the int output from (size) to a string.
                print "File is " + str(size) + " bytes"                    
                # Get the filename without the extention
                base = shp[:-4]
                # Remove entire shapefile
                print "Removing " + base + ".* \n"
                if os.path.exists(base + ".shp"):
                   os.remove(base + ".shp")
                if os.path.exists(base + ".shx"):
                    os.remove(base + ".shx")
                if os.path.exists(base + ".dbf"):
                    os.remove(base + ".dbf")
                if os.path.exists(base + ".prj"):
                    os.remove(base + ".prj")
                if os.path.exists(base + ".sbn"):
                    os.remove(base + ".sbn")
                if os.path.exists(base + ".sbx"):
                    os.remove(base + ".sbx")
                if os.path.exists(base + ".shp.xml"):
                    os.remove(base + ".shp.xml")
    


Answer: There are several ways to do this. I'm a fan of
[`glob`](http://docs.python.org/2/library/glob.html)

    
    
    for shp in glob.glob('C:/Naview/Platypus/Project/**/*.shp'):
        size = os.path.getsize(shp)
        print "\nChecking " + shp + "'s file size..."
    
        #If the file size is greater than 100 bytes, leave it alone.                  
        if size > 100:
            print "File is " + str(size) + " bytes"
            print shp + " will NOT be deleted \n"
            continue
        print "Removing", shp, "files"
        for file in glob.glob(shp[:-3] + '*'):
            print " removing", file
            os.remove(file)
    

