77import zipfile
88from contextlib import contextmanager , closing
99
10- from pandas .compat import StringIO , string_types , BytesIO
10+ from pandas .compat import StringIO , BytesIO , string_types , text_type
1111from pandas import compat
1212from pandas .core .common import pprint_thing , is_number
1313
1414
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+
1529if compat .PY3 :
1630 from urllib .request import urlopen , pathname2url
1731 _urlopen = urlopen
@@ -204,6 +218,25 @@ def _validate_header_arg(header):
204218 "header=int or list-like of ints to specify "
205219 "the row(s) making up the column names" )
206220
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+
207240def get_filepath_or_buffer (filepath_or_buffer , encoding = None ,
208241 compression = None ):
209242 """
@@ -212,7 +245,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
212245
213246 Parameters
214247 ----------
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
216250 encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
217251
218252 Returns
@@ -260,6 +294,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
260294 filepath_or_buffer = k
261295 return filepath_or_buffer , None , compression
262296
297+ # It is a pathlib.Path/py.path.local or string
298+ filepath_or_buffer = _stringify_path (filepath_or_buffer )
263299 return _expand_user (filepath_or_buffer ), None , compression
264300
265301
0 commit comments