1 \section{\module{tarfile
} --- Read and write tar archive files
}
3 \declaremodule{standard
}{tarfile
}
4 \modulesynopsis{Read and write tar-format archive files.
}
7 \moduleauthor{Lars Gust\"abel
}{lars@gustaebel.de
}
8 \sectionauthor{Lars Gust\"abel
}{lars@gustaebel.de
}
10 The
\module{tarfile
} module makes it possible to read and create tar archives.
11 Some facts and figures:
14 \item reads and writes
\module{gzip
} and
\module{bzip2
} compressed archives.
15 \item creates
\POSIX{} 1003.1-
1990 compliant or GNU tar compatible archives.
16 \item reads GNU tar extensions
\emph{longname
},
\emph{longlink
} and
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.
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
}).
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:
34 \begin{tableii
}{c|l
}{code
}{mode
}{action
}
35 \lineii{'r'
}{Open for reading with transparent compression (recommended).
}
36 \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.
}
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.
51 If
\var{fileobj
} is specified, it is used as an alternative to
52 a file object opened for
\var{name
}.
54 For special purposes, there is a second format for
\var{mode
}:
55 \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:
67 \begin{tableii
}{c|l
}{code
}{Mode
}{Action
}
68 \lineii{'r|'
}{Open a
\emph{stream
} of uncompressed tar blocks for reading.
}
69 \lineii{'r|gz'
}{Open a gzip compressed
\emph{stream
} for reading.
}
70 \lineii{'r|bz2'
}{Open a bzip2 compressed
\emph{stream
} for reading.
}
71 \lineii{'w|'
}{Open an uncompressed
\emph{stream
} for writing.
}
72 \lineii{'w|gz'
}{Open an gzip compressed
\emph{stream
} for writing.
}
73 \lineii{'w|bz2'
}{Open an bzip2 compressed
\emph{stream
} for writing.
}
77 \begin{classdesc*
}{TarFile
}
78 Class for reading and writing tar archives. Do not use this
79 class directly, better use
\function{open()
} instead.
80 See ``TarFile Objects'' (section~
\ref{tarfile-objects
}).
83 \begin{funcdesc
}{is_tarfile
}{name
}
84 Return
\constant{True
} if
\var{name
} is a tar archive file, that
85 the
\module{tarfile
} module can read.
88 \begin{classdesc
}{TarFileCompat
}{filename
\optional{, mode
\optional{,
90 Class for limited access to tar archives with a
91 \refmodule{zipfile
}-like interface. Please consult the
92 documentation of the
\refmodule{zipfile
} module for more details.
93 \var{compression
} must be one of the following constants:
94 \begin{datadesc
}{TAR_PLAIN
}
95 Constant for an uncompressed tar archive.
97 \begin{datadesc
}{TAR_GZIPPED
}
98 Constant for a
\refmodule{gzip
} compressed tar archive.
102 \begin{excdesc
}{TarError
}
103 Base class for all
\module{tarfile
} exceptions.
106 \begin{excdesc
}{ReadError
}
107 Is raised when a tar archive is opened, that either cannot be handled by
108 the
\module{tarfile
} module or is somehow invalid.
111 \begin{excdesc
}{CompressionError
}
112 Is raised when a compression method is not supported or when the data
113 cannot be decoded properly.
116 \begin{excdesc
}{StreamError
}
117 Is raised for the limitations that are typical for stream-like
118 \class{TarFile
} objects.
121 \begin{excdesc
}{ExtractError
}
122 Is raised for
\emph{non-fatal
} errors when using
\method{extract()
}, but
123 only if
\member{TarFile.errorlevel
}\code{ ==
2}.
127 \seemodule{zipfile
}{Documentation of the
\refmodule{zipfile
}
130 \seetitle[http://www.gnu.org/manual/tar/html_chapter/tar_8.html\#SEC118
]
131 {GNU tar manual, Standard Section
}{Documentation for tar archive files,
132 including GNU tar extensions.
}
139 \subsection{TarFile Objects
\label{tarfile-objects
}}
141 The
\class{TarFile
} object provides an interface to a tar archive. A tar
142 archive is a sequence of blocks. An archive member (a stored file) is made up
143 of a header block followed by data blocks. It is possible, to store a file in a
144 tar archive several times. Each archive member is represented by a
145 \class{TarInfo
} object, see
\citetitle{TarInfo Objects
} (section
146 \ref{tarinfo-objects
}) for details.
148 \begin{classdesc
}{TarFile
}{\optional{name
149 \optional{, mode
\optional{, fileobj
}}}}
150 Open an
\emph{(uncompressed)
} tar archive
\var{name
}.
151 \var{mode
} is either
\code{'r'
} to read from an existing archive,
152 \code{'a'
} to append data to an existing file or
\code{'w'
} to create a new
153 file overwriting an existing one.
\var{mode
} defaults to
\code{'r'
}.
155 If
\var{fileobj
} is given, it is used for reading or writing data.
156 If it can be determined,
\var{mode
} is overridden by
\var{fileobj
}'s mode.
158 \var{fileobj
} is not closed, when
\class{TarFile
} is closed.
162 \begin{methoddesc
}{open
}{...
}
163 Alternative constructor. The
\function{open()
} function on module level is
164 actually a shortcut to this classmethod. See section~
\ref{module-tarfile
}
168 \begin{methoddesc
}{getmember
}{name
}
169 Return a
\class{TarInfo
} object for member
\var{name
}. If
\var{name
} can
170 not be found in the archive,
\exception{KeyError
} is raised.
172 If a member occurs more than once in the archive, its last
173 occurence is assumed to be the most up-to-date version.
177 \begin{methoddesc
}{getmembers
}{}
178 Return the members of the archive as a list of
\class{TarInfo
} objects.
179 The list has the same order as the members in the archive.
182 \begin{methoddesc
}{getnames
}{}
183 Return the members as a list of their names. It has the same order as
184 the list returned by
\method{getmembers()
}.
187 \begin{methoddesc
}{list
}{verbose=True
}
188 Print a table of contents to
\code{sys.stdout
}. If
\var{verbose
} is
189 \constant{False
}, only the names of the members are printed. If it is
190 \constant{True
}, output similar to that of
\program{ls -l
} is produced.
193 \begin{methoddesc
}{next
}{}
194 Return the next member of the archive as a
\class{TarInfo
} object, when
195 \class{TarFile
} is opened for reading. Return
\code{None
} if there is no
199 \begin{methoddesc
}{extract
}{member
\optional{, path
}}
200 Extract a member from the archive to the current working directory,
201 using its full name. Its file information is extracted as accurately as
203 \var{member
} may be a filename or a
\class{TarInfo
} object.
204 You can specify a different directory using
\var{path
}.
207 \begin{methoddesc
}{extractfile
}{member
}
208 Extract a member from the archive as a file object.
209 \var{member
} may be a filename or a
\class{TarInfo
} object.
210 If
\var{member
} is a regular file, a file-like object is returned.
211 If
\var{member
} is a link, a file-like object is constructed from the
213 If
\var{member
} is none of the above,
\code{None
} is returned.
215 The file-like object is read-only and provides the following methods:
216 \method{read()
},
\method{readline()
},
\method{readlines()
},
217 \method{seek()
},
\method{tell()
}.
221 \begin{methoddesc
}{add
}{name
\optional{, arcname
\optional{, recursive
}}}
222 Add the file
\var{name
} to the archive.
\var{name
} may be any type
223 of file (directory, fifo, symbolic link, etc.).
224 If given,
\var{arcname
} specifies an alternative name for the file in the
225 archive. Directories are added recursively by default.
226 This can be avoided by setting
\var{recursive
} to
\constant{False
};
227 the default is
\constant{True
}.
230 \begin{methoddesc
}{addfile
}{tarinfo
\optional{, fileobj
}}
231 Add the
\class{TarInfo
} object
\var{tarinfo
} to the archive.
232 If
\var{fileobj
} is given,
\code{\var{tarinfo
}.size
} bytes are read
233 from it and added to the archive. You can create
\class{TarInfo
} objects
234 using
\method{gettarinfo()
}.
236 On Windows platforms,
\var{fileobj
} should always be opened with mode
237 \code{'rb'
} to avoid irritation about the file size.
241 \begin{methoddesc
}{gettarinfo
}{\optional{name
\optional{,
242 arcname
\optional{, fileobj
}}}}
243 Create a
\class{TarInfo
} object for either the file
\var{name
} or
244 the file object
\var{fileobj
} (using
\function{os.fstat()
} on its
245 file descriptor). You can modify some of the
\class{TarInfo
}'s
246 attributes before you add it using
\method{addfile()
}. If given,
247 \var{arcname
} specifies an alternative name for the file in the
251 \begin{methoddesc
}{close
}{}
252 Close the
\class{TarFile
}. In write mode, two finishing zero
253 blocks are appended to the archive.
256 \begin{memberdesc
}{posix
}
257 If true, create a
\POSIX{} 1003.1-
1990 compliant archive. GNU
258 extensions are not used, because they are not part of the
\POSIX{}
259 standard. This limits the length of filenames to at most
256 and
260 link names to
100 characters. A
\exception{ValueError
} is raised
261 if a pathname exceeds this limit. If false, create a GNU tar
262 compatible archive. It will not be
\POSIX{} compliant, but can
263 store pathnames of unlimited length.
266 \begin{memberdesc
}{dereference
}
267 If false, add symbolic and hard links to archive. If true, add the
268 content of the target files to the archive. This has no effect on
269 systems that do not support symbolic links.
272 \begin{memberdesc
}{ignore_zeros
}
273 If false, treat an empty block as the end of the archive. If true,
274 skip empty (and invalid) blocks and try to get as many members as
275 possible. This is only useful for concatenated or damaged
279 \begin{memberdesc
}{debug=
0}
280 To be set from
\code{0} (no debug messages; the default) up to
281 \code{3} (all debug messages). The messages are written to
285 \begin{memberdesc
}{errorlevel
}
286 If
\code{0} (the default), all errors are ignored when using
287 \method{extract()
}. Nevertheless, they appear as error messages
288 in the debug output, when debugging is enabled. If
\code{1}, all
289 \emph{fatal
} errors are raised as
\exception{OSError
} or
290 \exception{IOError
} exceptions. If
\code{2}, all
\emph{non-fatal
}
291 errors are raised as
\exception{TarError
} exceptions as well.
298 \subsection{TarInfo Objects
\label{tarinfo-objects
}}
300 A
\class{TarInfo
} object represents one member in a
301 \class{TarFile
}. Aside from storing all required attributes of a file
302 (like file type, size, time, permissions, owner etc.), it provides
303 some useful methods to determine its type. It does
\emph{not
} contain
304 the file's data itself.
306 \class{TarInfo
} objects are returned by
\class{TarFile
}'s methods
307 \method{getmember()
},
\method{getmembers()
} and
\method{gettarinfo()
}.
309 \begin{classdesc
}{TarInfo
}{\optional{name
}}
310 Create a
\class{TarInfo
} object.
313 \begin{methoddesc
}{frombuf
}{}
314 Create and return a
\class{TarInfo
} object from a string buffer.
317 \begin{methoddesc
}{tobuf
}{}
318 Create a string buffer from a
\class{TarInfo
} object.
321 A
\code{TarInfo
} object has the following public data attributes:
323 \begin{memberdesc
}{name
}
324 Name of the archive member.
327 \begin{memberdesc
}{size
}
331 \begin{memberdesc
}{mtime
}
332 Time of last modification.
335 \begin{memberdesc
}{mode
}
339 \begin{memberdesc
}{type
}
340 File type.
\var{type
} is usually one of these constants:
341 \constant{REGTYPE
},
\constant{AREGTYPE
},
\constant{LNKTYPE
},
342 \constant{SYMTYPE
},
\constant{DIRTYPE
},
\constant{FIFOTYPE
},
343 \constant{CONTTYPE
},
\constant{CHRTYPE
},
\constant{BLKTYPE
},
344 \constant{GNUTYPE_SPARSE
}. To determine the type of a
345 \class{TarInfo
} object more conveniently, use the
\code{is_*()
}
349 \begin{memberdesc
}{linkname
}
350 Name of the target file name, which is only present in
351 \class{TarInfo
} objects of type
\constant{LNKTYPE
} and
355 \begin{memberdesc
}{uid
}
356 User ID of the user who originally stored this member.
359 \begin{memberdesc
}{gid
}
360 Group ID of the user who originally stored this member.
363 \begin{memberdesc
}{uname
}
367 \begin{memberdesc
}{gname
}
371 A
\class{TarInfo
} object also provides some convenient query methods:
373 \begin{methoddesc
}{isfile
}{}
374 Return
\constant{True
} if the
\class{Tarinfo
} object is a regular
378 \begin{methoddesc
}{isreg
}{}
379 Same as
\method{isfile()
}.
382 \begin{methoddesc
}{isdir
}{}
383 Return
\constant{True
} if it is a directory.
386 \begin{methoddesc
}{issym
}{}
387 Return
\constant{True
} if it is a symbolic link.
390 \begin{methoddesc
}{islnk
}{}
391 Return
\constant{True
} if it is a hard link.
394 \begin{methoddesc
}{ischr
}{}
395 Return
\constant{True
} if it is a character device.
398 \begin{methoddesc
}{isblk
}{}
399 Return
\constant{True
} if it is a block device.
402 \begin{methoddesc
}{isfifo
}{}
403 Return
\constant{True
} if it is a FIFO.
406 \begin{methoddesc
}{isdev
}{}
407 Return
\constant{True
} if it is one of character device, block
411 %------------------------
413 %------------------------
415 \subsection{Examples
\label{tar-examples
}}
417 How to create an uncompressed tar archive from a list of filenames:
420 tar = tarfile.open("sample.tar", "w")
421 for name in
["foo", "bar", "quux"
]:
426 How to read a gzip compressed tar archive and display some member information:
429 tar = tarfile.open("sample.tar.gz", "r:gz")
431 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
433 print "a regular file."
434 elif tarinfo.isdir():
437 print "something else."
441 How to create a tar archive with faked information:
444 tar = tarfile.open("sample.tar.gz", "w:gz")
445 for name in namelist:
446 tarinfo = tar.gettarinfo(name, "fakeproj-
1.0/" + name)
449 tarinfo.uname = "johndoe"
450 tarinfo.gname = "fake"
451 tar.addfile(tarinfo, file(name))
455 The
\emph{only
} way to extract an uncompressed tar stream from
460 tar = tarfile.open(mode="r|", fileobj=sys.stdin)