Skip to content

Commit cbf3679

Browse files
committed
Merge pull request #8373 from jbradish/master
ENH: Added file path existence check for read_hdf
2 parents 62ac12b + 04f3d40 commit cbf3679

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/v0.15.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ API changes
262262

263263
- ``Series.to_csv()`` now returns a string when ``path=None``, matching the behaviour of ``DataFrame.to_csv()`` (:issue:`8215`).
264264

265+
-``read_hdf`` now raises IOError properly when a file that doesn't exist is passed in. Previously, a new, empty file was created, read and stored in an HDFStore object (:issue `7715`).
266+
265267
.. _whatsnew_0150.index_set_ops:
266268

267269
- The Index set operations ``+`` and ``-`` were deprecated in order to provide these for numeric type operations on certain index types. ``+`` can be replace by ``.union()`` or ``|``, and ``-`` by ``.difference()``. Further the method name ``Index.diff()`` is deprecated and can be replaced by ``Index.difference()`` (:issue:`8226`)

pandas/io/pytables.py

+10
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ def read_hdf(path_or_buf, key, **kwargs):
332332
key, auto_close=auto_close, **kwargs)
333333

334334
if isinstance(path_or_buf, string_types):
335+
336+
try:
337+
exists = os.path.exists(path_or_buf)
338+
339+
#if filepath is too long
340+
except (TypeError,ValueError):
341+
exists = False
342+
343+
if not exists:
344+
raise IOError('File %s does not exist' % path_or_buf)
335345

336346
# can't auto open/close if we are using an iterator
337347
# so delegate to the iterator

pandas/io/tests/test_pytables.py

+4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ def test_api(self):
288288
self.assertRaises(TypeError, df.to_hdf, path,'df',append=True,format='foo')
289289
self.assertRaises(TypeError, df.to_hdf, path,'df',append=False,format='bar')
290290

291+
#File path doesn't exist
292+
path = ""
293+
self.assertRaises(IOError, read_hdf, path, 'df')
294+
291295
def test_api_default_format(self):
292296

293297
# default_format option

0 commit comments

Comments
 (0)