Quickstart¶
This guide will help you get started with fsspec-utils
by demonstrating how to create and interact with a directory-based filesystem for local paths.
Installation¶
First, ensure you have fsspec-utils
installed.
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 os
andfrom fsspec_utils import filesystem
: We import the necessary modules.os
is used here to ensure the local directory exists, andfilesystem
is 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_data
directory if it doesn't already exist.fs_dir_local = filesystem(local_dir_path, dirfs=True)
: This is wherefsspec-utils
comes into play. We create aDirFileSystem
instance pointing to our local directory. Thedirfs=True
argument is crucial for enabling directory-level operations.fs_dir_local.ls('/')
: We use thels
method of ourfs_dir_local
object to list the contents of the root of ourmy_local_data
directory. Initially, it will be empty.fs_dir_local.open("test_file.txt", "w")
: We demonstrate writing a file within ourDirFileSystem
using theopen
method, 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
.