fatfs-ng (Python FatFS Wrapper)

0.1.15 · active · verified Sun Apr 12

fatfs-ng is an enhanced Python wrapper around ChaN's FatFS C library, building upon the `fatfs-python` project. It provides Pythonic access to FAT12, FAT16, and FAT32 filesystems, including VFAT (long file name) support. The library is currently at version 0.1.15 and appears to have a relatively active release cadence, albeit with minor version bumps.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a simple in-memory FAT filesystem, writing a file, reading it back, and listing directory contents using the `PyFatBytesIOFS` class from the `pyfatfs` library. This approach is commonly seen in related Python FAT wrappers like `pyfatfs`, as direct `fatfs-ng` specific examples were not readily available under the provided GitHub link.

from pyfatfs import PyFatBytesIOFS
from fs.opener import open_fs

# Create an in-memory FAT12 filesystem image (default size is 1.44MB floppy)
# This uses PyFilesystem2's 'memfs' which is then formatted as FAT
# For fatfs-ng specific usage, it might involve direct class instantiation or a different opener.
# This example is adapted from pyfatfs documentation.

# Initialize a PyFatBytesIOFS for an in-memory FAT filesystem
fat_fs = PyFatBytesIOFS(size=1474560) # 1.44MB floppy size

# Create a file and write to it
with fat_fs.open('test.txt', 'w') as f:
    f.write('Hello, fatfs-ng!')

# Read the file
with fat_fs.open('test.txt', 'r') as f:
    content = f.read()
    print(f"Read from FAT filesystem: {content}")

# List directory contents
print(f"Files in root directory: {fat_fs.listdir('/')}")

# Close the filesystem (important for releasing resources/flushing changes)
fat_fs.close()

# Example using PyFilesystem2 opener with a FAT image (conceptual, actual opener URI might vary)
# with open_fs('fat://my_fat_image.bin') as fat_disk:
#     fat_disk.writetext('another_file.txt', 'Content via PyFilesystem2')
#     print(fat_disk.listdir('/'))

view raw JSON →