Quickstart¶
This guide will help you get started with fsspeckit by demonstrating how to create and interact with a directory-based filesystem for local paths.
Installation¶
First, ensure you have fsspeckit installed.
Basic Usage: Local Directory FileSystem¶
fsspeckit 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 fsspeckit 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 fsspeckit import filesystem: We import the necessary modules.osis used here to ensure the local directory exists, andfilesystemis the core function fromfsspeckit.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 wherefsspeckitcomes 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 fsspeckit 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.