Folder#
A Folder represents a specified location for organizing and storing other Runhouse primitives across various systems.
Folder Factory Method#
- runhouse.folder(name: str | None = None, path: str | Path | None = None, system: str | Cluster | None = None, dryrun: bool = False, local_mount: bool = False, data_config: Dict | None = None) Folder [source]#
Creates a Runhouse folder object, which can be used to interact with the folder at the given path.
- Parameters:
name (Optional[str]) β Name to give the folder, to be re-used later on.
path (Optional[str or Path]) β Path (or path) that the folder is located at.
system (Optional[str or Cluster]) β File system or cluster name. If providing a file system this must be one of: [
file
,github
,sftp
,ssh
,s3
,gs
,azure
]. We are working to add additional file system support.dryrun (bool) β Whether to create the Folder if it doesnβt exist, or load a Folder object as a dryrun. (Default:
False
)local_mount (bool) β Whether or not to mount the folder locally. (Default:
False
)data_config (Optional[Dict]) β The data config to pass to the underlying fsspec handler.
- Returns:
The resulting folder.
- Return type:
Example
>>> import runhouse as rh >>> rh.folder(name='training_imgs', path='remote_directory/images', system='s3').save()
>>> # Load folder from above >>> reloaded_folder = rh.folder(name="training_imgs")
Folder Class#
- class runhouse.Folder(name: str | None = None, path: str | None = None, system: Cluster | str | None = None, dryrun: bool = False, local_mount: bool = False, data_config: Dict | None = None, **kwargs)[source]#
- contains(name_or_path) bool [source]#
Whether path of a Folder exists locally.
Example
>>> my_folder = rh.folder("local/folder/path") >>> in_folder = my_folder.contains("filename")
- destination_folder(dest_path: str, dest_system: str | None = 'file', data_config: dict | None = None)[source]#
Returns a new Folder object pointing to the destination folder.
- exists_in_system()[source]#
Whether the folder exists in the filesystem.
Example
>>> exists_on_system = my_folder.exists_in_system()
- property fsspec_url#
Generate the FSSpec URL using the file system and path of the folder
- get(name, mode='rb', encoding=None)[source]#
Returns the contents of a file as a string or bytes.
Example
>>> contents = my_folder.get(file_name)
- is_local()[source]#
Whether the folder is on the local filesystem.
Example
>>> is_local = my_folder.is_local()
- is_writable()[source]#
Whether the folder is writable.
Example
>>> if my_folder.is_writable(): >>> ....
- locate(name_or_path) -> (<class 'str'>, <class 'str'>)[source]#
Locate the local path of a Folder given an rns path.
Example
>>> my_folder = rh.folder("local/folder/path") >>> local_path = my_folder.locate("file_name")
- ls(full_paths: bool = True, sort: bool = False) list [source]#
List the contents of the folder.
- Parameters:
full_paths (Optional[bool]) β Whether to list the full paths of the folder contents. Defaults to
True
.sort (Optional[bool]) β Whether to sort the folder contents by time modified. Defaults to
False
.
- mount(path: str | None = None, tmp: bool = False) str [source]#
Mount the folder locally.
Example
remote_folder = rh.folder(βfolder/pathβ, system=βs3β) local_mount = remote_folder.mount()
- mv(system, path: str | None = None, data_config: dict | None = None) None [source]#
Move the folder to a new filesystem or cluster.
Example
>>> folder = rh.folder(path="local/path") >>> folder.mv(my_cluster) >>> folder.mv("s3", "s3_bucket/path")
- open(name, mode='rb', encoding=None)[source]#
Returns an fsspec file, which must be used as a content manager to be opened.
Example
>>> with my_folder.open('obj_name') as my_file: >>> pickle.load(my_file)
- put(contents, overwrite=False, mode: str = 'wb', write_fn: Callable | None = None)[source]#
Put given contents in folder.
- Parameters:
contents (Dict[str, Any] or Resource or List[Resource]) β Contents to put in folder. Must be a dict with keys being the file names (without full paths) and values being the file-like objects to write, or a Resource object, or a list of Resources.
overwrite (bool) β Whether to dump the file contents as json. By default expects data to be encoded. Defaults to
False
.mode (Optional(str)) β Write mode to use for fsspec. Defaults to
wb
.write_fn (Optional(Callable)) β Function to use for writing file contents. Example: ``write_fn = lambda f, data: json.dump(data, f)
Example
>>> my_folder.put(contents={"filename.txt": data})
- resources(full_paths: bool = False)[source]#
List the resources in the RNS folder.
Example
>>> resources = my_folder.resources()
- rm(contents: list | None = None, recursive: bool = True)[source]#
Delete a folder from the file system. Optionally provide a list of folder contents to delete.
- Parameters:
contents (Optional[List]) β Specific contents to delete in the folder.
recursive (bool) β Delete the folder itself (including all its contents). Defaults to
True
.
Example
>>> my_folder.rm()
- property rns_address#
Traverse up the filesystem until reaching one of the directories in rns_base_folders, then compute the relative path to that.