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:


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*\\"


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?


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*\\"


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.

  • So just to clear it up, you want a copy of grid into each country folder right? – Nathan W Apr 1 '15 at 11:59
  • I found in the past copying individual shapefile sometimes is problematic and may lead to a corrupt shapefile. You code only looks like it is copying the .shp of the shpafile, what about, .dbf, .shx...etc? Instead of using shutil to copy I would suggest using arcpy feature class to feature class method. – artwork21 Apr 1 '15 at 12:00
  • @NathanW - Yes, a copy of grid in each country folder but I would like to keep the country filename. I.e. copying Grid.shp to France.shp, the result would be France.shp. – Joseph Apr 2 '15 at 11:51
  • @artwork21 - That's a very good point, thank you! I tend to call QGIS functions in my script which, I guess, automatically creates the .dbf, .shx files. I forgot that shutil.copy is purely python :) – Joseph Apr 2 '15 at 12:22

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.


os.walk returns a tuple of three values:

for path, dirs, files in os.walk()

Also consider https://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.