astro py:docs

- Index
- Modules

### Navigation

- next »
- « previous |
- Astropy v0.3 »
- Astropy Core Package Utilities (`astropy.utils`) »
- get_readable_fileobj

# get_readable_fileobj

`astropy.utils.data.``get_readable_fileobj`(*args, **kwds)

  Given a filename or a readable file-like object, return a context manager that yields a readable file-like object.

  This supports passing filenames, URLs, and readable file-like objects, any of which can be compressed in gzip or bzip2.

  Parameters:

  name_or_obj : str or file-like object

  > The filename of the file to access (if given as a string), or the file-like object to access.

  > If a file-like object, it must be opened in binary mode.

  encoding : str, optional

  > When `None` (default), returns a file-like object with a `read` method that on Python 2.x returns `bytes` objects and on Python 3.x returns `str` (`unicode`) objects, using `locale.getpreferredencoding()` as an encoding. This matches the default behavior of the built-in `open` when no `mode` argument is provided.

  > When `'binary'`, returns a file-like object where its `read` method returns `bytes` objects.

  > When another string, it is the name of an encoding, and the file-like object’s `read` method will return `str` (`unicode`) objects, decoded from binary using the given encoding.

  cache : bool, optional

  > Whether to cache the contents of remote URLs.

  show_progress : bool, optional

  > Whether to display a progress bar if the file is downloaded from a remote server. Default is `True`.

  remote_timeout : float

  > Timeout for remote requests in seconds (default is the configurable REMOTE_TIMEOUT, which is 3s by default)

  Returns: | file : readable file-like object

  Notes

  This function is a context manager, and should be used for example as:

      with get_readable_fileobj('file.dat') as f:
          contents = f.read()