numpy.lib.npyio.DataSource#

class numpy.lib.npyio.DataSource(destpath='.')[原始碼]#

通用資料來源檔案 (檔案、http、ftp、…)。

DataSource 可以是本機檔案或遠端檔案/URL。檔案也可以是壓縮或未壓縮的。DataSource 隱藏了下載檔案的一些底層細節,讓您只需傳入有效的檔案路徑(或 URL)並取得檔案物件。

參數:
destpathstr 或 None,選用

來源檔案下載後供使用的目錄路徑。如果 destpath 為 None,將建立一個暫存目錄。預設路徑為目前目錄。

註解

URL 需要使用 scheme 字串 (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])

開啟並傳回檔案類物件。