File: example-reading-file-in-chunks.md

package info (click to toggle)
python-pycdlib 1.12.0%2Bds1-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,748 kB
  • sloc: python: 36,118; makefile: 63
file content (97 lines) | stat: -rw-r--r-- 2,909 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Example: Reading a large file in chunks
It may be useful in some applications to be able to read a file from an ISO a bit at a time and do some processing on it.  PyCdlib provides the context manager [open_file_from_iso](pycdlib-api.html#PyCdlib-open_file_from_iso) API to allow opening a file and reading in parts of it.  Here's the complete code for this example:

```
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib

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

with iso.open_file_from_iso(iso_path='/FOO.;1') as infp:
    all1 = infp.read()
    infp.seek(0)
    first = infp.read(3)
    second = infp.read()

iso.close()
```

Let's take a closer look at the code.

```
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib
```

As we've seen before, import pycdlib.  We also import the [BytesIO](https://docs.python.org/3/library/io.html#binary-i-o) module so we can use a python string as a file-like object.

```
iso = pycdlib.PyCdlib()
iso.new()
foostr = b'foofoo\n'
iso.add_fp(BytesIO(foostr), len(foostr), '/FOO.;1')
```

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](example-creating-new-basic-iso.md), so see that example for more information.

```
with iso.open_file_from_iso(iso_path='/FOO.;1') as infp:
```

Here we use the [open_file_from_iso](pycdlib-api.html#PyCdlib-open_file_from_iso) API to get a context manager to the file that we created; this will be used in the rest of the explanations below.

```
    all = infp.read()
```

The first `read` call reads in all of the data in the file, so at the end of the call the "all" variable will contain `foofoo\n`. 

```
    infp.seek(0)
```

The 'seek' call then resets the file pointer back to the beginning of the file.

```
    first = infp.read(3)
```

If the `read` API is passed a number, it will read and return that many bytes.  In this case, the 'first' variable will end up containing `foo`.

```
    second = infp.read()
```

And now we read the rest of the data, so the 'second' variable will end up containing `foo\n`.

```
iso.close()
```

As is the case in other examples, we close out the PyCdlib object.

---

<div style="width: 100%; display: table;">
  <div style="display: table-row;">
    <div style="width: 33%; display: table-cell; text-align: left;">
      <a href="example-walking-iso-filesystem.html"><-- Example: Walking the ISO filesystem</a>
    </div>
    <div style="width: 33%; display: table-cell; text-align: center;">
      <a href="https://clalancette.github.io/pycdlib/">Top</a>
    </div>
    <div style="width: 33%; display: table-cell; text-align: right;">
      <a href="design.html">Design --></a>
    </div>
</div>