Skip to content Skip to sidebar Skip to footer

Write Zeros To File Blocks

I'm attempting to identify the blocks that are tied to a specific file and write zeros to them. I've found several methods that do this to the free space on a disk, but so far I ha

Solution 1:

Writing code that can safely modify even an unmounted filesystem will require significant effort. It is to be avoided unless there is no other option.

You basically have two choices to make modifying the filesystem easy:

  • Run python in the virtual environment.
  • Mount the virtualized filesystem on the host. Most UNIX-like systems can do that, e.g. with the help of FUSE (which support a lot of filesystem types) and loop devices.

This way you can use the (guest or host) OS's filesystem code instead of having to roll your own. :-) If you can use one of those options, the code fragment listed below will fill a file with zeroes:

import os

def overwrite(f):
    """Overwrite a file with zeroes.

    Arguments:
    f -- name of the file
    """
    stat = os.stat(f)
    with open(f, 'r+') as of:
        of.write('\0' * stat.st_size)
        of.flush()

Post a Comment for "Write Zeros To File Blocks"