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.TarFilewith 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
filenameto 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.TarInfoobject.
If
memberis a regular file or a link, anio.BufferedReaderobject is returned. OtherwiseFileNotFoundErroris 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_nloption.
-
-
class
ZipFile(file, mode='r', compression=0, allowZip64=True, compresslevel=None, *, strict_timestamps=True)[source]¶ Subclass of
zipfile.ZipFilewith 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
filenameinto 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_nloption.
-
-
is_tarfile(name)[source]¶ Return
Trueifnamepoints to a tar archive thattarfilecan 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_archivewill use the filename extension and see if an unpacker was registered for that extension. DefaultNone.
If no unpacker is found, a
ValueErroris raised.