Quickstart¶
DOCUMENTATION FOR LEGACY CODE
This guide demonstrates fsspec-utils for reference purposes.
For new projects, use fsspeckit instead.
Installation¶
Basic Usage: Local Directory FileSystem¶
fsspec-utils simplifies working with various file systems by providing a unified interface. Here, we'll create a DirFileSystem for a local directory.
The filesystem function from fsspec_utils allows you to instantiate a file system object. By setting dirfs=True, you indicate that you want a directory-based filesystem, which treats directories as files themselves.
Let's create a local directory and then instantiate a DirFileSystem for it:
Explanation¶
import osandfrom fsspec_utils import filesystem: We import the necessary modules.osis used here to ensure the local directory exists, andfilesystemis the core function fromfsspec-utils.local_dir_path = "./my_local_data/": We define a relative path for our local directory.os.makedirs(local_dir_path, exist_ok=True): This line creates themy_local_datadirectory if it doesn't already exist.fs_dir_local = filesystem(local_dir_path, dirfs=True): This is wherefsspec-utilscomes into play. We create aDirFileSysteminstance pointing to our local directory. Thedirfs=Trueargument is crucial for enabling directory-level operations.fs_dir_local.ls('/'): We use thelsmethod of ourfs_dir_localobject to list the contents of the root of ourmy_local_datadirectory. Initially, it will be empty.fs_dir_local.open("test_file.txt", "w"): We demonstrate writing a file within ourDirFileSystemusing theopenmethod, similar to Python's built-inopen.fs_dir_local.open("test_file.txt", "r"): We demonstrate reading the content of the file we just created.fs_dir_local.rm("test_file.txt")andos.rmdir(local_dir_path): Finally, we clean up by removing the created file and the directory.
This example provides a basic overview of how to use fsspec-utils to interact with a local directory as a filesystem. The same filesystem function can be used for various other storage backends like S3, GCS, HDFS, etc., by simply changing the path and providing appropriate storage_options.