Extractor

class tfsnippet.utils.Extractor(archive_file)

Bases: object

The base class for all archive extractors.

from tfsnippet.utils import Extractor, maybe_close

with Extractor.open('a.zip') as archive_file:
    for name, f in archive_file:
        with maybe_close(f):  # This file object may not be closeable,
                              # thus we surround it by ``maybe_close()``
            print('the content of {} is:'.format(name))
            print(f.read())

Methods Summary

close() Close the extractor.
iter_extract() Extract files from the archive with an iterator.
open(file_path) Create an Extractor instance for given archive file.

Methods Documentation

close()

Close the extractor.

iter_extract()

Extract files from the archive with an iterator.

You may simply iterate over a Extractor object, which is same as calling to this method.

Yields:

(str, file-like)

Tuples of (name, file-like object), the

filename and corresponding file-like object for each file in the archive. The returned file-like object may or may not be closeable. You may surround it by maybe_close().

static open(file_path)

Create an Extractor instance for given archive file.

Parameters:file_path (str) – The path of the archive file.
Returns:The specified extractor instance.
Return type:Extractor
Raises:IOError – If the file_path is not a supported archive.