Extract file from fat image in python

Question:

I have a fat32 partition image file dump, for example created with dd. how i can parse this file with python and extract the desired file inside this partition.

Asked By: David A

||

Answers:

As far as reading a FAT32 filesystem image in Python goes, the Wikipedia page has all the detail you need to write a read-only implementation.

Construct may be of some use. Looks like they have an example for FAT16 (https://github.com/construct/construct/blob/master/construct/formats/filesystem/fat16.py) which you could try extending.

Answered By: davidg

Just found out this nice lib7zip bindings that can read RAW FAT images (and much more).

Example usage:

# pip install git+https://github.com/topia/pylib7zip
from lib7zip import Archive, formats

archive = Archive("fd.ima", forcetype="FAT")

# iterate over archive contents
for f in archive:
    if f.is_dir:
        continue

    print("; %12s  %s %s" % ( f.size, f.mtime.strftime("%H:%M.%S %Y-%m-%d"), f.path))

    f_crc = f.crc
    if not f_crc:
        # extract in memory and compute crc32
        f_crc = -1
        try:
            f_contents = f.contents
        except:
            # possible extraction error
            continue
        if len(f_contents) > 0:
            f_crc = crc32(f_contents)
    # end if
    print("%s %08X " % ( f.path, f_crc ) )

An alternative is pyfatfs (untested by me).

Answered By: eadmaster
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.