numpy.lib.npyio.DataSource#
- class numpy.lib.npyio.DataSource(destpath='.')[源代码]#
一个通用的数据源文件(文件,http,ftp,…).
DataSources 可以是本地文件或远程文件/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'
方法