API reference

LocalFileSystem

Local filesystem backed by the Rust LocalFs implementation.

LocalFile

File-like wrapper delegating to the Rust RustLocalFile.

S3FileSystem

S3 filesystem backed by the Rust S3Fs implementation.

S3File

File-like wrapper delegating to the Rust RustS3File.

FileInfo

Python-visible file metadata, analogous to the dict returned by fsspec's info().

FileType

Python-visible enum for file types.

Filesystems

class fsspec_rs.LocalFileSystem(*args, **kwargs)[source]

Bases: AbstractFileSystem

Local filesystem backed by the Rust LocalFs implementation.

The protocol is registered as ("file-rs", "local-rs") so it can coexist with the pure-Python fsspec.implementations.local.LocalFileSystem which owns the ("file", "local") protocol names.

protocol: ClassVar[str | tuple[str, ...]] = ('file-rs', 'local-rs')
local_file = True
unstrip_protocol(path)[source]

Format FS-specific path to generic, including protocol

ls(path: str, detail: bool = True, **kwargs)[source]

List objects at path.

This should include subdirectories and files at that location. The difference between a file and a directory must be clear when details are requested.

The specific keys, or perhaps a FileInfo class, or similar, is TBD, but must be consistent across implementations. Must include:

  • full path to the entry (without protocol)

  • size of the entry, in bytes. If the value cannot be determined, will be None.

  • type of entry, “file”, “directory” or other

Additional information may be present, appropriate to the file-system, e.g., generation, checksum, etc.

May use refresh=True|False to allow use of self._ls_from_cache to check for a saved listing and avoid calling the backend. This would be common where listing may be expensive.

Parameters:
  • path (str)

  • detail (bool) – if True, gives a list of dictionaries, where each is the same as the result of info(path). If False, gives a list of paths (str).

  • kwargs (may have additional backend-specific options, such as version) – information

Returns:

  • List of strings if detail is False, or list of directory information

  • dicts if detail is True.

info(path: str, **kwargs)[source]

Give details of entry at path

Returns a single dictionary, with exactly the same information as ls would with detail=True.

The default implementation calls ls and could be overridden by a shortcut. kwargs are passed on to `ls().

Some file systems might not be able to measure the file’s size, in which case, the returned dict will include 'size': None.

Returns:

  • dict with keys (name (full path in the FS), size (in bytes), type (file,)

  • directory, or something else) and other FS-specific keys.

mkdir(path: str, create_parents: bool = True, **kwargs)[source]

Create directory entry at path

For systems that don’t have true directories, may create an for this instance only and not touch the real filesystem

Parameters:
  • path (str) – location

  • create_parents (bool) – if True, this is equivalent to makedirs

  • kwargs – may be permissions, etc.

makedirs(path: str, exist_ok: bool = False)[source]

Recursively make directories

Creates directory at path and any intervening required directories. Raises exception if, for instance, the path already exists but is a file.

Parameters:
  • path (str) – leaf directory name

  • exist_ok (bool (False)) – If False, will error if the target already exists

rmdir(path: str)[source]

Remove a directory, if empty

rm_file(path: str)[source]

Delete a file

rm(path, recursive: bool = False, maxdepth=None)[source]

Delete files.

Parameters:
  • path (str or list of str) – File(s) to delete.

  • recursive (bool) – If file(s) are directories, recursively delete contents and then also remove the directory

  • maxdepth (int or None) – Depth to pass to walk for finding files to delete, if recursive. If None, there will be no limit and infinite recursion may be possible.

cp_file(path1: str, path2: str, **kwargs)[source]
copy(path1: str, path2: str, recursive: bool = False, **kwargs)[source]

Copy within two locations in the filesystem

on_error“raise”, “ignore”

If raise, any not-found exceptions will be raised; if ignore any not-found exceptions will cause the path to be skipped; defaults to raise unless recursive is true, where the default is ignore

mv(path1: str, path2: str, **kwargs)[source]

Move file(s) from one location to another

exists(path: str, **kwargs) bool[source]

Is there a file at the given path

isdir(path: str) bool[source]

Is this entry directory-like?

isfile(path: str) bool[source]

Is this entry file-like?

size(path: str) int[source]

Size in bytes of file

cat_file(path: str, start=None, end=None, **kwargs) bytes[source]

Get the content of a file

Parameters:
  • path (URL of file on this filesystems)

  • start (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively

  • end (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively

  • kwargs (passed to open().)

pipe_file(path: str, value: bytes, **kwargs)[source]

Set the bytes of given file

head(path: str, size: int = 1024) bytes[source]

Get the first size bytes from file

tail(path: str, size: int = 1024) bytes[source]

Get the last size bytes from file

touch(path: str, truncate: bool = True, **kwargs)[source]

Create empty file, or update timestamp

Parameters:
  • path (str) – file location

  • truncate (bool) – If True, always set file size to 0; if False, update timestamp and leave file unchanged, if backend allows this

walk(path: str, maxdepth=None, topdown: bool = True, **kwargs)[source]

Return all files under the given path.

List all files, recursing into subdirectories; output is iterator-style, like os.walk(). For a simple list of files, find() is available.

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect. (see os.walk)

Note that the “files” outputted will include anything that is not a directory, such as links.

Parameters:
  • path (str) – Root to recurse into

  • maxdepth (int) – Maximum recursion depth. None means limitless, but not recommended on link-based file-systems.

  • topdown (bool (True)) – Whether to walk the directory tree from the top downwards or from the bottom upwards.

  • on_error ("omit", "raise", a callable) – if omit (default), path with exception will simply be empty; If raise, an underlying exception will be raised; if callable, it will be called with a single OSError instance as argument

  • kwargs (passed to ls)

find(path: str, maxdepth=None, withdirs: bool = False, **kwargs)[source]

List all files below path.

Like posix find command without conditions

Parameters:
  • path (str)

  • maxdepth (int or None) – If not None, the maximum number of levels to descend

  • withdirs (bool) – Whether to include directory paths in the output. This is True when used by glob, but users usually only want files.

  • ls. (kwargs are passed to)

du(path: str, total: bool = True, maxdepth=None, **kwargs)[source]

Space used by files and optionally directories within a path

Directory size does not include the size of its contents.

Parameters:
  • path (str)

  • total (bool) – Whether to sum all the file sizes

  • maxdepth (int or None) – Maximum number of directory levels to descend, None for unlimited.

  • withdirs (bool) – Whether to include directory paths in the output.

  • kwargs (passed to find)

Returns:

  • Dict of {path (size} if total=False, or int otherwise, where numbers)

  • refer to bytes used.

read_text(path: str, encoding=None, errors=None, newline=None, **kwargs) str[source]

Get the contents of the file as a string.

Parameters:
  • path (str) – URL of file on this filesystems

  • encoding (same as open.)

  • errors (same as open.)

  • newline (same as open.)

write_text(path: str, value: str, encoding=None, errors=None, newline=None, **kwargs)[source]

Write the text to the given file.

An existing file will be overwritten.

Parameters:
  • path (str) – URL of file on this filesystems

  • value (str) – Text to write.

  • encoding (same as open.)

  • errors (same as open.)

  • newline (same as open.)

get_file(rpath: str, lpath: str, **kwargs)[source]

Copy single remote file to local

put_file(lpath: str, rpath: str, **kwargs)[source]

Copy single file to remote

class fsspec_rs.S3FileSystem(*args, **kwargs)[source]

Bases: AbstractFileSystem

S3 filesystem backed by the Rust S3Fs implementation.

The protocol is registered as "s3-rs" so it can coexist with the pure-Python s3fs.S3FileSystem which owns the "s3" protocol.

Credential resolution (matching s3fs conventions):
  1. Explicit keyword arguments (key, secret, endpoint_url, …)

  2. Values from fsspec.config.conf["s3"] (populated from FSSPEC_S3_* environment variables)

  3. Falls through to object_store’s own credential chain (IAM roles, etc.)

protocol: ClassVar[str | tuple[str, ...]] = ('s3-rs',)
unstrip_protocol(path)[source]

Reconstruct s3-rs://bucket/key from bucket/key.

ls(path: str, detail: bool = True, **kwargs)[source]

List objects at path.

This should include subdirectories and files at that location. The difference between a file and a directory must be clear when details are requested.

The specific keys, or perhaps a FileInfo class, or similar, is TBD, but must be consistent across implementations. Must include:

  • full path to the entry (without protocol)

  • size of the entry, in bytes. If the value cannot be determined, will be None.

  • type of entry, “file”, “directory” or other

Additional information may be present, appropriate to the file-system, e.g., generation, checksum, etc.

May use refresh=True|False to allow use of self._ls_from_cache to check for a saved listing and avoid calling the backend. This would be common where listing may be expensive.

Parameters:
  • path (str)

  • detail (bool) – if True, gives a list of dictionaries, where each is the same as the result of info(path). If False, gives a list of paths (str).

  • kwargs (may have additional backend-specific options, such as version) – information

Returns:

  • List of strings if detail is False, or list of directory information

  • dicts if detail is True.

info(path: str, **kwargs)[source]

Give details of entry at path

Returns a single dictionary, with exactly the same information as ls would with detail=True.

The default implementation calls ls and could be overridden by a shortcut. kwargs are passed on to `ls().

Some file systems might not be able to measure the file’s size, in which case, the returned dict will include 'size': None.

Returns:

  • dict with keys (name (full path in the FS), size (in bytes), type (file,)

  • directory, or something else) and other FS-specific keys.

mkdir(path: str, create_parents: bool = True, **kwargs)[source]

Create directory entry at path

For systems that don’t have true directories, may create an for this instance only and not touch the real filesystem

Parameters:
  • path (str) – location

  • create_parents (bool) – if True, this is equivalent to makedirs

  • kwargs – may be permissions, etc.

rmdir(path: str)[source]

Remove a directory, if empty

rm_file(path: str)[source]

Delete a file

rm(path, recursive: bool = False, maxdepth=None)[source]

Delete files.

Parameters:
  • path (str or list of str) – File(s) to delete.

  • recursive (bool) – If file(s) are directories, recursively delete contents and then also remove the directory

  • maxdepth (int or None) – Depth to pass to walk for finding files to delete, if recursive. If None, there will be no limit and infinite recursion may be possible.

cp_file(path1: str, path2: str, **kwargs)[source]
exists(path: str, **kwargs) bool[source]

Is there a file at the given path

isdir(path: str) bool[source]

Is this entry directory-like?

isfile(path: str) bool[source]

Is this entry file-like?

size(path: str) int[source]

Size in bytes of file

cat_file(path: str, start=None, end=None, **kwargs) bytes[source]

Get the content of a file

Parameters:
  • path (URL of file on this filesystems)

  • start (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively

  • end (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively

  • kwargs (passed to open().)

pipe_file(path: str, value: bytes, **kwargs)[source]

Set the bytes of given file

head(path: str, size: int = 1024) bytes[source]

Get the first size bytes from file

tail(path: str, size: int = 1024) bytes[source]

Get the last size bytes from file

walk(path: str, maxdepth=None, topdown: bool = True, **kwargs)[source]

Return all files under the given path.

List all files, recursing into subdirectories; output is iterator-style, like os.walk(). For a simple list of files, find() is available.

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect. (see os.walk)

Note that the “files” outputted will include anything that is not a directory, such as links.

Parameters:
  • path (str) – Root to recurse into

  • maxdepth (int) – Maximum recursion depth. None means limitless, but not recommended on link-based file-systems.

  • topdown (bool (True)) – Whether to walk the directory tree from the top downwards or from the bottom upwards.

  • on_error ("omit", "raise", a callable) – if omit (default), path with exception will simply be empty; If raise, an underlying exception will be raised; if callable, it will be called with a single OSError instance as argument

  • kwargs (passed to ls)

find(path: str, maxdepth=None, withdirs: bool = False, **kwargs)[source]

List all files below path.

Like posix find command without conditions

Parameters:
  • path (str)

  • maxdepth (int or None) – If not None, the maximum number of levels to descend

  • withdirs (bool) – Whether to include directory paths in the output. This is True when used by glob, but users usually only want files.

  • ls. (kwargs are passed to)

read_text(path: str, encoding=None, errors=None, newline=None, **kwargs) str[source]

Get the contents of the file as a string.

Parameters:
  • path (str) – URL of file on this filesystems

  • encoding (same as open.)

  • errors (same as open.)

  • newline (same as open.)

Open files

class fsspec_rs.LocalFile(fs: LocalFileSystem, path: str, mode: str = 'rb', cache_type=None, block_size=None, **kwargs)[source]

Bases: AbstractBufferedFile

File-like wrapper delegating to the Rust RustLocalFile.

read(length: int = -1) bytes[source]

Return data from cache, or fetch pieces as necessary

Parameters:

length (int (-1)) – Number of bytes to read; if <0, all remaining bytes.

write(data: bytes) int[source]

Write data to buffer.

Buffer only sent on flush() or if buffer is greater than or equal to blocksize.

Parameters:

data (bytes) – Set of bytes to be written.

seek(loc: int, whence: int = 0) int[source]

Set current file location

Parameters:
  • loc (int) – byte location

  • whence ({0, 1, 2}) – from start of file, current location or end of file, resp.

tell() int[source]

Current file location

flush(force: bool = False)[source]

Write buffered data to backend store.

Writes the current buffer, if it is larger than the block-size, or if the file is being closed.

Parameters:

force (bool) – When closing, write the last block even if it is smaller than blocks are allowed to be. Disallows further writing to this file.

close()[source]

Close file

Finalizes writes, discards cache

property closed: bool
readable() bool[source]

Whether opened for reading

writable() bool[source]

Whether opened for writing

seekable() bool[source]

Whether is seekable (only in read mode)

class fsspec_rs.S3File(fs: S3FileSystem, path: str, mode: str = 'rb', cache_type=None, block_size=None, **kwargs)[source]

Bases: AbstractBufferedFile

File-like wrapper delegating to the Rust RustS3File.

read(length: int = -1) bytes[source]

Return data from cache, or fetch pieces as necessary

Parameters:

length (int (-1)) – Number of bytes to read; if <0, all remaining bytes.

write(data: bytes) int[source]

Write data to buffer.

Buffer only sent on flush() or if buffer is greater than or equal to blocksize.

Parameters:

data (bytes) – Set of bytes to be written.

seek(loc: int, whence: int = 0) int[source]

Set current file location

Parameters:
  • loc (int) – byte location

  • whence ({0, 1, 2}) – from start of file, current location or end of file, resp.

tell() int[source]

Current file location

flush(force: bool = False)[source]

Write buffered data to backend store.

Writes the current buffer, if it is larger than the block-size, or if the file is being closed.

Parameters:

force (bool) – When closing, write the last block even if it is smaller than blocks are allowed to be. Disallows further writing to this file.

close()[source]

Close file

Finalizes writes, discards cache

property closed: bool
readable() bool[source]

Whether opened for reading

writable() bool[source]

Whether opened for writing

seekable() bool[source]

Whether is seekable (only in read mode)

Metadata types

class fsspec_rs.FileInfo(name, size=0, file_type=None)

Bases: object

Python-visible file metadata, analogous to the dict returned by fsspec’s info().

file_type

The file type as a string (“file”, “directory”, “other”).

is_dir()

Whether this is a directory.

is_file()

Whether this is a regular file.

name

The full path name.

size

Size in bytes.

to_dict()

Convert to a Python dict matching fsspec’s info() format.

class fsspec_rs.FileType

Bases: object

Python-visible enum for file types.

as_str()

Return the string representation used by fsspec (“file”, “directory”, “other”).

static directory()

Create a FileType representing a directory.

static file()

Create a FileType representing a regular file.

static other()

Create a FileType representing an other entry.