I am trying to find a fast way to merge two shapefiles and i ran over pyshp which apparently could do the job. However looks like this is not working for me because i receive the following error

file "c:\net\shapefile.py" ine 443 in __dbfHeader
fieldDesc = list(unpack("<11sc4xBB14x", dbf.read(32))) error: unpack requires a string argument of length 32.

I have no idea what could be wrong and i've been trying to find a solution but no luck

My code looks like this:

import glob
import shapefile
import sys
files = glob.glob("C:\TestSHAPE\merge\*.shp")
w = shapefile.Writer()
for f in files:
    r = shapefile.Reader(f)
w.fields = list(r.fields)

1 Answer 1


The error message seems to suggest that there could be a fault in the attributes file of one of your shapefiles or with the pyshp module itself (perhaps corrupted during download). However, there are also some issues with your code that is muddying the waters.

  1. You are appending the location of the pyshp shapefile module AFTER you have imported it. This tells me that Python already knows where it is or is importing another version of the shapefile module from somewhere else which may be incomplete/corrupted or simply incompatible with the version of python you are using.
  2. Your path for files is OK in this instance but be careful of inadvertently using escape characters. In this case you are OK because 'TestSHAPE' starts with a capital 'T'. Had it started with a lowercase 't' Python would have found the '\t' combination in the path which is an escape character. To avoid this gotcha in future put an 'r' in front of your path like this: files = glob.glob(r"C:\TestSHAPE\merge\*.shp")
  3. If your process works it will only contain the last shapefile as you need to move two of your lines to within the loop like this:

    for f in files:
        r = shapefile.Reader(f)
    w.fields = list(r.fields)
  4. You have not specified a path for the output. This means it will most likely be saved to the Python root folder (e.g. C\Python32\merged.shp). This is probably not what you want.

You need to check your shapefiles to be sure that they are not the source of the error. Try opening them in QGIS or something. Next correct your code. Your coding errors by themselves do not account for the error message which is why I am concentrating on either a bad copy of pyshp or a bad shapefile. However, they don't help and if you do track down the problem, your code in the current state will not work as intended.

Finally check out this post which is similar: Pyshp 1.2 example from documentation fails


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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