numpy.DataSource

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

通用数据源文件(文件、HTTP、FTP等)。

数据源可以是本地文件或远程文件/URL。文件也可以压缩或解压缩。数据源隐藏了下载文件的一些低级细节,允许您简单地传递有效的文件路径(或URL)并获取文件对象。

参数
destpathstr或none,可选

下载源文件以供使用的目录的路径。如果 destpath 如果为“无”,则将创建临时目录。默认路径是当前目录。

笔记

URL需要方案字符串 (http:// )如果不使用,它们将失败:

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

删除数据源时会删除临时目录。

实例

>>> ds = np.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.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 [(路径)]

返回数据源目录中文件的绝对路径。

exists [(路径)]

测试路径是否存在。

open [路径] [, mode, encoding, newline] )

打开并返回类似对象的文件。