blob: 96f835f539569ea6f09c38c4f4c600e4428de8ba [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
Lars Gustäbela69aa322007-02-12 09:25:53 +000052 If \var{fileobj} is specified, it is used as an alternative to a file
53 object opened for \var{name}. It is supposed to be at position 0.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000054
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.
Lars Gustäbela69aa322007-02-12 09:25:53 +0000164 \var{fileobj} will be used from position 0.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000165 \begin{notice}
166 \var{fileobj} is not closed, when \class{TarFile} is closed.
167 \end{notice}
168\end{classdesc}
169
170\begin{methoddesc}{open}{...}
171 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000172 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000173 for details.
174\end{methoddesc}
175
176\begin{methoddesc}{getmember}{name}
177 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
178 not be found in the archive, \exception{KeyError} is raised.
179 \begin{notice}
180 If a member occurs more than once in the archive, its last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000181 occurrence is assumed to be the most up-to-date version.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000182 \end{notice}
183\end{methoddesc}
184
185\begin{methoddesc}{getmembers}{}
186 Return the members of the archive as a list of \class{TarInfo} objects.
187 The list has the same order as the members in the archive.
188\end{methoddesc}
189
190\begin{methoddesc}{getnames}{}
191 Return the members as a list of their names. It has the same order as
192 the list returned by \method{getmembers()}.
193\end{methoddesc}
194
195\begin{methoddesc}{list}{verbose=True}
196 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000197 \constant{False}, only the names of the members are printed. If it is
198 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000199\end{methoddesc}
200
201\begin{methoddesc}{next}{}
202 Return the next member of the archive as a \class{TarInfo} object, when
203 \class{TarFile} is opened for reading. Return \code{None} if there is no
204 more available.
205\end{methoddesc}
206
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000207\begin{methoddesc}{extractall}{\optional{path\optional{, members}}}
208 Extract all members from the archive to the current working directory
209 or directory \var{path}. If optional \var{members} is given, it must be
210 a subset of the list returned by \method{getmembers()}.
211 Directory informations like owner, modification time and permissions are
212 set after all members have been extracted. This is done to work around two
213 problems: A directory's modification time is reset each time a file is
214 created in it. And, if a directory's permissions do not allow writing,
215 extracting files to it will fail.
216 \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}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000230\end{methoddesc}
231
232\begin{methoddesc}{extractfile}{member}
233 Extract a member from the archive as a file object.
234 \var{member} may be a filename or a \class{TarInfo} object.
235 If \var{member} is a regular file, a file-like object is returned.
236 If \var{member} is a link, a file-like object is constructed from the
237 link's target.
238 If \var{member} is none of the above, \code{None} is returned.
239 \begin{notice}
240 The file-like object is read-only and provides the following methods:
241 \method{read()}, \method{readline()}, \method{readlines()},
242 \method{seek()}, \method{tell()}.
243 \end{notice}
244\end{methoddesc}
245
Fred Drake3bbd1152004-01-13 23:41:32 +0000246\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000247 Add the file \var{name} to the archive. \var{name} may be any type
248 of file (directory, fifo, symbolic link, etc.).
249 If given, \var{arcname} specifies an alternative name for the file in the
250 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000251 This can be avoided by setting \var{recursive} to \constant{False};
252 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000253\end{methoddesc}
254
255\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
256 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000257 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000258 from it and added to the archive. You can create \class{TarInfo} objects
259 using \method{gettarinfo()}.
260 \begin{notice}
261 On Windows platforms, \var{fileobj} should always be opened with mode
262 \code{'rb'} to avoid irritation about the file size.
263 \end{notice}
264\end{methoddesc}
265
Fred Drake3bbd1152004-01-13 23:41:32 +0000266\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
267 arcname\optional{, fileobj}}}}
268 Create a \class{TarInfo} object for either the file \var{name} or
269 the file object \var{fileobj} (using \function{os.fstat()} on its
270 file descriptor). You can modify some of the \class{TarInfo}'s
271 attributes before you add it using \method{addfile()}. If given,
272 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000273 archive.
274\end{methoddesc}
275
276\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000277 Close the \class{TarFile}. In write mode, two finishing zero
278 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000279\end{methoddesc}
280
Fred Drake3bbd1152004-01-13 23:41:32 +0000281\begin{memberdesc}{posix}
282 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
283 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000284 standard. This limits the length of filenames to at most 256,
285 link names to 100 characters and the maximum file size to 8
286 gigabytes. A \exception{ValueError} is raised if a file exceeds
287 this limit. If false, create a GNU tar compatible archive. It
288 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000289 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000290 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000291\end{memberdesc}
292
Fred Drake3bbd1152004-01-13 23:41:32 +0000293\begin{memberdesc}{dereference}
294 If false, add symbolic and hard links to archive. If true, add the
295 content of the target files to the archive. This has no effect on
296 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000297\end{memberdesc}
298
Fred Drake3bbd1152004-01-13 23:41:32 +0000299\begin{memberdesc}{ignore_zeros}
300 If false, treat an empty block as the end of the archive. If true,
301 skip empty (and invalid) blocks and try to get as many members as
302 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000303 archives.
304\end{memberdesc}
305
306\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000307 To be set from \code{0} (no debug messages; the default) up to
308 \code{3} (all debug messages). The messages are written to
Georg Brandl208eec22005-07-12 07:28:20 +0000309 \code{sys.stderr}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000310\end{memberdesc}
311
Fred Drake3bbd1152004-01-13 23:41:32 +0000312\begin{memberdesc}{errorlevel}
313 If \code{0} (the default), all errors are ignored when using
314 \method{extract()}. Nevertheless, they appear as error messages
315 in the debug output, when debugging is enabled. If \code{1}, all
316 \emph{fatal} errors are raised as \exception{OSError} or
317 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
318 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000319\end{memberdesc}
320
321%-----------------
322% TarInfo Objects
323%-----------------
324
325\subsection{TarInfo Objects \label{tarinfo-objects}}
326
Fred Drake3bbd1152004-01-13 23:41:32 +0000327A \class{TarInfo} object represents one member in a
328\class{TarFile}. Aside from storing all required attributes of a file
329(like file type, size, time, permissions, owner etc.), it provides
330some useful methods to determine its type. It does \emph{not} contain
331the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000332
Fred Drake3bbd1152004-01-13 23:41:32 +0000333\class{TarInfo} objects are returned by \class{TarFile}'s methods
334\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000335
336\begin{classdesc}{TarInfo}{\optional{name}}
337 Create a \class{TarInfo} object.
338\end{classdesc}
339
340\begin{methoddesc}{frombuf}{}
341 Create and return a \class{TarInfo} object from a string buffer.
Georg Brandlebbeed72006-12-19 22:06:46 +0000342 \versionadded[Raises \exception{HeaderError} if the buffer is
343 invalid.]{2.6}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000344\end{methoddesc}
345
Georg Brandl38c6a222006-05-10 16:26:03 +0000346\begin{methoddesc}{tobuf}{posix}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000347 Create a string buffer from a \class{TarInfo} object.
Georg Brandl38c6a222006-05-10 16:26:03 +0000348 See \class{TarFile}'s \member{posix} attribute for information
349 on the \var{posix} argument. It defaults to \constant{False}.
350
351 \versionadded[The \var{posix} parameter]{2.5}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000352\end{methoddesc}
353
354A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000355
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000356\begin{memberdesc}{name}
357 Name of the archive member.
358\end{memberdesc}
359
360\begin{memberdesc}{size}
361 Size in bytes.
362\end{memberdesc}
363
364\begin{memberdesc}{mtime}
365 Time of last modification.
366\end{memberdesc}
367
368\begin{memberdesc}{mode}
369 Permission bits.
370\end{memberdesc}
371
372\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000373 File type. \var{type} is usually one of these constants:
374 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
375 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
376 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
377 \constant{GNUTYPE_SPARSE}. To determine the type of a
378 \class{TarInfo} object more conveniently, use the \code{is_*()}
379 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000380\end{memberdesc}
381
382\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000383 Name of the target file name, which is only present in
384 \class{TarInfo} objects of type \constant{LNKTYPE} and
385 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000386\end{memberdesc}
387
Fred Drake3bbd1152004-01-13 23:41:32 +0000388\begin{memberdesc}{uid}
389 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000390\end{memberdesc}
391
Fred Drake3bbd1152004-01-13 23:41:32 +0000392\begin{memberdesc}{gid}
393 Group ID of the user who originally stored this member.
394\end{memberdesc}
395
396\begin{memberdesc}{uname}
397 User name.
398\end{memberdesc}
399
400\begin{memberdesc}{gname}
401 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000402\end{memberdesc}
403
404A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000405
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000406\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000407 Return \constant{True} if the \class{Tarinfo} object is a regular
408 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000409\end{methoddesc}
410
411\begin{methoddesc}{isreg}{}
412 Same as \method{isfile()}.
413\end{methoddesc}
414
415\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000416 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000417\end{methoddesc}
418
419\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000420 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000421\end{methoddesc}
422
423\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000424 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000425\end{methoddesc}
426
427\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000428 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000429\end{methoddesc}
430
431\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000432 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000433\end{methoddesc}
434
435\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000436 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000437\end{methoddesc}
438
439\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000440 Return \constant{True} if it is one of character device, block
441 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000442\end{methoddesc}
443
444%------------------------
445% Examples
446%------------------------
447
448\subsection{Examples \label{tar-examples}}
449
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000450How to extract an entire tar archive to the current working directory:
451\begin{verbatim}
452import tarfile
453tar = tarfile.open("sample.tar.gz")
454tar.extractall()
455tar.close()
456\end{verbatim}
457
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000458How to create an uncompressed tar archive from a list of filenames:
459\begin{verbatim}
460import tarfile
461tar = tarfile.open("sample.tar", "w")
462for name in ["foo", "bar", "quux"]:
463 tar.add(name)
464tar.close()
465\end{verbatim}
466
467How to read a gzip compressed tar archive and display some member information:
468\begin{verbatim}
469import tarfile
470tar = tarfile.open("sample.tar.gz", "r:gz")
471for tarinfo in tar:
472 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
473 if tarinfo.isreg():
474 print "a regular file."
475 elif tarinfo.isdir():
476 print "a directory."
477 else:
478 print "something else."
479tar.close()
480\end{verbatim}
481
482How to create a tar archive with faked information:
483\begin{verbatim}
484import tarfile
485tar = tarfile.open("sample.tar.gz", "w:gz")
486for name in namelist:
487 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
488 tarinfo.uid = 123
489 tarinfo.gid = 456
490 tarinfo.uname = "johndoe"
491 tarinfo.gname = "fake"
492 tar.addfile(tarinfo, file(name))
493tar.close()
494\end{verbatim}
495
496The \emph{only} way to extract an uncompressed tar stream from
497\code{sys.stdin}:
498\begin{verbatim}
499import sys
500import tarfile
501tar = tarfile.open(mode="r|", fileobj=sys.stdin)
502for tarinfo in tar:
503 tar.extract(tarinfo)
504tar.close()
505\end{verbatim}