{"id":1484,"library":"fs","title":"PyFilesystem2","description":"PyFilesystem2 is a Python library that provides a common, high-level abstraction layer for interacting with various filesystems, including local, memory, zip, FTP, and more. It enables consistent management of files and directories across different storage backends. The current version is 2.4.16, with frequent maintenance and minor feature releases.","status":"active","version":"2.4.16","language":"en","source_language":"en","source_url":"https://github.com/PyFilesystem/pyfilesystem2","tags":["filesystem","abstraction","virtual filesystem","vfs","file operations"],"install":[{"cmd":"pip install fs","lang":"bash","label":"Install PyFilesystem2"}],"dependencies":[],"imports":[{"note":"The `open_fs` factory function is now directly available from the top-level `fs` package, not `fs.opener` (which was a v1 pattern).","wrong":"from fs.opener import open_fs","symbol":"open_fs","correct":"from fs import open_fs"},{"symbol":"OSFS","correct":"from fs.osfs import OSFS"},{"symbol":"MemoryFS","correct":"from fs.memoryfs import MemoryFS"},{"note":"While `import fs` is valid, `FS` (the base class) resides in `fs.base`. Directly importing `FS` is more precise. This also avoids confusion with the legacy `pyfilesystem` v1, which had different module structures.","wrong":"import fs","symbol":"FS","correct":"from fs.base import FS"}],"quickstart":{"code":"from fs import open_fs\nfrom fs.osfs import OSFS\nimport os\n\n# Example 1: Using a memory filesystem\nprint(\"--- Memory Filesystem Example ---\")\nwith open_fs(\"mem://\") as mem_fs:\n    mem_fs.writetext(\"hello.txt\", \"Hello from MemoryFS!\")\n    print(f\"Content of hello.txt: {mem_fs.readtext('hello.txt')}\")\n    print(f\"Files in memory FS: {mem_fs.listdir('/')}\")\n\n# Example 2: Using the local operating system filesystem\nprint(\"\\n--- OS Filesystem Example ---\")\n# Opens a filesystem for the current working directory\nwith OSFS('.') as os_fs:\n    temp_dir_path = \"_fs_temp_dir_\"\n    temp_file_path = os.path.join(temp_dir_path, \"temp_quickstart.txt\")\n\n    if not os_fs.exists(temp_dir_path):\n        os_fs.makedir(temp_dir_path)\n        print(f\"Created directory: {temp_dir_path}\")\n\n    os_fs.writetext(temp_file_path, \"This is a temporary file in OSFS.\")\n    print(f\"Content of {temp_file_path}: {os_fs.readtext(temp_file_path)}\")\n\n    # Clean up the created file and directory\n    os_fs.remove(temp_file_path)\n    os_fs.removedir(temp_dir_path)\n    print(f\"Cleaned up: {temp_file_path} and {temp_dir_path}\")\n","lang":"python","description":"Demonstrates opening and interacting with a memory filesystem and a local OS filesystem. It shows creating, writing, reading, listing, and cleaning up resources, emphasizing the use of context managers for proper resource handling."},"warnings":[{"fix":"Code written for `pyfilesystem` (v1) is incompatible with `fs` (v2). You must rewrite your code to use the `fs` (v2) API and import paths (e.g., `from fs import open_fs` instead of `from fs.opener import open_fs`). The `fs` package on PyPI refers to v2.","message":"PyFilesystem v1 (pyfilesystem) vs. v2 (fs) - Complete API and import path rewrite.","severity":"breaking","affected_versions":"<2.0.0 (pyfilesystem) vs. >=2.0.0 (fs)"},{"fix":"Always use filesystem objects within a `with` statement (e.g., `with open_fs('osfs://') as my_fs:`). This ensures resources (like file handles or network connections) are released automatically. If not using a context manager, call `my_fs.close()` explicitly when done.","message":"Filesystem objects should be properly closed using context managers or explicit close() calls.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Always use `/` as the path separator when interacting with `fs` objects. Avoid using `os.sep` or platform-specific separators directly within `fs` methods. The `fs.path` module provides utility functions for path manipulation that are consistent with `fs` conventions.","message":"Paths within PyFilesystem are 'universal' and use `/` as the separator, regardless of the underlying OS.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Ensure the filesystem is opened with write permissions if you intend to modify it. `fs.open_fs` has a `writable` parameter, and some filesystems (e.g., `ZipFS` opened in read mode) are inherently read-only. Check `my_fs.is_writable` before attempting writes. For creating read-only views of a writable FS, use `fs.wrap.read_only`.","message":"Attempting write operations on a read-only filesystem will raise errors.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}