filesystem
Filesystem operations on a linked LucidLink filespace.
Provides file, directory, metadata, and locking operations. Accessed via filespace.fs after linking to a filespace.
lucidlink.filesystem.FileHandle
FileHandle(filesystem: 'Filesystem', path: str, mode: str)Context manager for file handles (legacy API).
Provides Pythonic file operations with automatic handle cleanup. Use with 'with' statement to ensure handles are properly closed.
Example
with fs.open_legacy("/path/to/file.txt", "r") as fh:data = fh.read()# Handle automatically closed
closed: bool
Check if the file handle is closed.
mode: str
Get the file open mode.
path: str
Get the file path.
close() -> None
Close the file handle. Safe to call multiple times.
read(size: int = -1, offset: int = 0) -> bytes
Read data from the file.
Parameters
Parameter | Description |
|---|---|
| Number of bytes to read (-1 = read entire file) |
| Byte offset to start reading from |
size
Description
Number of bytes to read (-1 = read entire file)
offset
Description
Byte offset to start reading from
Returns
Bytes read from the file
write(data: bytes, offset: int = 0) -> None
Write data to the file.
Parameters
Parameter | Description |
|---|---|
| Bytes to write to the file |
| Byte offset to start writing at |
data
Description
Bytes to write to the file
offset
Description
Byte offset to start writing at
lucidlink.filesystem.Filesystem
Filesystem()Filesystem operations on a linked LucidLink filespace.
Provides file, directory, metadata, and locking operations. Obtained via the filespace.fs property after linking.
Example
filespace = workspace.link_filespace(name="production-data")entries = filespace.fs.read_dir("/")filespace.fs.create_dir("/new-folder")with filespace.fs.open("/file.txt", "wb") as f:f.write(b"data")
create(path: str) -> int
Create a new file and return a handle ID.
Use open() with mode="w" for most cases. This is a low-level API.
Parameters
Parameter | Description |
|---|---|
| File path to create |
path
Description
File path to create
Returns
File handle ID (use with read/write/close methods)
Raises
Exception | Condition |
|---|---|
| If file already exists |
| If no create permission |
FileExistsError
Condition
If file already exists
PermissionError
Condition
If no create permission
create_dir(path: str) -> None
Create a directory.
Parameters
Parameter | Description |
|---|---|
| Directory path to create |
path
Description
Directory path to create
Raises
Exception | Condition |
|---|---|
| If the parent directory does not exist |
| If directory already exists |
| If no create permission |
FileNotFoundError
Condition
If the parent directory does not exist
FileExistsError
Condition
If directory already exists
PermissionError
Condition
If no create permission
create_symlink(target_path: str, link_path: str) -> None
Create a symbolic link.
Parameters
Parameter | Description |
|---|---|
| Path the symlink points to |
| Path of the symlink to create |
target_path
Description
Path the symlink points to
link_path
Description
Path of the symlink to create
Raises
Exception | Condition |
|---|---|
| If link_path already exists |
| If no create permission |
FileExistsError
Condition
If link_path already exists
PermissionError
Condition
If no create permission
delete(path: str) -> None
Delete a file.
Parameters
Parameter | Description |
|---|---|
| File path to delete |
path
Description
File path to delete
Raises
Exception | Condition |
|---|---|
| If file doesn't exist |
| If path is a directory |
| If no delete permission |
FileNotFoundError
Condition
If file doesn't exist
IsADirectoryError
Condition
If path is a directory
PermissionError
Condition
If no delete permission
delete_dir(path: str, recursive: bool = False) -> None
Delete a directory.
Parameters
Parameter | Description |
|---|---|
| Directory path to delete |
| If True, delete non-empty directories (default: False) |
path
Description
Directory path to delete
recursive
Description
If True, delete non-empty directories (default: False)
Raises
Exception | Condition |
|---|---|
| If directory doesn't exist |
| If path is not a directory |
| If directory is not empty and recursive=False |
| If no delete permission |
FileNotFoundError
Condition
If directory doesn't exist
NotADirectoryError
Condition
If path is not a directory
OSError
Condition
If directory is not empty and recursive=False
PermissionError
Condition
If no delete permission
dir_exists(path: str) -> bool
Check if a directory exists.
Parameters
Parameter | Description |
|---|---|
| Directory path to check |
path
Description
Directory path to check
Returns
True if directory exists, False otherwise
file_exists(path: str) -> bool
Check if a file exists.
Parameters
Parameter | Description |
|---|---|
| File path to check |
path
Description
File path to check
Returns
True if file exists, False otherwise
get_entry(path: str) -> DirEntry
Get metadata for a file or directory.
Parameters
Parameter | Description |
|---|---|
| Path to get metadata for |
path
Description
Path to get metadata for
Returns
DirEntry with entry information
Raises
Exception | Condition |
|---|---|
| If path doesn't exist |
FileNotFoundError
Condition
If path doesn't exist
get_size() -> FilespaceSize
Get filespace size information.
Returns
FilespaceSize with entries, data, storage, and external file info
Raises
Exception | Condition |
|---|---|
| If operation fails |
RuntimeError
Condition
If operation fails
get_statistics() -> FilespaceStatistics
Get filespace statistics.
Returns
FilespaceStatistics with file/directory counts and size info
Raises
Exception | Condition |
|---|---|
| If operation fails |
RuntimeError
Condition
If operation fails
list_dir(path: str) -> List[str]
List directory contents, returning just filenames (convenience method).
Parameters
Parameter | Description |
|---|---|
| Directory path to list |
path
Description
Directory path to list
Returns
List of filenames (not full paths)
Raises
Exception | Condition |
|---|---|
| If path is not a directory |
| If directory doesn't exist |
NotADirectoryError
Condition
If path is not a directory
FileNotFoundError
Condition
If directory doesn't exist
lock_byte_range(handle_id: int, offset: int, length: int, lock_type: str = 'exclusive', blocking: bool = True) -> bool
Lock a byte range of an open file.
Acquires a lock on the specified byte range. Locks are coordinated through LucidLink services, ensuring proper mutual exclusion across all clients accessing the filespace.
Parameters
Parameter | Description |
|---|---|
| File handle ID from a low-level open operation |
| Start offset of the byte range to lock (0 for whole file) |
| Length of the byte range to lock (use file size for whole file) |
| Lock type (default: "exclusive"): - "exclusive": Exclusive lock (no other locks allowed) - "shared" or "read": Shared read lock (multiple readers allowed) - "write": Protected write lock |
| If True (default), wait until lock is available. If False, return immediately if lock unavailable. |
handle_id
Description
File handle ID from a low-level open operation
offset
Description
Start offset of the byte range to lock (0 for whole file)
length
Description
Length of the byte range to lock (use file size for whole file)
lock_type
Description
Lock type (default: "exclusive"): - "exclusive": Exclusive lock (no other locks allowed) - "shared" or "read": Shared read lock (multiple readers allowed) - "write": Protected write lock
blocking
Description
If True (default), wait until lock is available. If False, return immediately if lock unavailable.
Returns
True if lock was acquired, False if non-blocking and lock unavailable
Raises
Exception | Condition |
|---|---|
| If handle is invalid or lock operation fails |
| If lock_type is invalid |
RuntimeError
Condition
If handle is invalid or lock operation fails
ValueError
Condition
If lock_type is invalid
Example
handle_id = filespace.fs._native.open("/data.db", "r+b")try:if filespace.fs.lock_byte_range(handle_id, 0, 1, "exclusive"):# Perform exclusive operationspassfinally:filespace.fs.unlock_byte_range(handle_id, 0, 1)filespace.fs._native.close(handle_id)
move(src: str, dst: str) -> None
Move/rename a file or directory.
Parameters
Parameter | Description |
|---|---|
| Source path |
| Destination path |
src
Description
Source path
dst
Description
Destination path
Raises
Exception | Condition |
|---|---|
| If source doesn't exist |
| If destination already exists |
| If no move permission |
FileNotFoundError
Condition
If source doesn't exist
FileExistsError
Condition
If destination already exists
PermissionError
Condition
If no move permission
open(path: str, mode: str = 'rb', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, lock_type: str = '') -> Union[LucidFileStream, io.BufferedReader, io.BufferedWriter, io.TextIOWrapper]
Open a file with streaming support.
Returns an io.RawIOBase-compatible stream that works with standard Python libraries and third-party packages (Pandas, LangChain, PyTorch, etc.).
Parameters
Parameter | Description |
|---|---|
| File path to open |
| Open mode (default: 'rb'): - 'r': Read text (default encoding: utf-8) - 'w': Write text (create/truncate, default encoding: utf-8) - 'a': Append text (default encoding: utf-8) - 'r+': Read/write text (default encoding: utf-8) - 'w+': Write/read text (create/truncate, default encoding: utf-8) - 'rb': Read binary - 'wb': Write binary (create/truncate) - 'ab': Append binary - 'r+b': Read/write binary - 'w+b': Write/read binary (create/truncate) - 'a+b': Append/read binary - 'rt': Read text (explicit, same as 'r') - 'wt': Write text (explicit, same as 'w') |
| Buffer size: - -1 (default): Use system default (8192 bytes) - 0: Unbuffered (binary modes only) - 1: Line buffered (text mode only) - >1: Buffer size in bytes |
| Text encoding (e.g., 'utf-8', 'latin-1') Required for text modes ('r', 'w' without 'b') |
| Error handling ('strict', 'ignore', 'replace') |
| Newline handling (None, '', '\n', '\r', '\r\n') |
| Lock type - "" (no lock), "shared" (read), "exclusive" (write). Lock is held for lifetime of file handle and released on close. |
path
Description
File path to open
mode
Description
Open mode (default: 'rb'): - 'r': Read text (default encoding: utf-8) - 'w': Write text (create/truncate, default encoding: utf-8) - 'a': Append text (default encoding: utf-8) - 'r+': Read/write text (default encoding: utf-8) - 'w+': Write/read text (create/truncate, default encoding: utf-8) - 'rb': Read binary - 'wb': Write binary (create/truncate) - 'ab': Append binary - 'r+b': Read/write binary - 'w+b': Write/read binary (create/truncate) - 'a+b': Append/read binary - 'rt': Read text (explicit, same as 'r') - 'wt': Write text (explicit, same as 'w')
buffering
Description
Buffer size: - -1 (default): Use system default (8192 bytes) - 0: Unbuffered (binary modes only) - 1: Line buffered (text mode only) - >1: Buffer size in bytes
encoding
Description
Text encoding (e.g., 'utf-8', 'latin-1') Required for text modes ('r', 'w' without 'b')
errors
Description
Error handling ('strict', 'ignore', 'replace')
newline
Description
Newline handling (None, '', '\n', '\r', '\r\n')
lock_type
Description
Lock type - "" (no lock), "shared" (read), "exclusive" (write). Lock is held for lifetime of file handle and released on close.
Returns
File stream object supporting read, write, seek, tell operations. Compatible with context managers (with statement).
Raises
Exception | Condition |
|---|---|
| If file doesn't exist (read mode) |
| If no access permission |
| If mode is invalid |
FileNotFoundError
Condition
If file doesn't exist (read mode)
PermissionError
Condition
If no access permission
ValueError
Condition
If mode is invalid
Example
# Binary modewith filespace.fs.open("/file.dat", "rb") as f:data = f.read()# Text modewith filespace.fs.open("/file.txt", "rt", encoding="utf-8") as f:for line in f:print(line.strip())# With Pandaswith filespace.fs.open("/data.csv", "rb") as f:df = pd.read_csv(f)# Writingwith filespace.fs.open("/output.txt", "wb") as f:f.write(b"Hello, LucidLink!")# With exclusive locking (SQLite-style)with filespace.fs.open("/db.sqlite", "r+b", lock_type="exclusive") as f:data = f.read()
open_legacy(path: str, mode: str = 'r') -> FileHandle
Open a file using legacy FileHandle API (deprecated).
This method is preserved for backward compatibility. New code should use open() instead, which returns a more capable io.RawIOBase stream.
Parameters
Parameter | Description |
|---|---|
| File path to open |
| Open mode ('r', 'w', 'a') |
path
Description
File path to open
mode
Description
Open mode ('r', 'w', 'a')
Returns
FileHandle context manager
Raises
Exception | Condition |
|---|---|
| If file doesn't exist (read mode) |
| If no access permission |
FileNotFoundError
Condition
If file doesn't exist (read mode)
PermissionError
Condition
If no access permission
read_dir(path: str) -> List[DirEntry]
List directory contents.
Parameters
Parameter | Description |
|---|---|
| Directory path to list |
path
Description
Directory path to list
Returns
List of DirEntry objects with file entry information.
Raises
Exception | Condition |
|---|---|
| If path is not a directory |
| If directory doesn't exist |
| If no read permission |
NotADirectoryError
Condition
If path is not a directory
FileNotFoundError
Condition
If directory doesn't exist
PermissionError
Condition
If no read permission
read_file(path: str) -> bytes
Read entire file contents (convenience method).
Parameters
Parameter | Description |
|---|---|
| File path to read |
path
Description
File path to read
Returns
File contents as bytes
Raises
Exception | Condition |
|---|---|
| If file doesn't exist |
| If no read permission |
FileNotFoundError
Condition
If file doesn't exist
PermissionError
Condition
If no read permission
read_symlink(link_path: str) -> str
Read the target of a symbolic link.
Parameters
Parameter | Description |
|---|---|
| Path of the symlink |
link_path
Description
Path of the symlink
Returns
Target path the symlink points to
Raises
Exception | Condition |
|---|---|
| If link_path does not exist |
| If link_path is not a symlink |
FileNotFoundError
Condition
If link_path does not exist
OSError
Condition
If link_path is not a symlink
truncate(path: str, size: int) -> None
Truncate or extend file to specified size.
Parameters
Parameter | Description |
|---|---|
| File path to truncate |
| New file size in bytes |
path
Description
File path to truncate
size
Description
New file size in bytes
Raises
Exception | Condition |
|---|---|
| If file doesn't exist |
| If no write permission |
| If truncation fails |
FileNotFoundError
Condition
If file doesn't exist
PermissionError
Condition
If no write permission
RuntimeError
Condition
If truncation fails
unlock_all_byte_ranges(handle_id: int) -> None
Unlock all byte ranges on an open file.
Releases all previously acquired locks on the file.
Parameters
Parameter | Description |
|---|---|
| File handle ID from a low-level open operation |
handle_id
Description
File handle ID from a low-level open operation
Raises
Exception | Condition |
|---|---|
| If handle is invalid or unlock fails |
RuntimeError
Condition
If handle is invalid or unlock fails
unlock_byte_range(handle_id: int, offset: int, length: int) -> None
Unlock a byte range of an open file.
Releases a previously acquired lock on the specified byte range.
Parameters
Parameter | Description |
|---|---|
| File handle ID from a low-level open operation |
| Start offset of the byte range to unlock |
| Length of the byte range to unlock |
handle_id
Description
File handle ID from a low-level open operation
offset
Description
Start offset of the byte range to unlock
length
Description
Length of the byte range to unlock
Raises
Exception | Condition |
|---|---|
| If handle is invalid or unlock fails |
RuntimeError
Condition
If handle is invalid or unlock fails
write_file(path: str, data: bytes) -> None
Write data to file, creating it if needed (convenience method).
Parameters
Parameter | Description |
|---|---|
| File path to write |
| Bytes to write |
path
Description
File path to write
data
Description
Bytes to write
Raises
Exception | Condition |
|---|---|
| If no permission to write file |
| If data is not bytes |
PermissionError
Condition
If no permission to write file
TypeError
Condition
If data is not bytes