Recently I added the ability to read from Python file-like objects as well (as of version 1.1.2). Currently saving shapefiles to a file-like object requires calling three individual methods (saveShp, saveShx, saveDbf). Reading shapefiles uses keyword arguments. Eventually saving shapefiles will do so as well.
Reading a shapefile from the file system still works the same way - just pass in the name of the file.
import shapefile r = shapefile.Reader("myshapefile")
But if you use the keywords shp, shx, and dbf then you can specify file-like objects. This example will demonstrate reading a shapefile - from a zip file - on a website.
import urllib2 import zipfile import StringIO import shapefile cloudshape = urllib2.urlopen("http://pyshp.googlecode.com/files/GIS_CensusTract.zip") memoryshape = StringIO.StringIO(cloudshape.read()) zipshape = zipfile.ZipFile(memoryshape) shpname, shxname, dbfname, prjname = zipshape.namelist() cloudshp = zipshape.read(shpname) cloudshx = zipshape.read(shxname) clouddbf = zipshape.read(dbfname) r = shapefile.Reader(shp=cloudshp, shx=cloudshx, dbf=clouddbf) r.bbox [-89.8744162216216, 30.161122135135138, -89.1383837783784, 30.661213864864862]
You may specify only one of the three file types if you are just trying to read one of the file types. Some attributes such as Reader.shapeName will not be available using this method.
File-like objects provide a lot of power. However it is important to note that not all file-like objects implement all of the file methods. In the above example the urllib2 module does not provide the "seek" method needed by the zipfile module. You can always transfer data to the StringIO or cStringIO module in memory to ensure compatibility. If the data is potentially too big to hold in memory you can use the tempfile module to temporarily store the shapefile data on disk.