Skip to content

Python development

Joachim Metz edited this page Jul 15, 2022 · 2 revisions

libvhdi comes with Python-bindings named pyvhdi.

Below are examples how use pyvhdi. They assume you have a working version of pyvhdi on your system. To build pyvhdi see Building.

Import

To be able to use pyvhdi in your Python scripts add the following import:

import pyvhdi

Get version

The get_version() module function can be used to retrieve the version of the pyvhdi.

pyvhdi.get_version()

This will return a textual string (Unicode) that contains the libvhdi version. Since pyvhdi is a wrapper around libvhdi it does not have a separate version.

Open file

Open a file by path

vhdi_file = pyvhdi.file()

vhdi_file.open("image.vhd")

...

vhdi_file.close()

The explicit call to vhdi_file.close() is not required. Close only must be called once all operations on the file have been completed.

Open a file using a file-like object

file_object = open("image.vhd", "rb")

vhdi_file = pyvhdi.file()

vhdi_file.open_file_object(file_object)

...

vhdi_file.close()

The explicit call to vhdi_file.close() is not required. Close only must be called once all operations on the file have been completed and will not close the file-like object itself.

Examples

Combining pyvhdi with pytsk3

The following additional import is required:

import pytsk3
class vhdi_Img_Info(pytsk3.Img_Info):
  def __init__(self, vhdi_file):
    self._vhdi_file = vhdi_file
    super(vhdi_Img_Info, self).__init__(
        url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL)

  def close(self):
    self._vhdi_file.close()

  def read(self, offset, size):
    self._vhdi_file.seek(offset)
    return self._vhdi_file.read(size)

  def get_size(self):
    return self._vhdi_file.get_media_size()


vhdi_file = pyvhdi.file()

vhdi_file.open("image.vhd")

img_info = vhdi_Img_Info(vhdi_file)

fs_info = pytsk3.FS_Info(img_info, offset=63 * 512)

Also see

import pyvhdi

help(pyvhdi)
help(pyvhdi.file)