| 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="tarinfo-objects.html" /> |
| 13 | <link rel="prev" href="module-tarfile.html" /> |
| 14 | <link rel="parent" href="module-tarfile.html" /> |
| 15 | <link rel="next" href="tarinfo-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>7.19.1 TarFile Objects </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.19 tarfile " |
| 25 | href="module-tarfile.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.19 tarfile " |
| 28 | href="module-tarfile.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.19.2 TarInfo Objects" |
| 31 | href="tarinfo-objects.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-tarfile.html">7.19 tarfile </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-tarfile.html">7.19 tarfile </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="tarinfo-objects.html">7.19.2 TarInfo Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0091910000000000000000"></A><A NAME="tarfile-objects"></A> |
| 56 | <BR> |
| 57 | 7.19.1 TarFile Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | The <tt class="class">TarFile</tt> object provides an interface to a tar archive. A tar |
| 62 | archive is a sequence of blocks. An archive member (a stored file) is made up |
| 63 | of a header block followed by data blocks. It is possible, to store a file in a |
| 64 | tar archive several times. Each archive member is represented by a |
| 65 | <tt class="class">TarInfo</tt> object, see <em class="citetitle" |
| 66 | >TarInfo Objects</em> (section |
| 67 | <A href="tarinfo-objects.html#tarinfo-objects">7.19.2</A>) for details. |
| 68 | |
| 69 | <P> |
| 70 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 71 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-2900' xml:id='l2h-2900' class="class">TarFile</tt></b>(</nobr></td> |
| 72 | <td><var></var><big>[</big><var>name |
| 73 | </var><big>[</big><var>, mode</var><big>[</big><var>, fileobj</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 74 | <dd> |
| 75 | Open an <em>(uncompressed)</em> tar archive <var>name</var>. |
| 76 | <var>mode</var> is either <code>'r'</code> to read from an existing archive, |
| 77 | <code>'a'</code> to append data to an existing file or <code>'w'</code> to create a new |
| 78 | file overwriting an existing one. <var>mode</var> defaults to <code>'r'</code>. |
| 79 | |
| 80 | <P> |
| 81 | If <var>fileobj</var> is given, it is used for reading or writing data. |
| 82 | If it can be determined, <var>mode</var> is overridden by <var>fileobj</var>'s mode. |
| 83 | <div class="note"><b class="label">Note:</b> |
| 84 | <var>fileobj</var> is not closed, when <tt class="class">TarFile</tt> is closed. |
| 85 | </div> |
| 86 | </dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><tt id='l2h-2901' xml:id='l2h-2901' class="method">open</tt></b>(</nobr></td> |
| 91 | <td><var>...</var>)</td></tr></table></dt> |
| 92 | <dd> |
| 93 | Alternative constructor. The <tt class="function">open()</tt> function on module level is |
| 94 | actually a shortcut to this classmethod. See section <A href="module-tarfile.html#module-tarfile">7.19</A> |
| 95 | for details. |
| 96 | </dl> |
| 97 | |
| 98 | <P> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-2902' xml:id='l2h-2902' class="method">getmember</tt></b>(</nobr></td> |
| 101 | <td><var>name</var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | Return a <tt class="class">TarInfo</tt> object for member <var>name</var>. If <var>name</var> can |
| 104 | not be found in the archive, <tt class="exception">KeyError</tt> is raised. |
| 105 | <div class="note"><b class="label">Note:</b> |
| 106 | If a member occurs more than once in the archive, its last |
| 107 | occurrence is assumed to be the most up-to-date version. |
| 108 | </div> |
| 109 | </dl> |
| 110 | |
| 111 | <P> |
| 112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 113 | <td><nobr><b><tt id='l2h-2903' xml:id='l2h-2903' class="method">getmembers</tt></b>(</nobr></td> |
| 114 | <td><var></var>)</td></tr></table></dt> |
| 115 | <dd> |
| 116 | Return the members of the archive as a list of <tt class="class">TarInfo</tt> objects. |
| 117 | The list has the same order as the members in the archive. |
| 118 | </dl> |
| 119 | |
| 120 | <P> |
| 121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 122 | <td><nobr><b><tt id='l2h-2904' xml:id='l2h-2904' class="method">getnames</tt></b>(</nobr></td> |
| 123 | <td><var></var>)</td></tr></table></dt> |
| 124 | <dd> |
| 125 | Return the members as a list of their names. It has the same order as |
| 126 | the list returned by <tt class="method">getmembers()</tt>. |
| 127 | </dl> |
| 128 | |
| 129 | <P> |
| 130 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 131 | <td><nobr><b><tt id='l2h-2905' xml:id='l2h-2905' class="method">list</tt></b>(</nobr></td> |
| 132 | <td><var>verbose=True</var>)</td></tr></table></dt> |
| 133 | <dd> |
| 134 | Print a table of contents to <code>sys.stdout</code>. If <var>verbose</var> is |
| 135 | <tt class="constant">False</tt>, only the names of the members are printed. If it is |
| 136 | <tt class="constant">True</tt>, output similar to that of <b class="program">ls -l</b> is produced. |
| 137 | </dl> |
| 138 | |
| 139 | <P> |
| 140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 141 | <td><nobr><b><tt id='l2h-2906' xml:id='l2h-2906' class="method">next</tt></b>(</nobr></td> |
| 142 | <td><var></var>)</td></tr></table></dt> |
| 143 | <dd> |
| 144 | Return the next member of the archive as a <tt class="class">TarInfo</tt> object, when |
| 145 | <tt class="class">TarFile</tt> is opened for reading. Return <code>None</code> if there is no |
| 146 | more available. |
| 147 | </dl> |
| 148 | |
| 149 | <P> |
| 150 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 151 | <td><nobr><b><tt id='l2h-2907' xml:id='l2h-2907' class="method">extract</tt></b>(</nobr></td> |
| 152 | <td><var>member</var><big>[</big><var>, path</var><big>]</big><var></var>)</td></tr></table></dt> |
| 153 | <dd> |
| 154 | Extract a member from the archive to the current working directory, |
| 155 | using its full name. Its file information is extracted as accurately as |
| 156 | possible. |
| 157 | <var>member</var> may be a filename or a <tt class="class">TarInfo</tt> object. |
| 158 | You can specify a different directory using <var>path</var>. |
| 159 | </dl> |
| 160 | |
| 161 | <P> |
| 162 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 163 | <td><nobr><b><tt id='l2h-2908' xml:id='l2h-2908' class="method">extractfile</tt></b>(</nobr></td> |
| 164 | <td><var>member</var>)</td></tr></table></dt> |
| 165 | <dd> |
| 166 | Extract a member from the archive as a file object. |
| 167 | <var>member</var> may be a filename or a <tt class="class">TarInfo</tt> object. |
| 168 | If <var>member</var> is a regular file, a file-like object is returned. |
| 169 | If <var>member</var> is a link, a file-like object is constructed from the |
| 170 | link's target. |
| 171 | If <var>member</var> is none of the above, <code>None</code> is returned. |
| 172 | <div class="note"><b class="label">Note:</b> |
| 173 | The file-like object is read-only and provides the following methods: |
| 174 | <tt class="method">read()</tt>, <tt class="method">readline()</tt>, <tt class="method">readlines()</tt>, |
| 175 | <tt class="method">seek()</tt>, <tt class="method">tell()</tt>. |
| 176 | </div> |
| 177 | </dl> |
| 178 | |
| 179 | <P> |
| 180 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 181 | <td><nobr><b><tt id='l2h-2909' xml:id='l2h-2909' class="method">add</tt></b>(</nobr></td> |
| 182 | <td><var>name</var><big>[</big><var>, arcname</var><big>[</big><var>, recursive</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 183 | <dd> |
| 184 | Add the file <var>name</var> to the archive. <var>name</var> may be any type |
| 185 | of file (directory, fifo, symbolic link, etc.). |
| 186 | If given, <var>arcname</var> specifies an alternative name for the file in the |
| 187 | archive. Directories are added recursively by default. |
| 188 | This can be avoided by setting <var>recursive</var> to <tt class="constant">False</tt>; |
| 189 | the default is <tt class="constant">True</tt>. |
| 190 | </dl> |
| 191 | |
| 192 | <P> |
| 193 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 194 | <td><nobr><b><tt id='l2h-2910' xml:id='l2h-2910' class="method">addfile</tt></b>(</nobr></td> |
| 195 | <td><var>tarinfo</var><big>[</big><var>, fileobj</var><big>]</big><var></var>)</td></tr></table></dt> |
| 196 | <dd> |
| 197 | Add the <tt class="class">TarInfo</tt> object <var>tarinfo</var> to the archive. |
| 198 | If <var>fileobj</var> is given, <code><var>tarinfo</var>.size</code> bytes are read |
| 199 | from it and added to the archive. You can create <tt class="class">TarInfo</tt> objects |
| 200 | using <tt class="method">gettarinfo()</tt>. |
| 201 | <div class="note"><b class="label">Note:</b> |
| 202 | On Windows platforms, <var>fileobj</var> should always be opened with mode |
| 203 | <code>'rb'</code> to avoid irritation about the file size. |
| 204 | </div> |
| 205 | </dl> |
| 206 | |
| 207 | <P> |
| 208 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 209 | <td><nobr><b><tt id='l2h-2911' xml:id='l2h-2911' class="method">gettarinfo</tt></b>(</nobr></td> |
| 210 | <td><var></var><big>[</big><var>name</var><big>[</big><var>, |
| 211 | arcname</var><big>[</big><var>, fileobj</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 212 | <dd> |
| 213 | Create a <tt class="class">TarInfo</tt> object for either the file <var>name</var> or |
| 214 | the file object <var>fileobj</var> (using <tt class="function">os.fstat()</tt> on its |
| 215 | file descriptor). You can modify some of the <tt class="class">TarInfo</tt>'s |
| 216 | attributes before you add it using <tt class="method">addfile()</tt>. If given, |
| 217 | <var>arcname</var> specifies an alternative name for the file in the |
| 218 | archive. |
| 219 | </dl> |
| 220 | |
| 221 | <P> |
| 222 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 223 | <td><nobr><b><tt id='l2h-2912' xml:id='l2h-2912' class="method">close</tt></b>(</nobr></td> |
| 224 | <td><var></var>)</td></tr></table></dt> |
| 225 | <dd> |
| 226 | Close the <tt class="class">TarFile</tt>. In write mode, two finishing zero |
| 227 | blocks are appended to the archive. |
| 228 | </dl> |
| 229 | |
| 230 | <P> |
| 231 | <dl><dt><b><tt id='l2h-2913' xml:id='l2h-2913' class="member">posix</tt></b></dt> |
| 232 | <dd> |
| 233 | If true, create a POSIX 1003.1-1990 compliant archive. GNU |
| 234 | extensions are not used, because they are not part of the POSIX |
| 235 | standard. This limits the length of filenames to at most 256, |
| 236 | link names to 100 characters and the maximum file size to 8 |
| 237 | gigabytes. A <tt class="exception">ValueError</tt> is raised if a file exceeds |
| 238 | this limit. If false, create a GNU tar compatible archive. It |
| 239 | will not be POSIX compliant, but can store files without any |
| 240 | of the above restrictions. |
| 241 | |
| 242 | <span class="versionnote">Changed in version 2.4: |
| 243 | <var>posix</var> defaults to <tt class="constant">False</tt>.</span> |
| 244 | |
| 245 | </dl> |
| 246 | |
| 247 | <P> |
| 248 | <dl><dt><b><tt id='l2h-2914' xml:id='l2h-2914' class="member">dereference</tt></b></dt> |
| 249 | <dd> |
| 250 | If false, add symbolic and hard links to archive. If true, add the |
| 251 | content of the target files to the archive. This has no effect on |
| 252 | systems that do not support symbolic links. |
| 253 | </dl> |
| 254 | |
| 255 | <P> |
| 256 | <dl><dt><b><tt id='l2h-2915' xml:id='l2h-2915' class="member">ignore_zeros</tt></b></dt> |
| 257 | <dd> |
| 258 | If false, treat an empty block as the end of the archive. If true, |
| 259 | skip empty (and invalid) blocks and try to get as many members as |
| 260 | possible. This is only useful for concatenated or damaged |
| 261 | archives. |
| 262 | </dl> |
| 263 | |
| 264 | <P> |
| 265 | <dl><dt><b><tt id='l2h-2916' xml:id='l2h-2916' class="member">debug=0</tt></b></dt> |
| 266 | <dd> |
| 267 | To be set from <code>0</code> (no debug messages; the default) up to |
| 268 | <code>3</code> (all debug messages). The messages are written to |
| 269 | <code>sys.stderr</code>. |
| 270 | </dl> |
| 271 | |
| 272 | <P> |
| 273 | <dl><dt><b><tt id='l2h-2917' xml:id='l2h-2917' class="member">errorlevel</tt></b></dt> |
| 274 | <dd> |
| 275 | If <code>0</code> (the default), all errors are ignored when using |
| 276 | <tt class="method">extract()</tt>. Nevertheless, they appear as error messages |
| 277 | in the debug output, when debugging is enabled. If <code>1</code>, all |
| 278 | <em>fatal</em> errors are raised as <tt class="exception">OSError</tt> or |
| 279 | <tt class="exception">IOError</tt> exceptions. If <code>2</code>, all <em>non-fatal</em> |
| 280 | errors are raised as <tt class="exception">TarError</tt> exceptions as well. |
| 281 | </dl> |
| 282 | |
| 283 | <P> |
| 284 | |
| 285 | <DIV CLASS="navigation"> |
| 286 | <div class='online-navigation'> |
| 287 | <p></p><hr /> |
| 288 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 289 | <tr> |
| 290 | <td class='online-navigation'><a rel="prev" title="7.19 tarfile " |
| 291 | href="module-tarfile.html"><img src='../icons/previous.png' |
| 292 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 293 | <td class='online-navigation'><a rel="parent" title="7.19 tarfile " |
| 294 | href="module-tarfile.html"><img src='../icons/up.png' |
| 295 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 296 | <td class='online-navigation'><a rel="next" title="7.19.2 TarInfo Objects" |
| 297 | href="tarinfo-objects.html"><img src='../icons/next.png' |
| 298 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 299 | <td align="center" width="100%">Python Library Reference</td> |
| 300 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 301 | href="contents.html"><img src='../icons/contents.png' |
| 302 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 303 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 304 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 305 | <td class='online-navigation'><a rel="index" title="Index" |
| 306 | href="genindex.html"><img src='../icons/index.png' |
| 307 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 308 | </tr></table> |
| 309 | <div class='online-navigation'> |
| 310 | <b class="navlabel">Previous:</b> |
| 311 | <a class="sectref" rel="prev" href="module-tarfile.html">7.19 tarfile </A> |
| 312 | <b class="navlabel">Up:</b> |
| 313 | <a class="sectref" rel="parent" href="module-tarfile.html">7.19 tarfile </A> |
| 314 | <b class="navlabel">Next:</b> |
| 315 | <a class="sectref" rel="next" href="tarinfo-objects.html">7.19.2 TarInfo Objects</A> |
| 316 | </div> |
| 317 | </div> |
| 318 | <hr /> |
| 319 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 320 | </DIV> |
| 321 | <!--End of Navigation Panel--> |
| 322 | <ADDRESS> |
| 323 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 324 | </ADDRESS> |
| 325 | </BODY> |
| 326 | </HTML> |