Retrieves a data file from the standard locations for the package and provides the file as a file-like object that reads bytes.
Parameters: | data_name : str
package : str, optional
encoding : str, optional
cache : bool
|
---|---|
Returns: | fileobj : file-like
|
Raises: | urllib2.URLError, urllib.error.URLError
IOError
|
See also
Examples
This will retrieve a data file and its contents for the astropy.wcs tests:
>>> from astropy.utils.data import get_pkg_data_fileobj
>>> with get_pkg_data_fileobj('data/3d_cd.hdr',
... package='astropy.wcs.tests') as fobj:
... fcontents = fobj.read()
...
This next example would download a data file from the astropy data server because the allsky/allsky_rosat.fits file is not present in the source distribution. It will also save the file locally so the next time it is accessed it won’t need to be downloaded.:
>>> from astropy.utils.data import get_pkg_data_fileobj
>>> with get_pkg_data_fileobj('allsky/allsky_rosat.fits',
... encoding='binary') as fobj:
... fcontents = fobj.read()
...
Downloading http://data.astropy.org/allsky/allsky_rosat.fits [Done]
This does the same thing but does not cache it locally:
>>> with get_pkg_data_fileobj('allsky/allsky_rosat.fits',
... encoding='binary', cache=False) as fobj:
... fcontents = fobj.read()
...
Downloading http://data.astropy.org/allsky/allsky_rosat.fits [Done]