numpy.lib.npyio.DataSource#

class numpy.lib.npyio.DataSource(destpath='.')[source]#

一个通用的数据源文件(file、http、ftp,…)。

DataSources 可以是本地文件或远程文件/URL。文件也可能是压缩的或未压缩的。DataSource 隐藏了下载文件的一些底层细节,使您只需传入有效的文件路径(或 URL)即可获得文件对象。

参数:
destpathstr or None, optional

用于存放下载后源文件的目录路径。如果 destpath 为 None,将创建一个临时目录。默认路径是当前目录。

方法

abspath(path)

返回 DataSource 目录中文件的绝对路径。

exists(path)

测试路径是否存在。

open(path[, mode, encoding, newline])

打开并返回类文件对象。

备注

URL 必须使用方案字符串(例如 http://),否则会失败

>>> repos = np.lib.npyio.DataSource()
>>> repos.exists('www.google.com/index.html')
False
>>> repos.exists('http://www.google.com/index.html')
True

当 DataSource 被删除时,临时目录也会被删除。

示例

>>> ds = np.lib.npyio.DataSource('/home/guido')
>>> urlname = 'http://www.google.com/'
>>> gfile = ds.open('http://www.google.com/')
>>> ds.abspath(urlname)
'/home/guido/www.google.com/index.html'

>>> ds = np.lib.npyio.DataSource(None)  # use with temporary file
>>> ds.open('/home/guido/foobar.txt')
<open file '/home/guido.foobar.txt', mode 'r' at 0x91d4430>
>>> ds.abspath('/home/guido/foobar.txt')
'/tmp/.../home/guido/foobar.txt'