fatfs-ng (Python FatFS Wrapper)
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
- gotcha The GitHub repository link provided in the PyPI metadata for 'fatfs-ng' (https://github.com/Jason2866/pyfatfs) appears to point to a user profile 'Jason2866' which does not contain a Python FAT filesystem wrapper. The examples and imports in this registry entry are therefore based on the commonly used 'pyfatfs' library (by nathanhi) which is a prominent Python wrapper for ChaN's FatFS and likely the intended upstream or a very similar project.
- gotcha When manipulating FAT filesystems, especially those intended for embedded systems or specific hardware, ensure correct encoding is used for filenames. FAT typically uses IBM437, but VFAT (long file names) uses UTF-16-LE. Incorrect encoding can lead to unreadable filenames or data corruption.
- breaking The underlying ChaN's FatFS C library has undergone various revisions (e.g., R0.15, R0.16) with breaking changes in its C API, especially regarding synchronization functions, argument changes for `f_read`/`f_write`, and `f_mkfs`. While `fatfs-ng` is a Python wrapper, internal updates to the wrapped C library might introduce breaking changes that propagate to the Python API in future major versions.
- gotcha Handling timestamps on FAT filesystems can be tricky. By default, timestamps might be created in local time, which can cause issues with systems expecting UTC.
Install
-
pip install fatfs-ng
Imports
- PyFatFS
from pyfatfs import PyFatFS
- PyFatBytesIOFS
from pyfatfs import PyFatBytesIOFS
Quickstart
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('/'))