View on GitHub

pycdlib

Python library to read and write ISOs

Example: Extracting data from an existing ISO

This example will show how to extract data from an existing ISO. Here’s the complete code for this example:

from io import BytesIO

import pycdlib

iso = pycdlib.PyCdlib()
iso.new()
foostr = b'foo\n'
iso.add_fp(BytesIO(foostr), len(foostr), '/FOO.;1')
out = BytesIO()
iso.write_fp(out)
iso.close()

iso.open_fp(out)
extracted = BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/FOO.;1')
iso.close()

print(extracted.getvalue().decode('utf-8'))

Let’s take a closer look at the code.

from io import BytesIO

import pycdlib

As we’ve seen before, import pycdlib. We also import the BytesIO module so we can use a python string as a file-like object.

iso = pycdlib.PyCdlib()
iso.new()
foostr = b'foo\n'
iso.add_fp(BytesIO(foostr), len(foostr), '/FOO.;1')
out = BytesIO()
iso.write_fp(out)
iso.close()

This code creates a new ISO, adds a single file to it, and writes it out. This is very similar to the code in Creating a new, basic ISO, so see that example for more information. One important difference in this code is that it uses a BytesIO object to master the ISO into so we don’t have to write any temporary data out to the filesystem; it all happens in memory.

iso.open_fp(out)

Here we open up the ISO we created above. We can safely re-use the PyCdlib object because we called the close method earlier. Also note that we use open_fp to open the file-like object we wrote into using write_fp above; we do not need to seek out back to the beginning first because open_fp seeks the file pointer to the start of the ISO internally.

extracted = BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/FOO.;1')

Now we use the get_file_from_iso_fp API to extract the data from a file on the ISO. In this case, we access the “/FOO.;1” file that we created above, and write out the data to the BytesIO object extracted.

iso.close()

print(extracted.getvalue().decode('utf-8'))

As is the case in other examples, we close out the PyCdlib object, and print out the data we extracted.


<-- Example: Opening an existing ISO
Top
Example: Creating a bootable ISO (El Torito) -->