get_pkg_data_contents#

astropy.utils.data.get_pkg_data_contents(data_name, package=None, encoding=None, cache=True)[源代码]#

从标准位置检索数据文件,并将其内容作为bytes对象返回。

参数:
data_name : strPython :字符串

所需数据文件的名称/位置。以下之一:

  • 源分发中包含的数据文件的名称。路径与调用此函数的模块相关。例如,如果从 astropy.pkname 使用 'data/file.dat' 把文件放进去 astropy/pkgname/data/file.dat . 双点可以用来提升一个级别。在同一个例子中,使用 '../data/file.dat' 得到 astropy/data/file.dat .

  • 如果不存在匹配的本地文件,则会向Astropy数据服务器查询该文件。

  • 像这样的杂凑 compute_hash 可以请求,前缀为“hash/”,例如“hash/34c33b3eb0d56eb9462003af249eff28”。散列将首先在本地搜索,如果没有找到,将查询Astropy数据服务器。

  • 指向其他文件的URL。

package : str ,可选Python:字符串,可选

如果指定,则查找相对于给定包的文件,而不是默认的相对于调用模块的包查找的文件。

encoding : str ,可选Python:字符串,可选

When None (default), returns a file-like object with a read method that 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.

什么时候? 'binary' ,返回一个类似文件的对象,其中 read 方法返回 bytes 物体。

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布尔

如果为True,则文件将下载并保存在本地,或者访问已缓存的本地副本。如果为False,则类似文件的对象将直接访问资源(例如,如果访问了远程URL,则从 urllib.request.urlopen 返回)。

返回:
contents : bytes字节

作为bytes对象的文件的完整内容。

加薪:
urllib.error.URLError

如果找不到远程文件。

OSError

如果写入或读取本地文件时出现问题。

参见

get_pkg_data_fileobj

返回包含数据的类似文件的对象

get_pkg_data_filename

返回包含数据的文件的本地名称