1

I have this folder: sa

that has folders a,b,c. These folders have folders:

PST and DBOUND.

If for example folder b has empty pst folder and existing dbounds, the following code should NOT append contents from that folder at all and skip it.

code:

import geopandas as gpd
from glob import glob

ps=[]
db=[]
for i in glob(r'C:\Users\user\Desktop\sa' + '**/*/*/*', recursive=True):
    if i.endswith('PST.shp'):
        ps.append(i)
    if i.endswith('DBOUND.shp'):
        db.append(i)

How to make it not append from a folder, if their subfolder has either PST or DBOUND folders empty? and append only from folders that have all the needed subfolders there?

Explanation

To help you understand: If all the folders a and b are ok it returns this:

C:\Users\user\Desktop\sa\a\DBOUND\DBOUND.shp
C:\Users\user\Desktop\sa\a\PST\PST.shp
C:\Users\user\Desktop\sa\b\DBOUND\DBOUND.shp
C:\Users\user\Desktop\sa\b\PST\PST.shp

If c has empty either PST or DBOUND folders, it should not appear at all in the list. It should skip the folder and not append it.

  • wrap around an if condition to check first if the pst folder is empty or not. https://stackoverflow.com/questions/49284015/how-to-check-if-folder-is-empty-with-python might help – mad_ Aug 27 '18 at 13:01
  • You should rename i to fpath to make the code easier to understand. Maybe [this post] (https://stackoverflow.com/questions/25675352/how-to-check-to-see-if-a-folder-contains-files-using-python-3) could help you! – Herman Wilén Aug 27 '18 at 13:01
  • Are the file names and the directory names consistent under each top level directory ("a", "b", and "c")? – brandonsimpkins Aug 27 '18 at 13:05
  • I have made a script that checks what pst folders are empty but I am searching for something that will do it as this happens in this loop. Not just to see which are empty but to search and simply work with the needed folders. – user10278864 Aug 27 '18 at 13:07
  • @brandonsimpkins Yes all these folders have subfolders with same names. – user10278864 Aug 27 '18 at 13:08

0 Answers0