Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a4e29a

Browse files
committed
Fix py2 zipfile.open() not allowing seek by switching to extract()+open()
1 parent 0cf1565 commit 2a4e29a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

shapefile.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -971,12 +971,16 @@ def __init__(self, *args, **kwargs):
971971
path to the shapefile you would like to open.' % shapefiles )
972972
# Try to extract file-like objects from zipfile
973973
shapefile = os.path.splitext(shapefile)[0] # root shapefile name
974-
try: self.shp = archive.open(shapefile+'.shp')
975-
except: pass
976-
try: self.shx = archive.open(shapefile+'.shx')
977-
except: pass
978-
try: self.dbf = archive.open(shapefile+'.dbf')
979-
except: pass
974+
tempdir = tempfile.gettempdir()
975+
for ext in ['shp','shx','dbf']:
976+
try:
977+
# Note: archive.open() would be easier, but loads file into memory
978+
# and cannot always use .seek()
979+
temppath = archive.extract(shapefile+'.'+ext, tempdir)
980+
fileobj = open(temppath, 'rb')
981+
setattr(self, ext, fileobj)
982+
except:
983+
pass
980984
else:
981985
# Direct path to a shapefile given
982986
if path.startswith('http'):

0 commit comments

Comments
 (0)