[GIS] Using folder wildcards to copy files with Python

pyqgisqgis-processing

I would like to copy the data of one specific shapefile from a specific folder ("Grid") to a number of folders containing the name "Country". The country shapefiles would keep their original name. Since there are multiple Country folders, I thought using a wildcard would be quite useful. The following diagram tries to illustrate what I am doing:

File/folders

Below is the code I have at the moment where I used this post as a guide:

import os, sys, glob, shutil

root_dir = "C:\Users\xxxx\Desktop\Test\\"
country_dir = "Country*\\"

#do_some_function

for path, dirname in os.walk(root_dir):
    if country_dir in path:
        for subdirname in dirname:
            if subdirname.startswith('Country'):
                for fname in glob.glob('*.shp'):
                    shutil.copyfile(do_some_function, fname)

However, I receive the following error:

ValueError: too many values to unpack

Any advice on what I am doing wrong?


EDIT:

Following @NathanW's suggestion, I've changed the script to the following:

import os, sys, shutil

root_dir = "C:\Users\xxxx\Desktop\Test\\"
country_dir = "Country*\\"

#do_some_function

for path, dirs, files in os.walk(root_dir + folder_dir):
    for file in files:
        if file.endswith('*.shp'):
            shutil.copy(do_some_function, file)

Unfortunately, nothing happens.

Best Answer

Try this:

import os, glob, shutil
root_dir = "C:\Users\xxxx\Desktop\Test\\"
country_dir = "Country_"
grid_path = "C:\Users\xxxx\Desktop\Test\Grid\Grid.shp"

# Get all files that constitute the Grid Shapefile
gridShpFiles = glob.glob(grid_path[:-3]+"*")

for path,dirname,files in os.walk(root_dir):
  if country_dir in path:
    for f in gridShpFiles:
      shutil.copy(f, path)

Take your time to set paths (lines 2, 3, and 4), I tested the script on GNU/Linux, but Windows paths are always trickier.

As you notice, you can avoid the wildcard; the expression if country_dir in path: does the work.

Finally, as @atrwork21 mentioned, you need to take all Shapefile files into account. That's what the second block does.