@@ -161,6 +161,22 @@ def u(v, encoding='utf-8', encodingErrors='strict'):
161
161
def is_string (v ):
162
162
return isinstance (v , basestring )
163
163
164
+ if sys .version_info [0 :2 ] >= (3 , 6 ):
165
+ def pathlike_obj (path ):
166
+ if isinstance (path , os .PathLike ):
167
+ return os .fsdecode (path )
168
+ else :
169
+ return path
170
+ else :
171
+ def pathlike_obj (path ):
172
+ # detect if the object is path-like
173
+ if "path" in path .__class__ .__name__ .lower ():
174
+ try :
175
+ return str (path )
176
+ except TypeError :
177
+ pass
178
+ return path
179
+
164
180
165
181
# Begin
166
182
@@ -930,14 +946,14 @@ def __init__(self, *args, **kwargs):
930
946
self .encodingErrors = kwargs .pop ('encodingErrors' , 'strict' )
931
947
# See if a shapefile name was passed as the first argument
932
948
if len (args ) > 0 :
933
- if is_string (args [0 ]):
934
- path = args [ 0 ]
935
-
949
+ path = pathlike_obj (args [0 ])
950
+ if is_string ( path ):
951
+
936
952
if '.zip' in path :
937
953
# Shapefile is inside a zipfile
938
954
if path .count ('.zip' ) > 1 :
939
955
# Multiple nested zipfiles
940
- raise ShapefileException ('Reading from multiple nested zipfiles is not supported: %s' % args [ 0 ] )
956
+ raise ShapefileException ('Reading from multiple nested zipfiles is not supported: %s' % path )
941
957
# Split into zipfile and shapefile paths
942
958
if path .endswith ('.zip' ):
943
959
zpath = path
@@ -1708,8 +1724,9 @@ def __init__(self, target=None, shapeType=None, autoBalance=False, **kwargs):
1708
1724
self .shapeType = shapeType
1709
1725
self .shp = self .shx = self .dbf = None
1710
1726
if target :
1727
+ target = pathlike_obj (target )
1711
1728
if not is_string (target ):
1712
- raise Exception ('The target filepath {} must be of type str/unicode, not {}.' .format (repr (target ), type (target )) )
1729
+ raise Exception ('The target filepath {} must be of type str/unicode or path-like , not {}.' .format (repr (target ), type (target )) )
1713
1730
self .shp = self .__getFileObj (os .path .splitext (target )[0 ] + '.shp' )
1714
1731
self .shx = self .__getFileObj (os .path .splitext (target )[0 ] + '.shx' )
1715
1732
self .dbf = self .__getFileObj (os .path .splitext (target )[0 ] + '.dbf' )
0 commit comments