Python's filesystem abstraction layer providing a unified interface for working with different types of filesystems
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Concrete filesystem implementations for different storage backends including local directories, memory filesystems, archives, and network filesystems. Each implementation provides the same FS interface while handling the specifics of its storage type.
Access to the local operating system filesystem with full read/write capabilities.
class OSFS(FS):
"""Operating system filesystem."""
def __init__(self, root_path: str, create: bool = False, create_mode: int = 0o777) -> None:
"""
Create OSFS for a directory path.
Parameters:
- root_path: str, path to root directory
- create: bool, create directory if it doesn't exist
- create_mode: int, permissions for created directory
"""Fast in-memory filesystem for temporary data and testing.
class MemoryFS(FS):
"""In-memory filesystem."""
def __init__(self) -> None:
"""Create empty memory filesystem."""Read and write ZIP archive files as filesystems.
class ZipFS(FS):
"""ZIP archive filesystem."""
def __init__(self, file: Union[str, IO], write: bool = False, compression: str = 'deflate',
allowZip64: bool = True, encoding: str = 'utf-8') -> None:
"""
Create ZipFS from ZIP file.
Parameters:
- file: str or file-like object, ZIP file path or object
- write: bool, open for writing
- compression: str, compression method ('deflate', 'stored', 'bzip2', 'lzma')
- allowZip64: bool, enable ZIP64 extensions
- encoding: str, encoding for filenames
"""Read and write TAR archive files as filesystems.
class TarFS(FS):
"""TAR archive filesystem."""
def __init__(self, file: Union[str, IO], write: bool = False, compression: str = None,
encoding: str = 'utf-8') -> None:
"""
Create TarFS from TAR file.
Parameters:
- file: str or file-like object, TAR file path or object
- write: bool, open for writing
- compression: str, compression method ('gz', 'bz2', 'xz', None)
- encoding: str, encoding for filenames
"""Access FTP servers as filesystems with network connectivity.
class FTPFS(FS):
"""FTP filesystem."""
def __init__(self, host: str, user: str = 'anonymous', passwd: str = '',
acct: str = '', timeout: int = 10, port: int = 21,
proxy: str = None, tls: bool = False) -> None:
"""
Create FTP filesystem.
Parameters:
- host: str, FTP server hostname
- user: str, username
- passwd: str, password
- acct: str, account
- timeout: int, connection timeout in seconds
- port: int, FTP port
- proxy: str, proxy server
- tls: bool, use FTP over TLS
"""Temporary directory with automatic cleanup when closed.
class TempFS(FS):
"""Temporary filesystem."""
def __init__(self, identifier: str = 'fs', auto_clean: bool = True,
temp_dir: str = None) -> None:
"""
Create temporary filesystem.
Parameters:
- identifier: str, identifier for temp directory name
- auto_clean: bool, automatically clean up on close
- temp_dir: str, parent directory for temp filesystem
"""Filesystem for application data directories following platform conventions.
class AppFS(FS):
"""Application data filesystem."""
def __init__(self, appname: str, author: str = None, version: str = None,
roaming: bool = False, create: bool = True) -> None:
"""
Create application filesystem.
Parameters:
- appname: str, application name
- author: str, application author
- version: str, application version
- roaming: bool, use roaming profile (Windows)
- create: bool, create directory if it doesn't exist
"""View of a subdirectory within another filesystem.
class SubFS(FS):
"""Subdirectory filesystem view."""
def __init__(self, parent_fs: FS, path: str) -> None:
"""
Create SubFS for a directory.
Parameters:
- parent_fs: FS, parent filesystem
- path: str, path to subdirectory
"""Mount multiple filesystems at different paths in a single namespace.
class MountFS(FS):
"""Mount multiple filesystems."""
def __init__(self, auto_close: bool = True) -> None:
"""
Create mount filesystem.
Parameters:
- auto_close: bool, automatically close mounted filesystems
"""
def mount(self, path: str, fs: FS) -> None:
"""
Mount filesystem at path.
Parameters:
- path: str, mount point path
- fs: FS, filesystem to mount
"""
def unmount(self, path: str) -> FS:
"""
Unmount filesystem at path.
Parameters:
- path: str, mount point path
Returns:
FS: Unmounted filesystem
"""Layer multiple filesystems with fallback behavior for read operations.
class MultiFS(FS):
"""Multiple filesystem with fallback."""
def __init__(self, auto_close: bool = True) -> None:
"""
Create multi filesystem.
Parameters:
- auto_close: bool, automatically close added filesystems
"""
def add_fs(self, name: str, fs: FS, write: bool = False, priority: int = 0) -> None:
"""
Add filesystem to multi filesystem.
Parameters:
- name: str, filesystem name
- fs: FS, filesystem to add
- write: bool, allow writes to this filesystem
- priority: int, filesystem priority (higher = checked first)
"""
def remove_fs(self, name: str) -> FS:
"""
Remove filesystem by name.
Parameters:
- name: str, filesystem name
Returns:
FS: Removed filesystem
"""Base classes and common wrappers for extending filesystem functionality.
class WrapFS(FS):
"""Base class for filesystem wrappers."""
def __init__(self, wrap_fs: FS) -> None:
"""
Create wrapper filesystem.
Parameters:
- wrap_fs: FS, filesystem to wrap
"""
class ReadOnlyFS(WrapFS):
"""Read-only filesystem wrapper."""
pass
class CachingFS(WrapFS):
"""Caching filesystem wrapper."""
def __init__(self, wrap_fs: FS, cache_timeout: int = 60) -> None:
"""
Create caching filesystem.
Parameters:
- wrap_fs: FS, filesystem to wrap
- cache_timeout: int, cache timeout in seconds
"""Creating different filesystem types:
from fs.osfs import OSFS
from fs.memoryfs import MemoryFS
from fs.zipfs import ZipFS
from fs.tempfs import TempFS
# Local directory filesystem
home_fs = OSFS('~', create=True)
# Memory filesystem
mem_fs = MemoryFS()
# ZIP archive filesystem
with ZipFS('archive.zip', write=True) as zip_fs:
zip_fs.writetext('hello.txt', 'Hello World!')
# Temporary filesystem (auto-cleanup)
with TempFS() as temp_fs:
temp_fs.writetext('temp.txt', 'Temporary data')
# Automatically cleaned up on exit
# Application data filesystem
from fs.appfs import AppFS
app_fs = AppFS('MyApp', author='MyCompany', version='1.0')Mounting multiple filesystems:
from fs.mountfs import MountFS
from fs.osfs import OSFS
from fs.memoryfs import MemoryFS
mount_fs = MountFS()
mount_fs.mount('home', OSFS('~'))
mount_fs.mount('temp', MemoryFS())
# Access files through mount points
mount_fs.writetext('temp/cache.txt', 'Cached data')
mount_fs.copy('home/document.txt', 'temp/backup.txt')from typing import Union, IO, Optional
# Archive compression types
CompressionType = Union['deflate', 'stored', 'bzip2', 'lzma'] # ZIP
TarCompressionType = Union['gz', 'bz2', 'xz', None] # TARInstall with Tessl CLI
npx tessl i tessl/pypi-fs