diff --git a/pandas/formats/format.py b/pandas/formats/format.py index 7706666142a64..39670783d5f97 100644 --- a/pandas/formats/format.py +++ b/pandas/formats/format.py @@ -1455,9 +1455,9 @@ def save(self): f = self.path_or_buf close = False else: - f = _get_handle(self.path_or_buf, self.mode, - encoding=self.encoding, - compression=self.compression) + f, new_handle = _get_handle(self.path_or_buf, self.mode, + encoding=self.encoding, + compression=self.compression) close = True try: diff --git a/pandas/io/common.py b/pandas/io/common.py index 440b32924308f..25cad9605fbc2 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -295,9 +295,14 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None, Returns ------- - A file like object. + f : file-like + A file-like object + new_handle : file-like or None + A file-like object that was openned in this function. Or None if none + were openned. """ + new_handle = None f = path_or_buf is_path = isinstance(path_or_buf, compat.string_types) @@ -358,7 +363,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None, from io import TextIOWrapper f = TextIOWrapper(f, encoding=encoding) - return f + new_handle = f + return f, new_handle elif is_path: if compat.PY2: @@ -370,11 +376,13 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None, else: # Python 3 and no explicit encoding f = open(path_or_buf, mode, errors='replace') + new_handle = f # in Python 3, convert BytesIO or fileobjects passed with an encoding if compat.PY3 and isinstance(path_or_buf, compat.BytesIO): from io import TextIOWrapper f = TextIOWrapper(f, encoding=encoding) + new_handle = f if memory_map and hasattr(f, 'fileno'): try: @@ -388,7 +396,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None, # leave the file handler as is then pass - return f + return f, new_handle class MMapWrapper(BaseIterator): diff --git a/pandas/io/json.py b/pandas/io/json.py index 878506a6ddc05..a42d124daa5ba 100644 --- a/pandas/io/json.py +++ b/pandas/io/json.py @@ -259,8 +259,10 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, exists = False if exists: - with _get_handle(filepath_or_buffer, 'r', encoding=encoding) as fh: - json = fh.read() + fh, new_handle = _get_handle(filepath_or_buffer, 'r', + encoding=encoding) + json = fh.read() + fh.close() else: json = filepath_or_buffer elif hasattr(filepath_or_buffer, 'read'): diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 401a99cade206..4b6eea3a0d08d 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -1805,18 +1805,11 @@ def __init__(self, f, **kwds): self.comment = kwds['comment'] self._comment_lines = [] - add_handle = ( - isinstance(f, compat.string_types) or - self.compression or - (compat.PY3 and isinstance(f, compat.BytesIO)) - ) - - f = _get_handle(f, 'r', encoding=self.encoding, - compression=self.compression, - memory_map=self.memory_map) - - if add_handle: - self.handles.append(f) + f, new_handle = _get_handle(f, 'r', encoding=self.encoding, + compression=self.compression, + memory_map=self.memory_map) + if new_handle is not None: + self.handles.append(new_handle) # Set self.data to something that can read lines. if hasattr(f, 'readline'):