Skip to content

Python development

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

libqcow comes with Python-bindings named pyqcow.

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

Import

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

import pyqcow

Get version

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

pyqcow.get_version()

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

Open file

Open a file by path

qcow_file = pyqcow.file()

qcow_file.open("image.qcow2")

...

qcow_file.close()

The explicit call to qcow_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.qcow2", "rb")

qcow_file = pyqcow.file()

qcow_file.open_file_object(file_object)

...

qcow_file.close()

The explicit call to qcow_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 pyqcow with pytsk3

The following additional import is required:

import pytsk3
class qcow_Img_Info(pytsk3.Img_Info):
  def __init__(self, qcow_file):
    self._qcow_file = qcow_file
    super(qcow_Img_Info, self).__init__(
        url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL)

  def close(self):
    self._qcow_file.close()

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

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


qcow_file = pyqcow.file()

qcow_file.open("image.qcow2")

img_info = qcow_Img_Info(qcow_file)

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

Also see

import pyqcow

help(pyqcow)
help(pyqcow.file)