Hub Python 庫文件

檔案系統 API

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

檔案系統 API

HfFileSystem 類提供了一個基於 fsspec 的 Hugging Face Hub Python 檔案介面。

HfFileSystem

HfFileSystem 基於 fsspec,因此與它提供的大多數 API 相容。有關更多詳細資訊,請檢視我們的指南和 fsspec 的API 參考

class huggingface_hub.HfFileSystem

< >

( *args **kwargs )

引數

訪問遠端 Hugging Face Hub 倉庫,如同訪問本地檔案系統。

HfFileSystem 提供了 fsspec 相容性,這對於需要它的庫(例如,直接使用 pandas 讀取 Hugging Face 資料集)非常有用。但是,由於此相容層會引入額外的開銷。為了獲得更好的效能和可靠性,建議儘可能使用 HfApi 方法。

用法

>>> from huggingface_hub import HfFileSystem

>>> fs = HfFileSystem()

>>> # List files
>>> fs.glob("my-username/my-model/*.bin")
['my-username/my-model/pytorch_model.bin']
>>> fs.ls("datasets/my-username/my-dataset", detail=False)
['datasets/my-username/my-dataset/.gitattributes', 'datasets/my-username/my-dataset/README.md', 'datasets/my-username/my-dataset/data.json']

>>> # Read/write files
>>> with fs.open("my-username/my-model/pytorch_model.bin") as f:
...     data = f.read()
>>> with fs.open("my-username/my-model/pytorch_model.bin", "wb") as f:
...     f.write(data)

__init__

< >

( *args endpoint: typing.Optional[str] = None token: typing.Union[bool, str, NoneType] = None **storage_options )

cp_file

< >

( path1: str path2: str revision: typing.Optional[str] = None **kwargs )

引數

  • path1 (str) — 源路徑。
  • path2 (str) — 目標路徑。
  • revision (str, 可選) — 要從中複製的 Git 版本。

在倉庫內部或倉庫之間複製檔案。

注意:如果可能,請使用 HfApi.upload_file() 以獲得更好的效能。

exists

< >

( path **kwargs ) bool

引數

  • path (str) — 要檢查的路徑。

返回

布林值

如果檔案存在,則為 True,否則為 False。

檢查檔案是否存在。

有關更多詳細資訊,請參閱 fsspec 文件

注意:如果可能,請使用 HfApi.file_exists() 以獲得更好的效能。

find

< >

( path: str maxdepth: typing.Optional[int] = None withdirs: bool = False detail: bool = False refresh: bool = False revision: typing.Optional[str] = None **kwargs ) Union[List[str], Dict[str, Dict[str, Any]]]

引數

  • path (str) — 列出檔案的根路徑。
  • maxdepth (int, 可選) — 深入子目錄的最大深度。
  • withdirs (bool, 可選) — 在輸出中包含目錄路徑。預設為 False。
  • detail (bool, 可選) — 如果為 True,則返回一個將路徑對映到檔案資訊的字典。預設為 False。
  • refresh (bool, 可選) — 如果為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 可選) — 要從中列出的 Git 版本。

返回

Union[List[str], Dict[str, Dict[str, Any]]]

路徑列表或檔案資訊字典。

列出路徑下的所有檔案。

有關更多詳細資訊,請參閱 fsspec 文件

get_file

< >

( rpath lpath callback = <fsspec.callbacks.NoOpCallback object at 0x7fd63b7cafb0> outfile = None **kwargs )

引數

  • rpath (str) — 要下載的遠端路徑。
  • lpath (str) — 要下載到的本地路徑。
  • callback (Callback, 可選) — 可選的回撥函式,用於跟蹤下載進度。預設為無回撥。
  • outfile (IO, 可選) — 可選的檔案類物件,用於寫入。如果提供,lpath 將被忽略。

將單個遠端檔案複製到本地。

注意:如果可能,請使用 HfApi.hf_hub_download() 以獲得更好的效能。

glob

< >

( path **kwargs ) List[str]

引數

  • path (str) — 要匹配的路徑模式。

返回

List[str]

匹配模式的路徑列表。

透過 glob 匹配查詢檔案。

有關更多詳細資訊,請參閱 fsspec 文件

info

< >

( path: str refresh: bool = False revision: typing.Optional[str] = None **kwargs ) Dict[str, Any]

引數

  • path (str) — 要獲取資訊的路徑。
  • refresh (bool, 可選) — 如果為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 可選) — 要獲取資訊的 Git 版本。

返回

Dict[str, Any]

包含檔案資訊(型別、大小、提交資訊等)的字典。

獲取檔案或目錄的資訊。

有關更多詳細資訊,請參閱 fsspec 文件

注意:如果可能,請使用 HfApi.get_paths_info()HfApi.repo_info() 以獲得更好的效能。

invalidate_cache

< >

( path: typing.Optional[str] = None )

引數

  • path (str, 可選) — 要從快取中清除的路徑。如果未提供,則清除整個快取。

清除給定路徑的快取。

有關更多詳細資訊,請參閱 fsspec 文件

isdir

< >

( path ) bool

引數

  • path (str) — 要檢查的路徑。

返回

布林值

如果路徑是目錄,則為 True,否則為 False。

檢查路徑是否為目錄。

有關更多詳細資訊,請參閱 fsspec 文件

isfile

< >

( path ) bool

引數

  • path (str) — 要檢查的路徑。

返回

布林值

如果路徑是檔案,則為 True,否則為 False。

檢查路徑是否為檔案。

有關更多詳細資訊,請參閱 fsspec 文件

ls

< >

( path: str detail: bool = True refresh: bool = False revision: typing.Optional[str] = None **kwargs ) List[Union[str, Dict[str, Any]]]

引數

  • path (str) — 目錄的路徑。
  • detail (bool, 可選) — 如果為 True,則返回包含檔案資訊的字典列表。如果為 False,則返回檔案路徑列表。預設為 True。
  • refresh (bool, 可選) — 如果為 True,則繞過快取並獲取最新資料。預設為 False。
  • revision (str, 可選) — 要從中列出的 Git 版本。

返回

List[Union[str, Dict[str, Any]]]

檔案路徑列表(如果 detail=False)或檔案資訊字典列表(如果 detail=True)。

列出目錄內容。

有關更多詳細資訊,請參閱 fsspec 文件

注意:如果可能,請使用 HfApi.list_repo_tree() 以獲得更好的效能。

modified

< >

( path: str **kwargs ) datetime

引數

  • path (str) — 檔案的路徑。

返回

datetime

檔案的最後提交日期。

獲取檔案的最後修改時間。

有關更多詳細資訊,請參閱 fsspec 文件

resolve_path

< >

( path: str revision: typing.Optional[str] = None ) HfFileSystemResolvedPath

引數

  • path (str) — 要解析的路徑。
  • revision (str, 可選) — 要解析的倉庫版本。預設為路徑中指定的版本。

返回

HfFileSystemResolvedPath

已解析的路徑資訊,包含 repo_typerepo_idrevisionpath_in_repo

引發

ValueErrorNotImplementedError

  • ValueError — 如果路徑包含衝突的版本資訊。
  • NotImplementedError — 如果嘗試列出倉庫。

將 Hugging Face 檔案系統路徑解析為其元件。

rm

< >

( path: str recursive: bool = False maxdepth: typing.Optional[int] = None revision: typing.Optional[str] = None **kwargs )

引數

  • path (str) — 要刪除的路徑。
  • recursive (bool, 可選) — 如果為 True,則刪除目錄及其所有內容。預設為 False。
  • maxdepth (int, 可選) — 遞迴刪除時要訪問的最大子目錄數。
  • revision (str, 可選) — 要從中刪除的 git 版本。

從儲存庫中刪除檔案。

有關更多詳細資訊,請參閱 fsspec 文件

注意:如果可能,請使用 HfApi.delete_file() 以獲得更好的效能。

url

< >

( 路徑: str ) str

引數

  • path (str) — 獲取 URL 的路徑。

返回

字串

訪問 Hub 上檔案或目錄的 HTTP URL。

獲取給定路徑的 HTTP URL。

walk

< >

( 路徑: str *args **kwargs ) Iterator[Tuple[str, List[str], List[str]]]

引數

  • 路徑 (str) — 列出檔案的根路徑。

返回

Iterator[Tuple[str, List[str], List[str]]]

一個由(路徑、目錄名列表、檔名列表)元組組成的迭代器。

返回給定路徑下的所有檔案。

有關更多詳細資訊,請參閱 fsspec 文件

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.