7
7
import zipfile
8
8
from contextlib import contextmanager , closing
9
9
10
- from pandas .compat import StringIO , string_types , BytesIO
10
+ from pandas .compat import StringIO , BytesIO , string_types , text_type
11
11
from pandas import compat
12
12
from pandas .core .common import pprint_thing , is_number
13
13
14
14
15
+ try :
16
+ import pathlib
17
+ _PATHLIB_INSTALLED = True
18
+ except ImportError :
19
+ _PATHLIB_INSTALLED = False
20
+
21
+
22
+ try :
23
+ from py .path import local as LocalPath
24
+ _PY_PATH_INSTALLED = True
25
+ except :
26
+ _PY_PATH_INSTALLED = False
27
+
28
+
15
29
if compat .PY3 :
16
30
from urllib .request import urlopen , pathname2url
17
31
_urlopen = urlopen
@@ -204,6 +218,25 @@ def _validate_header_arg(header):
204
218
"header=int or list-like of ints to specify "
205
219
"the row(s) making up the column names" )
206
220
221
+ def _stringify_path (filepath_or_buffer ):
222
+ """Return the argument coerced to a string if it was a pathlib.Path
223
+ or a py.path.local
224
+
225
+ Parameters
226
+ ----------
227
+ filepath_or_buffer : object to be converted
228
+
229
+ Returns
230
+ -------
231
+ str_filepath_or_buffer : a the string version of the input path
232
+ """
233
+ if _PATHLIB_INSTALLED and isinstance (filepath_or_buffer , pathlib .Path ):
234
+ return text_type (filepath_or_buffer )
235
+ if _PY_PATH_INSTALLED and isinstance (filepath_or_buffer , LocalPath ):
236
+ return filepath_or_buffer .strpath
237
+ return filepath_or_buffer
238
+
239
+
207
240
def get_filepath_or_buffer (filepath_or_buffer , encoding = None ,
208
241
compression = None ):
209
242
"""
@@ -212,7 +245,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
212
245
213
246
Parameters
214
247
----------
215
- filepath_or_buffer : a url, filepath, or buffer
248
+ filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
249
+ or buffer
216
250
encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
217
251
218
252
Returns
@@ -260,6 +294,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
260
294
filepath_or_buffer = k
261
295
return filepath_or_buffer , None , compression
262
296
297
+ # It is a pathlib.Path/py.path.local or string
298
+ filepath_or_buffer = _stringify_path (filepath_or_buffer )
263
299
return _expand_user (filepath_or_buffer ), None , compression
264
300
265
301
0 commit comments