numpy.lib.npyio.DataSource#

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

一个通用的数据源文件 (file, 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])

打开并返回类文件对象.