handy_archives
¶
Some handy archive helpers for Python.
Classes:
|
Subclass of |
|
Subclass of |
Functions:
|
Return |
|
Unpack an archive. |
-
class
TarFile
(name=None, mode='r', fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, errors='surrogateescape', pax_headers=None, debug=None, errorlevel=None, copybufsize=None)[source]¶ Subclass of
tarfile.TarFile
with additional methods.Methods:
extract
(member[, path, set_attrs, …])Wrapper around
tarfile.TarFile.extract()
with compatibility shim for PEP 706 on unpatched Pythons.extractall
([path, members, numeric_owner, …])Wrapper around
tarfile.TarFile.extractall()
with compatibility shim for PEP 706 on unpatched Pythons.extractfile
(member)Extract a member from the archive as a file object.
read_bytes
(member)Returns the content of the given file as bytes.
read_text
(member, *[, normalize_nl])Returns the content of the given file as a string.
write_file
(filename[, arcname, mtime])Add the file
filename
to the archive under the namearcname
.-
extract
(member, path='', set_attrs=True, *, numeric_owner=False, filter=None)[source]¶ Wrapper around
tarfile.TarFile.extract()
with compatibility shim for PEP 706 on unpatched Pythons.
-
extractall
(path='.', members=None, *, numeric_owner=False, filter=None)[source]¶ Wrapper around
tarfile.TarFile.extractall()
with compatibility shim for PEP 706 on unpatched Pythons.
-
extractfile
(member)[source]¶ Extract a member from the archive as a file object.
- Parameters
member (
Union
[str
,TarInfo
]) – A filename or atarfile.TarInfo
object.
If
member
is a regular file or a link, anio.BufferedReader
object is returned. OtherwiseFileNotFoundError
is raised.
-
read_bytes
(member)[source]¶ Returns the content of the given file as bytes.
- Parameters
- Raises
FileNotFoundError – If the file is not found in the archive.
- Return type
-
read_text
(member, *, normalize_nl=False)[source]¶ Returns the content of the given file as a string.
- Parameters
- Raises
FileNotFoundError – If the file is not found in the archive.
- Return type
New in version 0.2.0: Added the
normalize_nl
option.
-
-
class
ZipFile
(file, mode='r', compression=0, allowZip64=True, compresslevel=None, *, strict_timestamps=True)[source]¶ Subclass of
zipfile.ZipFile
with additional methods.Methods:
extractfile
(member[, pwd])Extract a member from the archive as a file object.
read_bytes
(member[, pwd])Returns the content of the given file as bytes.
read_text
(member[, pwd, normalize_nl])Returns the content of the given file as a string.
write_file
(filename[, arcname, mtime])Put the bytes from
filename
into the archive under the namearcname
.-
read_text
(member, pwd=None, *, normalize_nl=False)[source]¶ Returns the content of the given file as a string.
- Parameters
- Raises
FileNotFoundError – If the file is not found in the archive.
- Return type
New in version 0.2.0: Added the
normalize_nl
option.
-
-
is_tarfile
(name)[source]¶ Return
True
ifname
points to a tar archive thattarfile
can handle, else returnFalse
.
-
unpack_archive
(filename, extract_dir=None, format=None)[source]¶ Unpack an archive.
- Parameters
filename (
Union
[str
,Path
,PathLike
]) – The name of the archive.extract_dir (
Union
[str
,Path
,PathLike
,None
]) – The name of the target directory, where the archive is unpacked. If not provided, the current working directory is used. DefaultNone
.format (
Optional
[str
]) – The archive format: one of'zip'
,'tar'
,'gztar'
,'bztar'
, or'xztar'
, or any other format registered throughshutil.register_unpack_format()
. If not provided,unpack_archive
will use the filename extension and see if an unpacker was registered for that extension. DefaultNone
.
If no unpacker is found, a
ValueError
is raised.