blob: b33ac606ca291b551847c2d1e06506cc6c3af426 [file] [log] [blame]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001\section{\module{tarfile} --- Read and write tar archive files}
2
3\declaremodule{standard}{tarfile}
4\modulesynopsis{Read and write tar-format archive files.}
5\versionadded{2.3}
6
7\moduleauthor{Lars Gust\"abel}{lars@gustaebel.de}
8\sectionauthor{Lars Gust\"abel}{lars@gustaebel.de}
9
10The \module{tarfile} module makes it possible to read and create tar archives.
11Some facts and figures:
12
13\begin{itemize}
14\item reads and writes \module{gzip} and \module{bzip2} compressed archives.
Fred Drake3bbd1152004-01-13 23:41:32 +000015\item creates \POSIX{} 1003.1-1990 compliant or GNU tar compatible archives.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000016\item reads GNU tar extensions \emph{longname}, \emph{longlink} and
17 \emph{sparse}.
18\item stores pathnames of unlimited length using GNU tar extensions.
19\item handles directories, regular files, hardlinks, symbolic links, fifos,
20 character devices and block devices and is able to acquire and
21 restore file information like timestamp, access permissions and owner.
22\item can handle tape devices.
23\end{itemize}
24
25\begin{funcdesc}{open}{\optional{name\optional{, mode
26 \optional{, fileobj\optional{, bufsize}}}}}
27 Return a \class{TarFile} object for the pathname \var{name}.
28 For detailed information on \class{TarFile} objects,
29 see \citetitle{TarFile Objects} (section \ref{tarfile-objects}).
30
31 \var{mode} has to be a string of the form \code{'filemode[:compression]'},
32 it defaults to \code{'r'}. Here is a full list of mode combinations:
33
34 \begin{tableii}{c|l}{code}{mode}{action}
Martin v. Löwis78be7df2005-03-05 12:47:42 +000035 \lineii{'r' or 'r:*'}{Open for reading with transparent compression (recommended).}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000036 \lineii{'r:'}{Open for reading exclusively without compression.}
37 \lineii{'r:gz'}{Open for reading with gzip compression.}
38 \lineii{'r:bz2'}{Open for reading with bzip2 compression.}
39 \lineii{'a' or 'a:'}{Open for appending with no compression.}
40 \lineii{'w' or 'w:'}{Open for uncompressed writing.}
41 \lineii{'w:gz'}{Open for gzip compressed writing.}
42 \lineii{'w:bz2'}{Open for bzip2 compressed writing.}
43 \end{tableii}
44
45 Note that \code{'a:gz'} or \code{'a:bz2'} is not possible.
46 If \var{mode} is not suitable to open a certain (compressed) file for
47 reading, \exception{ReadError} is raised. Use \var{mode} \code{'r'} to
48 avoid this. If a compression method is not supported,
49 \exception{CompressionError} is raised.
50
Lars Gustäbelf5bf3b02007-02-12 09:27:10 +000051 If \var{fileobj} is specified, it is used as an alternative to a file
52 object opened for \var{name}. It is supposed to be at position 0.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000053
54 For special purposes, there is a second format for \var{mode}:
Fred Drake3bbd1152004-01-13 23:41:32 +000055 \code{'filemode|[compression]'}. \function{open()} will return a
56 \class{TarFile} object that processes its data as a stream of
57 blocks. No random seeking will be done on the file. If given,
58 \var{fileobj} may be any object that has a \method{read()} or
59 \method{write()} method (depending on the \var{mode}).
60 \var{bufsize} specifies the blocksize and defaults to \code{20 *
61 512} bytes. Use this variant in combination with
62 e.g. \code{sys.stdin}, a socket file object or a tape device.
63 However, such a \class{TarFile} object is limited in that it does
64 not allow to be accessed randomly, see ``Examples''
65 (section~\ref{tar-examples}). The currently possible modes:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000066
Fred Drake3bbd1152004-01-13 23:41:32 +000067 \begin{tableii}{c|l}{code}{Mode}{Action}
Martin v. Löwis78be7df2005-03-05 12:47:42 +000068 \lineii{'r|*'}{Open a \emph{stream} of tar blocks for reading with transparent compression.}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000069 \lineii{'r|'}{Open a \emph{stream} of uncompressed tar blocks for reading.}
70 \lineii{'r|gz'}{Open a gzip compressed \emph{stream} for reading.}
71 \lineii{'r|bz2'}{Open a bzip2 compressed \emph{stream} for reading.}
72 \lineii{'w|'}{Open an uncompressed \emph{stream} for writing.}
73 \lineii{'w|gz'}{Open an gzip compressed \emph{stream} for writing.}
74 \lineii{'w|bz2'}{Open an bzip2 compressed \emph{stream} for writing.}
75 \end{tableii}
76\end{funcdesc}
77
78\begin{classdesc*}{TarFile}
79 Class for reading and writing tar archives. Do not use this
80 class directly, better use \function{open()} instead.
Fred Drake3bbd1152004-01-13 23:41:32 +000081 See ``TarFile Objects'' (section~\ref{tarfile-objects}).
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000082\end{classdesc*}
83
84\begin{funcdesc}{is_tarfile}{name}
Fred Drake3bbd1152004-01-13 23:41:32 +000085 Return \constant{True} if \var{name} is a tar archive file, that
86 the \module{tarfile} module can read.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000087\end{funcdesc}
88
89\begin{classdesc}{TarFileCompat}{filename\optional{, mode\optional{,
Fred Drake3bbd1152004-01-13 23:41:32 +000090 compression}}}
91 Class for limited access to tar archives with a
92 \refmodule{zipfile}-like interface. Please consult the
93 documentation of the \refmodule{zipfile} module for more details.
94 \var{compression} must be one of the following constants:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000095 \begin{datadesc}{TAR_PLAIN}
96 Constant for an uncompressed tar archive.
97 \end{datadesc}
98 \begin{datadesc}{TAR_GZIPPED}
Fred Drake3bbd1152004-01-13 23:41:32 +000099 Constant for a \refmodule{gzip} compressed tar archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000100 \end{datadesc}
101\end{classdesc}
102
103\begin{excdesc}{TarError}
104 Base class for all \module{tarfile} exceptions.
105\end{excdesc}
106
107\begin{excdesc}{ReadError}
108 Is raised when a tar archive is opened, that either cannot be handled by
109 the \module{tarfile} module or is somehow invalid.
110\end{excdesc}
111
112\begin{excdesc}{CompressionError}
113 Is raised when a compression method is not supported or when the data
114 cannot be decoded properly.
115\end{excdesc}
116
117\begin{excdesc}{StreamError}
118 Is raised for the limitations that are typical for stream-like
119 \class{TarFile} objects.
120\end{excdesc}
121
122\begin{excdesc}{ExtractError}
123 Is raised for \emph{non-fatal} errors when using \method{extract()}, but
124 only if \member{TarFile.errorlevel}\code{ == 2}.
125\end{excdesc}
126
127\begin{seealso}
Fred Drake3bbd1152004-01-13 23:41:32 +0000128 \seemodule{zipfile}{Documentation of the \refmodule{zipfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000129 standard module.}
130
George Yoshidad7716722006-04-28 16:40:14 +0000131 \seetitle[http://www.gnu.org/software/tar/manual/html_node/tar_134.html\#SEC134]
Georg Brandl9a19e5c2005-08-27 17:10:35 +0000132 {GNU tar manual, Basic Tar Format}{Documentation for tar archive files,
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000133 including GNU tar extensions.}
134\end{seealso}
135
136%-----------------
137% TarFile Objects
138%-----------------
139
140\subsection{TarFile Objects \label{tarfile-objects}}
141
142The \class{TarFile} object provides an interface to a tar archive. A tar
143archive is a sequence of blocks. An archive member (a stored file) is made up
144of a header block followed by data blocks. It is possible, to store a file in a
145tar archive several times. Each archive member is represented by a
146\class{TarInfo} object, see \citetitle{TarInfo Objects} (section
147\ref{tarinfo-objects}) for details.
148
149\begin{classdesc}{TarFile}{\optional{name
150 \optional{, mode\optional{, fileobj}}}}
151 Open an \emph{(uncompressed)} tar archive \var{name}.
152 \var{mode} is either \code{'r'} to read from an existing archive,
153 \code{'a'} to append data to an existing file or \code{'w'} to create a new
154 file overwriting an existing one. \var{mode} defaults to \code{'r'}.
155
156 If \var{fileobj} is given, it is used for reading or writing data.
157 If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode.
Lars Gustäbelf5bf3b02007-02-12 09:27:10 +0000158 \var{fileobj} will be used from position 0.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000159 \begin{notice}
160 \var{fileobj} is not closed, when \class{TarFile} is closed.
161 \end{notice}
162\end{classdesc}
163
164\begin{methoddesc}{open}{...}
165 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000166 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000167 for details.
168\end{methoddesc}
169
170\begin{methoddesc}{getmember}{name}
171 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
172 not be found in the archive, \exception{KeyError} is raised.
173 \begin{notice}
174 If a member occurs more than once in the archive, its last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000175 occurrence is assumed to be the most up-to-date version.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000176 \end{notice}
177\end{methoddesc}
178
179\begin{methoddesc}{getmembers}{}
180 Return the members of the archive as a list of \class{TarInfo} objects.
181 The list has the same order as the members in the archive.
182\end{methoddesc}
183
184\begin{methoddesc}{getnames}{}
185 Return the members as a list of their names. It has the same order as
186 the list returned by \method{getmembers()}.
187\end{methoddesc}
188
189\begin{methoddesc}{list}{verbose=True}
190 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000191 \constant{False}, only the names of the members are printed. If it is
192 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000193\end{methoddesc}
194
195\begin{methoddesc}{next}{}
196 Return the next member of the archive as a \class{TarInfo} object, when
197 \class{TarFile} is opened for reading. Return \code{None} if there is no
198 more available.
199\end{methoddesc}
200
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000201\begin{methoddesc}{extractall}{\optional{path\optional{, members}}}
202 Extract all members from the archive to the current working directory
203 or directory \var{path}. If optional \var{members} is given, it must be
204 a subset of the list returned by \method{getmembers()}.
205 Directory informations like owner, modification time and permissions are
206 set after all members have been extracted. This is done to work around two
207 problems: A directory's modification time is reset each time a file is
208 created in it. And, if a directory's permissions do not allow writing,
209 extracting files to it will fail.
210 \versionadded{2.5}
211\end{methoddesc}
212
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000213\begin{methoddesc}{extract}{member\optional{, path}}
214 Extract a member from the archive to the current working directory,
215 using its full name. Its file information is extracted as accurately as
216 possible.
217 \var{member} may be a filename or a \class{TarInfo} object.
218 You can specify a different directory using \var{path}.
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000219 \begin{notice}
220 Because the \method{extract()} method allows random access to a tar
221 archive there are some issues you must take care of yourself. See the
222 description for \method{extractall()} above.
223 \end{notice}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000224\end{methoddesc}
225
226\begin{methoddesc}{extractfile}{member}
227 Extract a member from the archive as a file object.
228 \var{member} may be a filename or a \class{TarInfo} object.
229 If \var{member} is a regular file, a file-like object is returned.
230 If \var{member} is a link, a file-like object is constructed from the
231 link's target.
232 If \var{member} is none of the above, \code{None} is returned.
233 \begin{notice}
234 The file-like object is read-only and provides the following methods:
235 \method{read()}, \method{readline()}, \method{readlines()},
236 \method{seek()}, \method{tell()}.
237 \end{notice}
238\end{methoddesc}
239
Fred Drake3bbd1152004-01-13 23:41:32 +0000240\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000241 Add the file \var{name} to the archive. \var{name} may be any type
242 of file (directory, fifo, symbolic link, etc.).
243 If given, \var{arcname} specifies an alternative name for the file in the
244 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000245 This can be avoided by setting \var{recursive} to \constant{False};
246 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000247\end{methoddesc}
248
249\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
250 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000251 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000252 from it and added to the archive. You can create \class{TarInfo} objects
253 using \method{gettarinfo()}.
254 \begin{notice}
255 On Windows platforms, \var{fileobj} should always be opened with mode
256 \code{'rb'} to avoid irritation about the file size.
257 \end{notice}
258\end{methoddesc}
259
Fred Drake3bbd1152004-01-13 23:41:32 +0000260\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
261 arcname\optional{, fileobj}}}}
262 Create a \class{TarInfo} object for either the file \var{name} or
263 the file object \var{fileobj} (using \function{os.fstat()} on its
264 file descriptor). You can modify some of the \class{TarInfo}'s
265 attributes before you add it using \method{addfile()}. If given,
266 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000267 archive.
268\end{methoddesc}
269
270\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000271 Close the \class{TarFile}. In write mode, two finishing zero
272 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000273\end{methoddesc}
274
Fred Drake3bbd1152004-01-13 23:41:32 +0000275\begin{memberdesc}{posix}
276 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
277 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000278 standard. This limits the length of filenames to at most 256,
279 link names to 100 characters and the maximum file size to 8
280 gigabytes. A \exception{ValueError} is raised if a file exceeds
281 this limit. If false, create a GNU tar compatible archive. It
282 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000283 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000284 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000285\end{memberdesc}
286
Fred Drake3bbd1152004-01-13 23:41:32 +0000287\begin{memberdesc}{dereference}
288 If false, add symbolic and hard links to archive. If true, add the
289 content of the target files to the archive. This has no effect on
290 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000291\end{memberdesc}
292
Fred Drake3bbd1152004-01-13 23:41:32 +0000293\begin{memberdesc}{ignore_zeros}
294 If false, treat an empty block as the end of the archive. If true,
295 skip empty (and invalid) blocks and try to get as many members as
296 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000297 archives.
298\end{memberdesc}
299
300\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000301 To be set from \code{0} (no debug messages; the default) up to
302 \code{3} (all debug messages). The messages are written to
Georg Brandl208eec22005-07-12 07:28:20 +0000303 \code{sys.stderr}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000304\end{memberdesc}
305
Fred Drake3bbd1152004-01-13 23:41:32 +0000306\begin{memberdesc}{errorlevel}
307 If \code{0} (the default), all errors are ignored when using
308 \method{extract()}. Nevertheless, they appear as error messages
309 in the debug output, when debugging is enabled. If \code{1}, all
310 \emph{fatal} errors are raised as \exception{OSError} or
311 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
312 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000313\end{memberdesc}
314
315%-----------------
316% TarInfo Objects
317%-----------------
318
319\subsection{TarInfo Objects \label{tarinfo-objects}}
320
Fred Drake3bbd1152004-01-13 23:41:32 +0000321A \class{TarInfo} object represents one member in a
322\class{TarFile}. Aside from storing all required attributes of a file
323(like file type, size, time, permissions, owner etc.), it provides
324some useful methods to determine its type. It does \emph{not} contain
325the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000326
Fred Drake3bbd1152004-01-13 23:41:32 +0000327\class{TarInfo} objects are returned by \class{TarFile}'s methods
328\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000329
330\begin{classdesc}{TarInfo}{\optional{name}}
331 Create a \class{TarInfo} object.
332\end{classdesc}
333
334\begin{methoddesc}{frombuf}{}
335 Create and return a \class{TarInfo} object from a string buffer.
336\end{methoddesc}
337
Georg Brandl38c6a222006-05-10 16:26:03 +0000338\begin{methoddesc}{tobuf}{posix}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000339 Create a string buffer from a \class{TarInfo} object.
Georg Brandl38c6a222006-05-10 16:26:03 +0000340 See \class{TarFile}'s \member{posix} attribute for information
341 on the \var{posix} argument. It defaults to \constant{False}.
342
343 \versionadded[The \var{posix} parameter]{2.5}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000344\end{methoddesc}
345
346A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000347
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000348\begin{memberdesc}{name}
349 Name of the archive member.
350\end{memberdesc}
351
352\begin{memberdesc}{size}
353 Size in bytes.
354\end{memberdesc}
355
356\begin{memberdesc}{mtime}
357 Time of last modification.
358\end{memberdesc}
359
360\begin{memberdesc}{mode}
361 Permission bits.
362\end{memberdesc}
363
364\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000365 File type. \var{type} is usually one of these constants:
366 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
367 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
368 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
369 \constant{GNUTYPE_SPARSE}. To determine the type of a
370 \class{TarInfo} object more conveniently, use the \code{is_*()}
371 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000372\end{memberdesc}
373
374\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000375 Name of the target file name, which is only present in
376 \class{TarInfo} objects of type \constant{LNKTYPE} and
377 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000378\end{memberdesc}
379
Fred Drake3bbd1152004-01-13 23:41:32 +0000380\begin{memberdesc}{uid}
381 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000382\end{memberdesc}
383
Fred Drake3bbd1152004-01-13 23:41:32 +0000384\begin{memberdesc}{gid}
385 Group ID of the user who originally stored this member.
386\end{memberdesc}
387
388\begin{memberdesc}{uname}
389 User name.
390\end{memberdesc}
391
392\begin{memberdesc}{gname}
393 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000394\end{memberdesc}
395
396A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000397
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000398\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000399 Return \constant{True} if the \class{Tarinfo} object is a regular
400 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000401\end{methoddesc}
402
403\begin{methoddesc}{isreg}{}
404 Same as \method{isfile()}.
405\end{methoddesc}
406
407\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000408 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000409\end{methoddesc}
410
411\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000412 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000413\end{methoddesc}
414
415\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000416 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000417\end{methoddesc}
418
419\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000420 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000421\end{methoddesc}
422
423\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000424 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000425\end{methoddesc}
426
427\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000428 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000429\end{methoddesc}
430
431\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000432 Return \constant{True} if it is one of character device, block
433 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000434\end{methoddesc}
435
436%------------------------
437% Examples
438%------------------------
439
440\subsection{Examples \label{tar-examples}}
441
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000442How to extract an entire tar archive to the current working directory:
443\begin{verbatim}
444import tarfile
445tar = tarfile.open("sample.tar.gz")
446tar.extractall()
447tar.close()
448\end{verbatim}
449
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000450How to create an uncompressed tar archive from a list of filenames:
451\begin{verbatim}
452import tarfile
453tar = tarfile.open("sample.tar", "w")
454for name in ["foo", "bar", "quux"]:
455 tar.add(name)
456tar.close()
457\end{verbatim}
458
459How to read a gzip compressed tar archive and display some member information:
460\begin{verbatim}
461import tarfile
462tar = tarfile.open("sample.tar.gz", "r:gz")
463for tarinfo in tar:
464 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
465 if tarinfo.isreg():
466 print "a regular file."
467 elif tarinfo.isdir():
468 print "a directory."
469 else:
470 print "something else."
471tar.close()
472\end{verbatim}
473
474How to create a tar archive with faked information:
475\begin{verbatim}
476import tarfile
477tar = tarfile.open("sample.tar.gz", "w:gz")
478for name in namelist:
479 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
480 tarinfo.uid = 123
481 tarinfo.gid = 456
482 tarinfo.uname = "johndoe"
483 tarinfo.gname = "fake"
484 tar.addfile(tarinfo, file(name))
485tar.close()
486\end{verbatim}
487
488The \emph{only} way to extract an uncompressed tar stream from
489\code{sys.stdin}:
490\begin{verbatim}
491import sys
492import tarfile
493tar = tarfile.open(mode="r|", fileobj=sys.stdin)
494for tarinfo in tar:
495 tar.extract(tarinfo)
496tar.close()
497\end{verbatim}