blob: 5f277dafef4692378c9c66353404eb296d8d2082 [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
51 If \var{fileobj} is specified, it is used as an alternative to
52 a file object opened for \var{name}.
53
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
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000127\begin{excdesc}{HeaderError}
128 Is raised by \method{frombuf()} if the buffer it gets is invalid.
129 \versionadded{2.6}
130\end{excdesc}
131
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000132\begin{seealso}
Fred Drake3bbd1152004-01-13 23:41:32 +0000133 \seemodule{zipfile}{Documentation of the \refmodule{zipfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000134 standard module.}
135
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136 \seetitle[http://www.gnu.org/software/tar/manual/html_node/tar_134.html\#SEC134]
Georg Brandl9a19e5c2005-08-27 17:10:35 +0000137 {GNU tar manual, Basic Tar Format}{Documentation for tar archive files,
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000138 including GNU tar extensions.}
139\end{seealso}
140
141%-----------------
142% TarFile Objects
143%-----------------
144
145\subsection{TarFile Objects \label{tarfile-objects}}
146
147The \class{TarFile} object provides an interface to a tar archive. A tar
148archive is a sequence of blocks. An archive member (a stored file) is made up
149of a header block followed by data blocks. It is possible, to store a file in a
150tar archive several times. Each archive member is represented by a
151\class{TarInfo} object, see \citetitle{TarInfo Objects} (section
152\ref{tarinfo-objects}) for details.
153
154\begin{classdesc}{TarFile}{\optional{name
155 \optional{, mode\optional{, fileobj}}}}
156 Open an \emph{(uncompressed)} tar archive \var{name}.
157 \var{mode} is either \code{'r'} to read from an existing archive,
158 \code{'a'} to append data to an existing file or \code{'w'} to create a new
159 file overwriting an existing one. \var{mode} defaults to \code{'r'}.
160
161 If \var{fileobj} is given, it is used for reading or writing data.
162 If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode.
163 \begin{notice}
164 \var{fileobj} is not closed, when \class{TarFile} is closed.
165 \end{notice}
166\end{classdesc}
167
168\begin{methoddesc}{open}{...}
169 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000170 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000171 for details.
172\end{methoddesc}
173
174\begin{methoddesc}{getmember}{name}
175 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
176 not be found in the archive, \exception{KeyError} is raised.
177 \begin{notice}
178 If a member occurs more than once in the archive, its last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000179 occurrence is assumed to be the most up-to-date version.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000180 \end{notice}
181\end{methoddesc}
182
183\begin{methoddesc}{getmembers}{}
184 Return the members of the archive as a list of \class{TarInfo} objects.
185 The list has the same order as the members in the archive.
186\end{methoddesc}
187
188\begin{methoddesc}{getnames}{}
189 Return the members as a list of their names. It has the same order as
190 the list returned by \method{getmembers()}.
191\end{methoddesc}
192
193\begin{methoddesc}{list}{verbose=True}
194 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000195 \constant{False}, only the names of the members are printed. If it is
196 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000197\end{methoddesc}
198
199\begin{methoddesc}{next}{}
200 Return the next member of the archive as a \class{TarInfo} object, when
201 \class{TarFile} is opened for reading. Return \code{None} if there is no
202 more available.
203\end{methoddesc}
204
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000205\begin{methoddesc}{extractall}{\optional{path\optional{, members}}}
206 Extract all members from the archive to the current working directory
207 or directory \var{path}. If optional \var{members} is given, it must be
208 a subset of the list returned by \method{getmembers()}.
209 Directory informations like owner, modification time and permissions are
210 set after all members have been extracted. This is done to work around two
211 problems: A directory's modification time is reset each time a file is
212 created in it. And, if a directory's permissions do not allow writing,
213 extracting files to it will fail.
214 \versionadded{2.5}
215\end{methoddesc}
216
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000217\begin{methoddesc}{extract}{member\optional{, path}}
218 Extract a member from the archive to the current working directory,
219 using its full name. Its file information is extracted as accurately as
220 possible.
221 \var{member} may be a filename or a \class{TarInfo} object.
222 You can specify a different directory using \var{path}.
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000223 \begin{notice}
224 Because the \method{extract()} method allows random access to a tar
225 archive there are some issues you must take care of yourself. See the
226 description for \method{extractall()} above.
227 \end{notice}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000228\end{methoddesc}
229
230\begin{methoddesc}{extractfile}{member}
231 Extract a member from the archive as a file object.
232 \var{member} may be a filename or a \class{TarInfo} object.
233 If \var{member} is a regular file, a file-like object is returned.
234 If \var{member} is a link, a file-like object is constructed from the
235 link's target.
236 If \var{member} is none of the above, \code{None} is returned.
237 \begin{notice}
238 The file-like object is read-only and provides the following methods:
239 \method{read()}, \method{readline()}, \method{readlines()},
240 \method{seek()}, \method{tell()}.
241 \end{notice}
242\end{methoddesc}
243
Fred Drake3bbd1152004-01-13 23:41:32 +0000244\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000245 Add the file \var{name} to the archive. \var{name} may be any type
246 of file (directory, fifo, symbolic link, etc.).
247 If given, \var{arcname} specifies an alternative name for the file in the
248 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000249 This can be avoided by setting \var{recursive} to \constant{False};
250 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000251\end{methoddesc}
252
253\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
254 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000255 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000256 from it and added to the archive. You can create \class{TarInfo} objects
257 using \method{gettarinfo()}.
258 \begin{notice}
259 On Windows platforms, \var{fileobj} should always be opened with mode
260 \code{'rb'} to avoid irritation about the file size.
261 \end{notice}
262\end{methoddesc}
263
Fred Drake3bbd1152004-01-13 23:41:32 +0000264\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
265 arcname\optional{, fileobj}}}}
266 Create a \class{TarInfo} object for either the file \var{name} or
267 the file object \var{fileobj} (using \function{os.fstat()} on its
268 file descriptor). You can modify some of the \class{TarInfo}'s
269 attributes before you add it using \method{addfile()}. If given,
270 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000271 archive.
272\end{methoddesc}
273
274\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000275 Close the \class{TarFile}. In write mode, two finishing zero
276 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000277\end{methoddesc}
278
Fred Drake3bbd1152004-01-13 23:41:32 +0000279\begin{memberdesc}{posix}
280 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
281 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000282 standard. This limits the length of filenames to at most 256,
283 link names to 100 characters and the maximum file size to 8
284 gigabytes. A \exception{ValueError} is raised if a file exceeds
285 this limit. If false, create a GNU tar compatible archive. It
286 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000287 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000288 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000289\end{memberdesc}
290
Fred Drake3bbd1152004-01-13 23:41:32 +0000291\begin{memberdesc}{dereference}
292 If false, add symbolic and hard links to archive. If true, add the
293 content of the target files to the archive. This has no effect on
294 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000295\end{memberdesc}
296
Fred Drake3bbd1152004-01-13 23:41:32 +0000297\begin{memberdesc}{ignore_zeros}
298 If false, treat an empty block as the end of the archive. If true,
299 skip empty (and invalid) blocks and try to get as many members as
300 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000301 archives.
302\end{memberdesc}
303
304\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000305 To be set from \code{0} (no debug messages; the default) up to
306 \code{3} (all debug messages). The messages are written to
Georg Brandl208eec22005-07-12 07:28:20 +0000307 \code{sys.stderr}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000308\end{memberdesc}
309
Fred Drake3bbd1152004-01-13 23:41:32 +0000310\begin{memberdesc}{errorlevel}
311 If \code{0} (the default), all errors are ignored when using
312 \method{extract()}. Nevertheless, they appear as error messages
313 in the debug output, when debugging is enabled. If \code{1}, all
314 \emph{fatal} errors are raised as \exception{OSError} or
315 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
316 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000317\end{memberdesc}
318
319%-----------------
320% TarInfo Objects
321%-----------------
322
323\subsection{TarInfo Objects \label{tarinfo-objects}}
324
Fred Drake3bbd1152004-01-13 23:41:32 +0000325A \class{TarInfo} object represents one member in a
326\class{TarFile}. Aside from storing all required attributes of a file
327(like file type, size, time, permissions, owner etc.), it provides
328some useful methods to determine its type. It does \emph{not} contain
329the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000330
Fred Drake3bbd1152004-01-13 23:41:32 +0000331\class{TarInfo} objects are returned by \class{TarFile}'s methods
332\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000333
334\begin{classdesc}{TarInfo}{\optional{name}}
335 Create a \class{TarInfo} object.
336\end{classdesc}
337
338\begin{methoddesc}{frombuf}{}
339 Create and return a \class{TarInfo} object from a string buffer.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000340 \versionadded[Raises \exception{HeaderError} if the buffer is
341 invalid.]{2.6}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000342\end{methoddesc}
343
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344\begin{methoddesc}{tobuf}{posix}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000345 Create a string buffer from a \class{TarInfo} object.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000346 See \class{TarFile}'s \member{posix} attribute for information
347 on the \var{posix} argument. It defaults to \constant{False}.
348
349 \versionadded[The \var{posix} parameter]{2.5}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000350\end{methoddesc}
351
352A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000353
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000354\begin{memberdesc}{name}
355 Name of the archive member.
356\end{memberdesc}
357
358\begin{memberdesc}{size}
359 Size in bytes.
360\end{memberdesc}
361
362\begin{memberdesc}{mtime}
363 Time of last modification.
364\end{memberdesc}
365
366\begin{memberdesc}{mode}
367 Permission bits.
368\end{memberdesc}
369
370\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000371 File type. \var{type} is usually one of these constants:
372 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
373 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
374 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
375 \constant{GNUTYPE_SPARSE}. To determine the type of a
376 \class{TarInfo} object more conveniently, use the \code{is_*()}
377 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000378\end{memberdesc}
379
380\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000381 Name of the target file name, which is only present in
382 \class{TarInfo} objects of type \constant{LNKTYPE} and
383 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000384\end{memberdesc}
385
Fred Drake3bbd1152004-01-13 23:41:32 +0000386\begin{memberdesc}{uid}
387 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000388\end{memberdesc}
389
Fred Drake3bbd1152004-01-13 23:41:32 +0000390\begin{memberdesc}{gid}
391 Group ID of the user who originally stored this member.
392\end{memberdesc}
393
394\begin{memberdesc}{uname}
395 User name.
396\end{memberdesc}
397
398\begin{memberdesc}{gname}
399 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000400\end{memberdesc}
401
402A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000403
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000404\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000405 Return \constant{True} if the \class{Tarinfo} object is a regular
406 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000407\end{methoddesc}
408
409\begin{methoddesc}{isreg}{}
410 Same as \method{isfile()}.
411\end{methoddesc}
412
413\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000414 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000415\end{methoddesc}
416
417\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000418 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000419\end{methoddesc}
420
421\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000422 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000423\end{methoddesc}
424
425\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000426 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000427\end{methoddesc}
428
429\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000430 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000431\end{methoddesc}
432
433\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000434 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000435\end{methoddesc}
436
437\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000438 Return \constant{True} if it is one of character device, block
439 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000440\end{methoddesc}
441
442%------------------------
443% Examples
444%------------------------
445
446\subsection{Examples \label{tar-examples}}
447
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000448How to extract an entire tar archive to the current working directory:
449\begin{verbatim}
450import tarfile
451tar = tarfile.open("sample.tar.gz")
452tar.extractall()
453tar.close()
454\end{verbatim}
455
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000456How to create an uncompressed tar archive from a list of filenames:
457\begin{verbatim}
458import tarfile
459tar = tarfile.open("sample.tar", "w")
460for name in ["foo", "bar", "quux"]:
461 tar.add(name)
462tar.close()
463\end{verbatim}
464
465How to read a gzip compressed tar archive and display some member information:
466\begin{verbatim}
467import tarfile
468tar = tarfile.open("sample.tar.gz", "r:gz")
469for tarinfo in tar:
470 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
471 if tarinfo.isreg():
472 print "a regular file."
473 elif tarinfo.isdir():
474 print "a directory."
475 else:
476 print "something else."
477tar.close()
478\end{verbatim}
479
480How to create a tar archive with faked information:
481\begin{verbatim}
482import tarfile
483tar = tarfile.open("sample.tar.gz", "w:gz")
484for name in namelist:
485 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
486 tarinfo.uid = 123
487 tarinfo.gid = 456
488 tarinfo.uname = "johndoe"
489 tarinfo.gname = "fake"
490 tar.addfile(tarinfo, file(name))
491tar.close()
492\end{verbatim}
493
494The \emph{only} way to extract an uncompressed tar stream from
495\code{sys.stdin}:
496\begin{verbatim}
497import sys
498import tarfile
499tar = tarfile.open(mode="r|", fileobj=sys.stdin)
500for tarinfo in tar:
501 tar.extract(tarinfo)
502tar.close()
503\end{verbatim}