| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node407.html" /> |
| 13 | <link rel="prev" href="module-bz2.html" /> |
| 14 | <link rel="parent" href="module-bz2.html" /> |
| 15 | <link rel="next" href="node407.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>7.17.1 (De)compression of files</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="7.17 bz2 " |
| 25 | href="module-bz2.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="7.17 bz2 " |
| 28 | href="module-bz2.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="7.17.2 Sequential (de)compression" |
| 31 | href="node407.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-bz2.html">7.17 bz2 </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-bz2.html">7.17 bz2 </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node407.html">7.17.2 Sequential (de)compression</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0091710000000000000000"> |
| 56 | 7.17.1 (De)compression of files</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | Handling of compressed files is offered by the <tt class="class">BZ2File</tt> class. |
| 61 | |
| 62 | <P> |
| 63 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 64 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-2833' xml:id='l2h-2833' class="class">BZ2File</tt></b>(</nobr></td> |
| 65 | <td><var>filename</var><big>[</big><var>, mode</var><big>[</big><var>, |
| 66 | buffering</var><big>[</big><var>, compresslevel</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 67 | <dd> |
| 68 | Open a bz2 file. Mode can be either <code>'r'</code> or <code>'w'</code>, for reading |
| 69 | (default) or writing. When opened for writing, the file will be created if |
| 70 | it doesn't exist, and truncated otherwise. If <var>buffering</var> is given, |
| 71 | <code>0</code> means unbuffered, and larger numbers specify the buffer size; |
| 72 | the default is <code>0</code>. If |
| 73 | <var>compresslevel</var> is given, it must be a number between <code>1</code> and |
| 74 | <code>9</code>; the default is <code>9</code>. |
| 75 | Add a "<tt class="character">U</tt>" to mode to open the file for input with universal newline |
| 76 | support. Any line ending in the input file will be seen as a |
| 77 | "<tt class="character">\n</tt>" in Python. Also, a file so opened gains the |
| 78 | attribute <tt class="member">newlines</tt>; the value for this attribute is one of |
| 79 | <code>None</code> (no newline read yet), <code>'\r'</code>, <code>'\n'</code>, |
| 80 | <code>'\r\n'</code> or a tuple containing all the newline types |
| 81 | seen. Universal newlines are available only when reading. |
| 82 | Instances support iteration in the same way as normal <tt class="class">file</tt> |
| 83 | instances. |
| 84 | </dl> |
| 85 | |
| 86 | <P> |
| 87 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 88 | <td><nobr><b><tt id='l2h-2834' xml:id='l2h-2834' class="method">close</tt></b>(</nobr></td> |
| 89 | <td><var></var>)</td></tr></table></dt> |
| 90 | <dd> |
| 91 | Close the file. Sets data attribute <tt class="member">closed</tt> to true. A closed file |
| 92 | cannot be used for further I/O operations. <tt class="method">close()</tt> may be called |
| 93 | more than once without error. |
| 94 | </dl> |
| 95 | |
| 96 | <P> |
| 97 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 98 | <td><nobr><b><tt id='l2h-2835' xml:id='l2h-2835' class="method">read</tt></b>(</nobr></td> |
| 99 | <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt> |
| 100 | <dd> |
| 101 | Read at most <var>size</var> uncompressed bytes, returned as a string. If the |
| 102 | <var>size</var> argument is negative or omitted, read until EOF is reached. |
| 103 | </dl> |
| 104 | |
| 105 | <P> |
| 106 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 107 | <td><nobr><b><tt id='l2h-2836' xml:id='l2h-2836' class="method">readline</tt></b>(</nobr></td> |
| 108 | <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt> |
| 109 | <dd> |
| 110 | Return the next line from the file, as a string, retaining newline. |
| 111 | A non-negative <var>size</var> argument limits the maximum number of bytes to |
| 112 | return (an incomplete line may be returned then). Return an empty |
| 113 | string at EOF. |
| 114 | </dl> |
| 115 | |
| 116 | <P> |
| 117 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 118 | <td><nobr><b><tt id='l2h-2837' xml:id='l2h-2837' class="method">readlines</tt></b>(</nobr></td> |
| 119 | <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt> |
| 120 | <dd> |
| 121 | Return a list of lines read. The optional <var>size</var> argument, if given, |
| 122 | is an approximate bound on the total number of bytes in the lines returned. |
| 123 | </dl> |
| 124 | |
| 125 | <P> |
| 126 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 127 | <td><nobr><b><tt id='l2h-2838' xml:id='l2h-2838' class="method">xreadlines</tt></b>(</nobr></td> |
| 128 | <td><var></var>)</td></tr></table></dt> |
| 129 | <dd> |
| 130 | For backward compatibility. <tt class="class">BZ2File</tt> objects now include the |
| 131 | performance optimizations previously implemented in the |
| 132 | <tt class="module">xreadlines</tt> module. |
| 133 | <div class="versionnote"><b>Deprecated since release 2.3.</b> |
| 134 | This exists only for compatibility with the method by |
| 135 | this name on <tt class="class">file</tt> objects, which is |
| 136 | deprecated. Use <code>for line in file</code> instead.</div><p></p> |
| 137 | </dl> |
| 138 | |
| 139 | <P> |
| 140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 141 | <td><nobr><b><tt id='l2h-2839' xml:id='l2h-2839' class="method">seek</tt></b>(</nobr></td> |
| 142 | <td><var>offset</var><big>[</big><var>, whence</var><big>]</big><var></var>)</td></tr></table></dt> |
| 143 | <dd> |
| 144 | Move to new file position. Argument <var>offset</var> is a byte count. Optional |
| 145 | argument <var>whence</var> defaults to <code>0</code> (offset from start of file, |
| 146 | offset should be <code>>= 0</code>); other values are <code>1</code> (move relative to |
| 147 | current position, positive or negative), and <code>2</code> (move relative to end |
| 148 | of file, usually negative, although many platforms allow seeking beyond |
| 149 | the end of a file). |
| 150 | |
| 151 | <P> |
| 152 | Note that seeking of bz2 files is emulated, and depending on the parameters |
| 153 | the operation may be extremely slow. |
| 154 | </dl> |
| 155 | |
| 156 | <P> |
| 157 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 158 | <td><nobr><b><tt id='l2h-2840' xml:id='l2h-2840' class="method">tell</tt></b>(</nobr></td> |
| 159 | <td><var></var>)</td></tr></table></dt> |
| 160 | <dd> |
| 161 | Return the current file position, an integer (may be a long integer). |
| 162 | </dl> |
| 163 | |
| 164 | <P> |
| 165 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 166 | <td><nobr><b><tt id='l2h-2841' xml:id='l2h-2841' class="method">write</tt></b>(</nobr></td> |
| 167 | <td><var>data</var>)</td></tr></table></dt> |
| 168 | <dd> |
| 169 | Write string <var>data</var> to file. Note that due to buffering, <tt class="method">close()</tt> |
| 170 | may be needed before the file on disk reflects the data written. |
| 171 | </dl> |
| 172 | |
| 173 | <P> |
| 174 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 175 | <td><nobr><b><tt id='l2h-2842' xml:id='l2h-2842' class="method">writelines</tt></b>(</nobr></td> |
| 176 | <td><var>sequence_of_strings</var>)</td></tr></table></dt> |
| 177 | <dd> |
| 178 | Write the sequence of strings to the file. Note that newlines are not added. |
| 179 | The sequence can be any iterable object producing strings. This is equivalent |
| 180 | to calling write() for each string. |
| 181 | </dl> |
| 182 | |
| 183 | <P> |
| 184 | |
| 185 | <DIV CLASS="navigation"> |
| 186 | <div class='online-navigation'> |
| 187 | <p></p><hr /> |
| 188 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 189 | <tr> |
| 190 | <td class='online-navigation'><a rel="prev" title="7.17 bz2 " |
| 191 | href="module-bz2.html"><img src='../icons/previous.png' |
| 192 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 193 | <td class='online-navigation'><a rel="parent" title="7.17 bz2 " |
| 194 | href="module-bz2.html"><img src='../icons/up.png' |
| 195 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 196 | <td class='online-navigation'><a rel="next" title="7.17.2 Sequential (de)compression" |
| 197 | href="node407.html"><img src='../icons/next.png' |
| 198 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 199 | <td align="center" width="100%">Python Library Reference</td> |
| 200 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 201 | href="contents.html"><img src='../icons/contents.png' |
| 202 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 203 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 204 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 205 | <td class='online-navigation'><a rel="index" title="Index" |
| 206 | href="genindex.html"><img src='../icons/index.png' |
| 207 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 208 | </tr></table> |
| 209 | <div class='online-navigation'> |
| 210 | <b class="navlabel">Previous:</b> |
| 211 | <a class="sectref" rel="prev" href="module-bz2.html">7.17 bz2 </A> |
| 212 | <b class="navlabel">Up:</b> |
| 213 | <a class="sectref" rel="parent" href="module-bz2.html">7.17 bz2 </A> |
| 214 | <b class="navlabel">Next:</b> |
| 215 | <a class="sectref" rel="next" href="node407.html">7.17.2 Sequential (de)compression</A> |
| 216 | </div> |
| 217 | </div> |
| 218 | <hr /> |
| 219 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 220 | </DIV> |
| 221 | <!--End of Navigation Panel--> |
| 222 | <ADDRESS> |
| 223 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 224 | </ADDRESS> |
| 225 | </BODY> |
| 226 | </HTML> |