blob: d1731642485517ba7b5015dc79250fc77b546b0e [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.
Lars Gustäbelb9ff6692007-08-30 20:25:13 +0000210 \begin{notice}[warning]
211 Never extract archives from untrusted sources without prior inspection.
212 It is possible that files are created outside of \var{path}, e.g. members
213 that have absolute filenames starting with \code{"/"} or filenames with
214 two dots \code{".."}.
215 \end{notice}
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000216 \versionadded{2.5}
217\end{methoddesc}
218
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000219\begin{methoddesc}{extract}{member\optional{, path}}
220 Extract a member from the archive to the current working directory,
221 using its full name. Its file information is extracted as accurately as
222 possible.
223 \var{member} may be a filename or a \class{TarInfo} object.
224 You can specify a different directory using \var{path}.
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000225 \begin{notice}
226 Because the \method{extract()} method allows random access to a tar
227 archive there are some issues you must take care of yourself. See the
228 description for \method{extractall()} above.
229 \end{notice}
Lars Gustäbelb9ff6692007-08-30 20:25:13 +0000230
231 \begin{notice}[warning]
232 See the warning for \method{extractall()}.
233 \end{notice}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000234\end{methoddesc}
235
236\begin{methoddesc}{extractfile}{member}
237 Extract a member from the archive as a file object.
238 \var{member} may be a filename or a \class{TarInfo} object.
239 If \var{member} is a regular file, a file-like object is returned.
240 If \var{member} is a link, a file-like object is constructed from the
241 link's target.
242 If \var{member} is none of the above, \code{None} is returned.
243 \begin{notice}
244 The file-like object is read-only and provides the following methods:
245 \method{read()}, \method{readline()}, \method{readlines()},
246 \method{seek()}, \method{tell()}.
247 \end{notice}
248\end{methoddesc}
249
Fred Drake3bbd1152004-01-13 23:41:32 +0000250\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000251 Add the file \var{name} to the archive. \var{name} may be any type
252 of file (directory, fifo, symbolic link, etc.).
253 If given, \var{arcname} specifies an alternative name for the file in the
254 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000255 This can be avoided by setting \var{recursive} to \constant{False};
256 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000257\end{methoddesc}
258
259\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
260 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000261 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000262 from it and added to the archive. You can create \class{TarInfo} objects
263 using \method{gettarinfo()}.
264 \begin{notice}
265 On Windows platforms, \var{fileobj} should always be opened with mode
266 \code{'rb'} to avoid irritation about the file size.
267 \end{notice}
268\end{methoddesc}
269
Fred Drake3bbd1152004-01-13 23:41:32 +0000270\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
271 arcname\optional{, fileobj}}}}
272 Create a \class{TarInfo} object for either the file \var{name} or
273 the file object \var{fileobj} (using \function{os.fstat()} on its
274 file descriptor). You can modify some of the \class{TarInfo}'s
275 attributes before you add it using \method{addfile()}. If given,
276 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000277 archive.
278\end{methoddesc}
279
280\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000281 Close the \class{TarFile}. In write mode, two finishing zero
282 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000283\end{methoddesc}
284
Fred Drake3bbd1152004-01-13 23:41:32 +0000285\begin{memberdesc}{posix}
286 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
287 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000288 standard. This limits the length of filenames to at most 256,
289 link names to 100 characters and the maximum file size to 8
290 gigabytes. A \exception{ValueError} is raised if a file exceeds
291 this limit. If false, create a GNU tar compatible archive. It
292 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000293 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000294 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000295\end{memberdesc}
296
Fred Drake3bbd1152004-01-13 23:41:32 +0000297\begin{memberdesc}{dereference}
298 If false, add symbolic and hard links to archive. If true, add the
299 content of the target files to the archive. This has no effect on
300 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000301\end{memberdesc}
302
Fred Drake3bbd1152004-01-13 23:41:32 +0000303\begin{memberdesc}{ignore_zeros}
304 If false, treat an empty block as the end of the archive. If true,
305 skip empty (and invalid) blocks and try to get as many members as
306 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000307 archives.
308\end{memberdesc}
309
310\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000311 To be set from \code{0} (no debug messages; the default) up to
312 \code{3} (all debug messages). The messages are written to
Georg Brandl208eec22005-07-12 07:28:20 +0000313 \code{sys.stderr}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000314\end{memberdesc}
315
Fred Drake3bbd1152004-01-13 23:41:32 +0000316\begin{memberdesc}{errorlevel}
317 If \code{0} (the default), all errors are ignored when using
318 \method{extract()}. Nevertheless, they appear as error messages
319 in the debug output, when debugging is enabled. If \code{1}, all
320 \emph{fatal} errors are raised as \exception{OSError} or
321 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
322 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000323\end{memberdesc}
324
325%-----------------
326% TarInfo Objects
327%-----------------
328
329\subsection{TarInfo Objects \label{tarinfo-objects}}
330
Fred Drake3bbd1152004-01-13 23:41:32 +0000331A \class{TarInfo} object represents one member in a
332\class{TarFile}. Aside from storing all required attributes of a file
333(like file type, size, time, permissions, owner etc.), it provides
334some useful methods to determine its type. It does \emph{not} contain
335the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000336
Fred Drake3bbd1152004-01-13 23:41:32 +0000337\class{TarInfo} objects are returned by \class{TarFile}'s methods
338\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000339
340\begin{classdesc}{TarInfo}{\optional{name}}
341 Create a \class{TarInfo} object.
342\end{classdesc}
343
344\begin{methoddesc}{frombuf}{}
345 Create and return a \class{TarInfo} object from a string buffer.
346\end{methoddesc}
347
Georg Brandl38c6a222006-05-10 16:26:03 +0000348\begin{methoddesc}{tobuf}{posix}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000349 Create a string buffer from a \class{TarInfo} object.
Georg Brandl38c6a222006-05-10 16:26:03 +0000350 See \class{TarFile}'s \member{posix} attribute for information
351 on the \var{posix} argument. It defaults to \constant{False}.
352
353 \versionadded[The \var{posix} parameter]{2.5}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000354\end{methoddesc}
355
356A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000357
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000358\begin{memberdesc}{name}
359 Name of the archive member.
360\end{memberdesc}
361
362\begin{memberdesc}{size}
363 Size in bytes.
364\end{memberdesc}
365
366\begin{memberdesc}{mtime}
367 Time of last modification.
368\end{memberdesc}
369
370\begin{memberdesc}{mode}
371 Permission bits.
372\end{memberdesc}
373
374\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000375 File type. \var{type} is usually one of these constants:
376 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
377 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
378 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
379 \constant{GNUTYPE_SPARSE}. To determine the type of a
380 \class{TarInfo} object more conveniently, use the \code{is_*()}
381 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000382\end{memberdesc}
383
384\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000385 Name of the target file name, which is only present in
386 \class{TarInfo} objects of type \constant{LNKTYPE} and
387 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000388\end{memberdesc}
389
Fred Drake3bbd1152004-01-13 23:41:32 +0000390\begin{memberdesc}{uid}
391 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000392\end{memberdesc}
393
Fred Drake3bbd1152004-01-13 23:41:32 +0000394\begin{memberdesc}{gid}
395 Group ID of the user who originally stored this member.
396\end{memberdesc}
397
398\begin{memberdesc}{uname}
399 User name.
400\end{memberdesc}
401
402\begin{memberdesc}{gname}
403 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000404\end{memberdesc}
405
406A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000407
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000408\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000409 Return \constant{True} if the \class{Tarinfo} object is a regular
410 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000411\end{methoddesc}
412
413\begin{methoddesc}{isreg}{}
414 Same as \method{isfile()}.
415\end{methoddesc}
416
417\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000418 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000419\end{methoddesc}
420
421\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000422 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000423\end{methoddesc}
424
425\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000426 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000427\end{methoddesc}
428
429\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000430 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000431\end{methoddesc}
432
433\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000434 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000435\end{methoddesc}
436
437\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000438 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000439\end{methoddesc}
440
441\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000442 Return \constant{True} if it is one of character device, block
443 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000444\end{methoddesc}
445
446%------------------------
447% Examples
448%------------------------
449
450\subsection{Examples \label{tar-examples}}
451
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000452How to extract an entire tar archive to the current working directory:
453\begin{verbatim}
454import tarfile
455tar = tarfile.open("sample.tar.gz")
456tar.extractall()
457tar.close()
458\end{verbatim}
459
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000460How to create an uncompressed tar archive from a list of filenames:
461\begin{verbatim}
462import tarfile
463tar = tarfile.open("sample.tar", "w")
464for name in ["foo", "bar", "quux"]:
465 tar.add(name)
466tar.close()
467\end{verbatim}
468
469How to read a gzip compressed tar archive and display some member information:
470\begin{verbatim}
471import tarfile
472tar = tarfile.open("sample.tar.gz", "r:gz")
473for tarinfo in tar:
474 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
475 if tarinfo.isreg():
476 print "a regular file."
477 elif tarinfo.isdir():
478 print "a directory."
479 else:
480 print "something else."
481tar.close()
482\end{verbatim}
483
484How to create a tar archive with faked information:
485\begin{verbatim}
486import tarfile
487tar = tarfile.open("sample.tar.gz", "w:gz")
488for name in namelist:
489 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
490 tarinfo.uid = 123
491 tarinfo.gid = 456
492 tarinfo.uname = "johndoe"
493 tarinfo.gname = "fake"
494 tar.addfile(tarinfo, file(name))
495tar.close()
496\end{verbatim}
497
498The \emph{only} way to extract an uncompressed tar stream from
499\code{sys.stdin}:
500\begin{verbatim}
501import sys
502import tarfile
503tar = tarfile.open(mode="r|", fileobj=sys.stdin)
504for tarinfo in tar:
505 tar.extract(tarinfo)
506tar.close()
507\end{verbatim}