numpy.lib.npyio.DataSource#

class numpy.lib.npyio.DataSource(destpath='.')[源代码]#

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

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

参数:
destpathstr 或 None,可选

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

备注

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'

方法

abspath(path)

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

exists(path)

测试路径是否存在。

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

打开并返回一个类文件对象。