blob: 1a53a27e76ac67901d41d9049302e7a186d9fa7f [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.}
Lars Gustäbel3f8aca12007-02-06 18:38:13 +000039 \lineii{'a' or 'a:'}{Open for appending with no compression. The file
40 is created if it does not exist.}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000041 \lineii{'w' or 'w:'}{Open for uncompressed writing.}
42 \lineii{'w:gz'}{Open for gzip compressed writing.}
43 \lineii{'w:bz2'}{Open for bzip2 compressed writing.}
44 \end{tableii}
45
46 Note that \code{'a:gz'} or \code{'a:bz2'} is not possible.
47 If \var{mode} is not suitable to open a certain (compressed) file for
48 reading, \exception{ReadError} is raised. Use \var{mode} \code{'r'} to
49 avoid this. If a compression method is not supported,
50 \exception{CompressionError} is raised.
51
52 If \var{fileobj} is specified, it is used as an alternative to
53 a file object opened for \var{name}.
54
55 For special purposes, there is a second format for \var{mode}:
Fred Drake3bbd1152004-01-13 23:41:32 +000056 \code{'filemode|[compression]'}. \function{open()} will return a
57 \class{TarFile} object that processes its data as a stream of
58 blocks. No random seeking will be done on the file. If given,
59 \var{fileobj} may be any object that has a \method{read()} or
60 \method{write()} method (depending on the \var{mode}).
61 \var{bufsize} specifies the blocksize and defaults to \code{20 *
62 512} bytes. Use this variant in combination with
63 e.g. \code{sys.stdin}, a socket file object or a tape device.
64 However, such a \class{TarFile} object is limited in that it does
65 not allow to be accessed randomly, see ``Examples''
66 (section~\ref{tar-examples}). The currently possible modes:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000067
Fred Drake3bbd1152004-01-13 23:41:32 +000068 \begin{tableii}{c|l}{code}{Mode}{Action}
Martin v. Löwis78be7df2005-03-05 12:47:42 +000069 \lineii{'r|*'}{Open a \emph{stream} of tar blocks for reading with transparent compression.}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000070 \lineii{'r|'}{Open a \emph{stream} of uncompressed tar blocks for reading.}
71 \lineii{'r|gz'}{Open a gzip compressed \emph{stream} for reading.}
72 \lineii{'r|bz2'}{Open a bzip2 compressed \emph{stream} for reading.}
73 \lineii{'w|'}{Open an uncompressed \emph{stream} for writing.}
74 \lineii{'w|gz'}{Open an gzip compressed \emph{stream} for writing.}
75 \lineii{'w|bz2'}{Open an bzip2 compressed \emph{stream} for writing.}
76 \end{tableii}
77\end{funcdesc}
78
79\begin{classdesc*}{TarFile}
80 Class for reading and writing tar archives. Do not use this
81 class directly, better use \function{open()} instead.
Fred Drake3bbd1152004-01-13 23:41:32 +000082 See ``TarFile Objects'' (section~\ref{tarfile-objects}).
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000083\end{classdesc*}
84
85\begin{funcdesc}{is_tarfile}{name}
Fred Drake3bbd1152004-01-13 23:41:32 +000086 Return \constant{True} if \var{name} is a tar archive file, that
87 the \module{tarfile} module can read.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000088\end{funcdesc}
89
90\begin{classdesc}{TarFileCompat}{filename\optional{, mode\optional{,
Fred Drake3bbd1152004-01-13 23:41:32 +000091 compression}}}
92 Class for limited access to tar archives with a
93 \refmodule{zipfile}-like interface. Please consult the
94 documentation of the \refmodule{zipfile} module for more details.
95 \var{compression} must be one of the following constants:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000096 \begin{datadesc}{TAR_PLAIN}
97 Constant for an uncompressed tar archive.
98 \end{datadesc}
99 \begin{datadesc}{TAR_GZIPPED}
Fred Drake3bbd1152004-01-13 23:41:32 +0000100 Constant for a \refmodule{gzip} compressed tar archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000101 \end{datadesc}
102\end{classdesc}
103
104\begin{excdesc}{TarError}
105 Base class for all \module{tarfile} exceptions.
106\end{excdesc}
107
108\begin{excdesc}{ReadError}
109 Is raised when a tar archive is opened, that either cannot be handled by
110 the \module{tarfile} module or is somehow invalid.
111\end{excdesc}
112
113\begin{excdesc}{CompressionError}
114 Is raised when a compression method is not supported or when the data
115 cannot be decoded properly.
116\end{excdesc}
117
118\begin{excdesc}{StreamError}
119 Is raised for the limitations that are typical for stream-like
120 \class{TarFile} objects.
121\end{excdesc}
122
123\begin{excdesc}{ExtractError}
124 Is raised for \emph{non-fatal} errors when using \method{extract()}, but
125 only if \member{TarFile.errorlevel}\code{ == 2}.
126\end{excdesc}
127
Georg Brandlebbeed72006-12-19 22:06:46 +0000128\begin{excdesc}{HeaderError}
129 Is raised by \method{frombuf()} if the buffer it gets is invalid.
130 \versionadded{2.6}
131\end{excdesc}
132
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000133\begin{seealso}
Fred Drake3bbd1152004-01-13 23:41:32 +0000134 \seemodule{zipfile}{Documentation of the \refmodule{zipfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000135 standard module.}
136
George Yoshidad7716722006-04-28 16:40:14 +0000137 \seetitle[http://www.gnu.org/software/tar/manual/html_node/tar_134.html\#SEC134]
Georg Brandl9a19e5c2005-08-27 17:10:35 +0000138 {GNU tar manual, Basic Tar Format}{Documentation for tar archive files,
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000139 including GNU tar extensions.}
140\end{seealso}
141
142%-----------------
143% TarFile Objects
144%-----------------
145
146\subsection{TarFile Objects \label{tarfile-objects}}
147
148The \class{TarFile} object provides an interface to a tar archive. A tar
149archive is a sequence of blocks. An archive member (a stored file) is made up
150of a header block followed by data blocks. It is possible, to store a file in a
151tar archive several times. Each archive member is represented by a
152\class{TarInfo} object, see \citetitle{TarInfo Objects} (section
153\ref{tarinfo-objects}) for details.
154
155\begin{classdesc}{TarFile}{\optional{name
156 \optional{, mode\optional{, fileobj}}}}
157 Open an \emph{(uncompressed)} tar archive \var{name}.
158 \var{mode} is either \code{'r'} to read from an existing archive,
159 \code{'a'} to append data to an existing file or \code{'w'} to create a new
160 file overwriting an existing one. \var{mode} defaults to \code{'r'}.
161
162 If \var{fileobj} is given, it is used for reading or writing data.
163 If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode.
164 \begin{notice}
165 \var{fileobj} is not closed, when \class{TarFile} is closed.
166 \end{notice}
167\end{classdesc}
168
169\begin{methoddesc}{open}{...}
170 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000171 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000172 for details.
173\end{methoddesc}
174
175\begin{methoddesc}{getmember}{name}
176 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
177 not be found in the archive, \exception{KeyError} is raised.
178 \begin{notice}
179 If a member occurs more than once in the archive, its last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000180 occurrence is assumed to be the most up-to-date version.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000181 \end{notice}
182\end{methoddesc}
183
184\begin{methoddesc}{getmembers}{}
185 Return the members of the archive as a list of \class{TarInfo} objects.
186 The list has the same order as the members in the archive.
187\end{methoddesc}
188
189\begin{methoddesc}{getnames}{}
190 Return the members as a list of their names. It has the same order as
191 the list returned by \method{getmembers()}.
192\end{methoddesc}
193
194\begin{methoddesc}{list}{verbose=True}
195 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000196 \constant{False}, only the names of the members are printed. If it is
197 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000198\end{methoddesc}
199
200\begin{methoddesc}{next}{}
201 Return the next member of the archive as a \class{TarInfo} object, when
202 \class{TarFile} is opened for reading. Return \code{None} if there is no
203 more available.
204\end{methoddesc}
205
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000206\begin{methoddesc}{extractall}{\optional{path\optional{, members}}}
207 Extract all members from the archive to the current working directory
208 or directory \var{path}. If optional \var{members} is given, it must be
209 a subset of the list returned by \method{getmembers()}.
210 Directory informations like owner, modification time and permissions are
211 set after all members have been extracted. This is done to work around two
212 problems: A directory's modification time is reset each time a file is
213 created in it. And, if a directory's permissions do not allow writing,
214 extracting files to it will fail.
215 \versionadded{2.5}
216\end{methoddesc}
217
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000218\begin{methoddesc}{extract}{member\optional{, path}}
219 Extract a member from the archive to the current working directory,
220 using its full name. Its file information is extracted as accurately as
221 possible.
222 \var{member} may be a filename or a \class{TarInfo} object.
223 You can specify a different directory using \var{path}.
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000224 \begin{notice}
225 Because the \method{extract()} method allows random access to a tar
226 archive there are some issues you must take care of yourself. See the
227 description for \method{extractall()} above.
228 \end{notice}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000229\end{methoddesc}
230
231\begin{methoddesc}{extractfile}{member}
232 Extract a member from the archive as a file object.
233 \var{member} may be a filename or a \class{TarInfo} object.
234 If \var{member} is a regular file, a file-like object is returned.
235 If \var{member} is a link, a file-like object is constructed from the
236 link's target.
237 If \var{member} is none of the above, \code{None} is returned.
238 \begin{notice}
239 The file-like object is read-only and provides the following methods:
240 \method{read()}, \method{readline()}, \method{readlines()},
241 \method{seek()}, \method{tell()}.
242 \end{notice}
243\end{methoddesc}
244
Fred Drake3bbd1152004-01-13 23:41:32 +0000245\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000246 Add the file \var{name} to the archive. \var{name} may be any type
247 of file (directory, fifo, symbolic link, etc.).
248 If given, \var{arcname} specifies an alternative name for the file in the
249 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000250 This can be avoided by setting \var{recursive} to \constant{False};
251 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000252\end{methoddesc}
253
254\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
255 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000256 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000257 from it and added to the archive. You can create \class{TarInfo} objects
258 using \method{gettarinfo()}.
259 \begin{notice}
260 On Windows platforms, \var{fileobj} should always be opened with mode
261 \code{'rb'} to avoid irritation about the file size.
262 \end{notice}
263\end{methoddesc}
264
Fred Drake3bbd1152004-01-13 23:41:32 +0000265\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
266 arcname\optional{, fileobj}}}}
267 Create a \class{TarInfo} object for either the file \var{name} or
268 the file object \var{fileobj} (using \function{os.fstat()} on its
269 file descriptor). You can modify some of the \class{TarInfo}'s
270 attributes before you add it using \method{addfile()}. If given,
271 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000272 archive.
273\end{methoddesc}
274
275\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000276 Close the \class{TarFile}. In write mode, two finishing zero
277 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000278\end{methoddesc}
279
Fred Drake3bbd1152004-01-13 23:41:32 +0000280\begin{memberdesc}{posix}
281 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
282 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000283 standard. This limits the length of filenames to at most 256,
284 link names to 100 characters and the maximum file size to 8
285 gigabytes. A \exception{ValueError} is raised if a file exceeds
286 this limit. If false, create a GNU tar compatible archive. It
287 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000288 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000289 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000290\end{memberdesc}
291
Fred Drake3bbd1152004-01-13 23:41:32 +0000292\begin{memberdesc}{dereference}
293 If false, add symbolic and hard links to archive. If true, add the
294 content of the target files to the archive. This has no effect on
295 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000296\end{memberdesc}
297
Fred Drake3bbd1152004-01-13 23:41:32 +0000298\begin{memberdesc}{ignore_zeros}
299 If false, treat an empty block as the end of the archive. If true,
300 skip empty (and invalid) blocks and try to get as many members as
301 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000302 archives.
303\end{memberdesc}
304
305\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000306 To be set from \code{0} (no debug messages; the default) up to
307 \code{3} (all debug messages). The messages are written to
Georg Brandl208eec22005-07-12 07:28:20 +0000308 \code{sys.stderr}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000309\end{memberdesc}
310
Fred Drake3bbd1152004-01-13 23:41:32 +0000311\begin{memberdesc}{errorlevel}
312 If \code{0} (the default), all errors are ignored when using
313 \method{extract()}. Nevertheless, they appear as error messages
314 in the debug output, when debugging is enabled. If \code{1}, all
315 \emph{fatal} errors are raised as \exception{OSError} or
316 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
317 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000318\end{memberdesc}
319
320%-----------------
321% TarInfo Objects
322%-----------------
323
324\subsection{TarInfo Objects \label{tarinfo-objects}}
325
Fred Drake3bbd1152004-01-13 23:41:32 +0000326A \class{TarInfo} object represents one member in a
327\class{TarFile}. Aside from storing all required attributes of a file
328(like file type, size, time, permissions, owner etc.), it provides
329some useful methods to determine its type. It does \emph{not} contain
330the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000331
Fred Drake3bbd1152004-01-13 23:41:32 +0000332\class{TarInfo} objects are returned by \class{TarFile}'s methods
333\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000334
335\begin{classdesc}{TarInfo}{\optional{name}}
336 Create a \class{TarInfo} object.
337\end{classdesc}
338
339\begin{methoddesc}{frombuf}{}
340 Create and return a \class{TarInfo} object from a string buffer.
Georg Brandlebbeed72006-12-19 22:06:46 +0000341 \versionadded[Raises \exception{HeaderError} if the buffer is
342 invalid.]{2.6}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000343\end{methoddesc}
344
Georg Brandl38c6a222006-05-10 16:26:03 +0000345\begin{methoddesc}{tobuf}{posix}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000346 Create a string buffer from a \class{TarInfo} object.
Georg Brandl38c6a222006-05-10 16:26:03 +0000347 See \class{TarFile}'s \member{posix} attribute for information
348 on the \var{posix} argument. It defaults to \constant{False}.
349
350 \versionadded[The \var{posix} parameter]{2.5}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000351\end{methoddesc}
352
353A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000354
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000355\begin{memberdesc}{name}
356 Name of the archive member.
357\end{memberdesc}
358
359\begin{memberdesc}{size}
360 Size in bytes.
361\end{memberdesc}
362
363\begin{memberdesc}{mtime}
364 Time of last modification.
365\end{memberdesc}
366
367\begin{memberdesc}{mode}
368 Permission bits.
369\end{memberdesc}
370
371\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000372 File type. \var{type} is usually one of these constants:
373 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
374 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
375 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
376 \constant{GNUTYPE_SPARSE}. To determine the type of a
377 \class{TarInfo} object more conveniently, use the \code{is_*()}
378 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000379\end{memberdesc}
380
381\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000382 Name of the target file name, which is only present in
383 \class{TarInfo} objects of type \constant{LNKTYPE} and
384 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000385\end{memberdesc}
386
Fred Drake3bbd1152004-01-13 23:41:32 +0000387\begin{memberdesc}{uid}
388 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000389\end{memberdesc}
390
Fred Drake3bbd1152004-01-13 23:41:32 +0000391\begin{memberdesc}{gid}
392 Group ID of the user who originally stored this member.
393\end{memberdesc}
394
395\begin{memberdesc}{uname}
396 User name.
397\end{memberdesc}
398
399\begin{memberdesc}{gname}
400 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000401\end{memberdesc}
402
403A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000404
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000405\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000406 Return \constant{True} if the \class{Tarinfo} object is a regular
407 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000408\end{methoddesc}
409
410\begin{methoddesc}{isreg}{}
411 Same as \method{isfile()}.
412\end{methoddesc}
413
414\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000415 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000416\end{methoddesc}
417
418\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000419 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000420\end{methoddesc}
421
422\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000423 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000424\end{methoddesc}
425
426\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000427 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000428\end{methoddesc}
429
430\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000431 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000432\end{methoddesc}
433
434\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000435 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000436\end{methoddesc}
437
438\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000439 Return \constant{True} if it is one of character device, block
440 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000441\end{methoddesc}
442
443%------------------------
444% Examples
445%------------------------
446
447\subsection{Examples \label{tar-examples}}
448
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000449How to extract an entire tar archive to the current working directory:
450\begin{verbatim}
451import tarfile
452tar = tarfile.open("sample.tar.gz")
453tar.extractall()
454tar.close()
455\end{verbatim}
456
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000457How to create an uncompressed tar archive from a list of filenames:
458\begin{verbatim}
459import tarfile
460tar = tarfile.open("sample.tar", "w")
461for name in ["foo", "bar", "quux"]:
462 tar.add(name)
463tar.close()
464\end{verbatim}
465
466How to read a gzip compressed tar archive and display some member information:
467\begin{verbatim}
468import tarfile
469tar = tarfile.open("sample.tar.gz", "r:gz")
470for tarinfo in tar:
471 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
472 if tarinfo.isreg():
473 print "a regular file."
474 elif tarinfo.isdir():
475 print "a directory."
476 else:
477 print "something else."
478tar.close()
479\end{verbatim}
480
481How to create a tar archive with faked information:
482\begin{verbatim}
483import tarfile
484tar = tarfile.open("sample.tar.gz", "w:gz")
485for name in namelist:
486 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
487 tarinfo.uid = 123
488 tarinfo.gid = 456
489 tarinfo.uname = "johndoe"
490 tarinfo.gname = "fake"
491 tar.addfile(tarinfo, file(name))
492tar.close()
493\end{verbatim}
494
495The \emph{only} way to extract an uncompressed tar stream from
496\code{sys.stdin}:
497\begin{verbatim}
498import sys
499import tarfile
500tar = tarfile.open(mode="r|", fileobj=sys.stdin)
501for tarinfo in tar:
502 tar.extract(tarinfo)
503tar.close()
504\end{verbatim}