当前位置: 首页>>代码示例>>Python>>正文


Python tarfile.TarFile方法代码示例

本文整理汇总了Python中tarfile.TarFile方法的典型用法代码示例。如果您正苦于以下问题:Python tarfile.TarFile方法的具体用法?Python tarfile.TarFile怎么用?Python tarfile.TarFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tarfile的用法示例。


在下文中一共展示了tarfile.TarFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: open_archive

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def open_archive(self, file_path, compression_type=None, mode='r'):
        """
        Test if archive can be assumed by filename

        @param file_path: Path to file
        @type file_path: str | unicode

        @return: True if stream
        @rtype: tarfile.TarFile
        """
        assert mode in self._modes, "Unsupported mode".format(mode)
        if compression_type is None:
            compression_type = self.get_compression_type(file_path)
        assert compression_type in self._modes[mode], "Unsupported compression '{}' for archive files.".format(
            compression_type)
        assert self.is_archive(file_path)

        if compression_type is None:
            compression_type = 'tar'

        mode = self._modes[mode][compression_type]
        return self._open[compression_type](file_path, mode=mode) 
开发者ID:CAMI-challenge,项目名称:CAMISIM,代码行数:24,代码来源:archive.py

示例2: test_extract_tarred_chute

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_extract_tarred_chute():
    # Normal case: tar file with a paradrop.yaml file.
    with open("/tmp/paradrop.yaml", "w") as output:
        output.write("name: test")

    tar = tarfile.TarFile(name="/tmp/test_chute.tar", mode="w")
    tar.add("/tmp/paradrop.yaml", arcname="paradrop.yaml")
    tar.close()

    with open("/tmp/test_chute.tar", "r") as source:
        workdir, paradrop_yaml = chute_api.extract_tarred_chute(source)
        assert os.path.isdir(workdir)
        assert paradrop_yaml['name'] == "test"

    # Bad case: empty tar file, no paradrop.yaml.
    tar = tarfile.TarFile(name="/tmp/test_chute.tar", mode="w")
    tar.close()

    with open("/tmp/test_chute.tar", "r") as source:
        assert_raises(Exception, chute_api.extract_tarred_chute, source) 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:22,代码来源:test_chute_api.py

示例3: test_init_close_fobj

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        with open(empty, "wb") as fobj:
            fobj.write("")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            support.unlink(empty) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:20,代码来源:test_tarfile.py

示例4: extractall

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def extractall(file_path, destination, ext):
    """Extracts an archive file.

    This function extracts an archive file to a destination.

    Args:
        file_path (string): The path of a file to be extracted.
        destination (string): A directory path. The archive file
            will be extracted under this directory.
        ext (string): An extension suffix of the archive file.
            This function supports :obj:`'.zip'`, :obj:`'.tar'`,
            :obj:`'.gz'` and :obj:`'.tgz'`.

    """

    if ext == '.zip':
        with zipfile.ZipFile(file_path, 'r') as z:
            z.extractall(destination)
    elif ext == '.tar':
        with tarfile.TarFile(file_path, 'r') as t:
            t.extractall(destination)
    elif ext == '.gz' or ext == '.tgz':
        with tarfile.open(file_path, 'r:gz') as t:
            t.extractall(destination) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:26,代码来源:download.py

示例5: _tar_add_string_file

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def _tar_add_string_file(self, tarobj, fpath, content):
        """
        Given a tarfile object, add a file to it at ``fpath``, with content
        ``content``.

        Largely based on: http://stackoverflow.com/a/40392022

        :param tarobj: the tarfile to add to
        :type tarobj: tarfile.TarFile
        :param fpath: path to put the file at in the archive
        :type fpath: str
        :param content: file content
        :type content: str
        """
        logger.debug('Adding %d-length string to tarfile at %s',
                     len(content), fpath)
        data = content.encode('utf-8')
        f = BytesIO(data)
        info = tarfile.TarInfo(name=fpath)
        info.size = len(data)
        tarobj.addfile(tarinfo=info, fileobj=f) 
开发者ID:jantman,项目名称:biweeklybudget,代码行数:23,代码来源:docker_build.py

示例6: test_ignore_zeros

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_ignore_zeros(self):
        # Test TarFile's ignore_zeros option.
        if self.mode.endswith(":gz"):
            _open = gzip.GzipFile
        elif self.mode.endswith(":bz2"):
            _open = bz2.BZ2File
        else:
            _open = open

        for char in ('\0', 'a'):
            # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
            # are ignored correctly.
            fobj = _open(tmpname, "wb")
            fobj.write(char * 1024)
            fobj.write(tarfile.TarInfo("foo").tobuf())
            fobj.close()

            tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
            self.assertListEqual(tar.getnames(), ["foo"],
                    "ignore_zeros=True should have skipped the %r-blocks" % char)
            tar.close() 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:23,代码来源:test_tarfile.py

示例7: test_init_close_fobj

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        open(empty, "wb").write("")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            os.remove(empty) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:19,代码来源:test_tarfile.py

示例8: reader

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def reader(self, remote_snapshots=None):
        """
        Package up filesystem contents as a tarball.
        """
        result = BytesIO()
        tarball = TarFile(fileobj=result, mode="w")
        for child in self.path.children():
            tarball.add(child.path, arcname=child.basename(), recursive=True)
        tarball.close()

        # You can append anything to the end of a tar stream without corrupting
        # it.  Smuggle some data about the snapshots through here.  This lets
        # tests verify that an incremental stream is really being produced
        # without forcing us to implement actual incremental streams on top of
        # dumb directories.
        if remote_snapshots:
            result.write(
                u"\nincremental stream based on\n{}".format(
                    u"\n".join(snapshot.name for snapshot in remote_snapshots)
                ).encode("ascii")
            )
        result.seek(0, 0)
        yield result 
开发者ID:ClusterHQ,项目名称:flocker,代码行数:25,代码来源:memory.py

示例9: test_ignore_zeros

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_ignore_zeros(self):
        # Test TarFile's ignore_zeros option.
        for char in (b'\0', b'a'):
            # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
            # are ignored correctly.
            with self.open(tmpname, "w") as fobj:
                fobj.write(char * 1024)
                fobj.write(tarfile.TarInfo("foo").tobuf())

            tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
            try:
                self.assertListEqual(tar.getnames(), ["foo"],
                    "ignore_zeros=True should have skipped the %r-blocks" %
                    char)
            finally:
                tar.close() 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:18,代码来源:test_tarfile.py

示例10: test_init_close_fobj

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        with open(empty, "wb") as fobj:
            fobj.write(b"")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            support.unlink(empty) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:20,代码来源:test_tarfile.py

示例11: load_chunk

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def load_chunk(tarfile, size=None):
  """Load a number of images from a single imagenet .tar file.

  This function also converts the image from grayscale to RGB if necessary.

  Args:
    tarfile (tarfile.TarFile): The archive from which the files get loaded.
    size (Optional[Tuple[int, int]]): Resize the image to this size if provided.

  Returns:
    numpy.ndarray: Contains the image data in format [batch, w, h, c]
  """
  result = []
  filenames = []
  for member in tarfile.getmembers():
    filename = member.path
    content = tarfile.extractfile(member)
    img = Image.open(content)
    rgbimg = Image.new("RGB", img.size)
    rgbimg.paste(img)
    if size != None:
      rgbimg = rgbimg.resize(size, Image.ANTIALIAS)
    result.append(np.array(rgbimg).reshape(1, rgbimg.size[0], rgbimg.size[1], 3))
    filenames.append(filename)
  return np.concatenate(result), filenames 
开发者ID:ray-project,项目名称:ray-legacy,代码行数:27,代码来源:alexnet.py

示例12: _unpack_data

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def _unpack_data(self, tar: TarFile, data_archive: TarFile):
        with io.BytesIO(
            str.encode(
                "\n".join(
                    [
                        member.name.lstrip(".")
                        for member in data_archive
                        if member.name.lstrip(".")
                    ]
                )
                + "\n"
            )
        ) as fileobj:
            info = TarInfo("list")
            info.size = fileobj.getbuffer().nbytes
            self._unpack_info_file(tar, info, fileobj)

        names = tar.getnames()

        for member in (member for member in data_archive if member.name not in names):
            if member.islnk() or member.issym() or member.isdir():
                tar.addfile(member)
            else:
                with data_archive.extractfile(member) as fileobj:
                    tar.addfile(member, fileobj) 
开发者ID:ThomasKluiters,项目名称:fetchy,代码行数:27,代码来源:debian.py

示例13: tar_error

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def tar_error(tmpdir):
    def fixture(filename):
        path = os.path.join(str(tmpdir), filename)

        def reset(path):
            os.mkdir('d')
            with tarfile.TarFile(path, 'w') as archive:
                for file in ('a', 'b', 'c', 'd/e'):
                    with open(file, 'w') as f:
                        f.write('*')

                    archive.add(file)

                    os.remove(file)

            with tarfile.TarFile(path, 'r') as archive:
                archive.extractall()

        os.chdir(str(tmpdir))
        reset(path)

        assert set(os.listdir('.')) == {filename, 'a', 'b', 'c', 'd'}
        assert set(os.listdir('./d')) == {'e'}

    return fixture 
开发者ID:nvbn,项目名称:thefuck,代码行数:27,代码来源:test_dirty_untar.py

示例14: install_pkg

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def install_pkg(pkg_spec, install_path):
    data = get_pkg_metadata(pkg_spec)

    latest_ver = data["info"]["version"]
    packages = data["releases"][latest_ver]
    assert len(packages) == 1
    package_url = packages[0]["url"]
    print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url))
    f1 = url_open(package_url)
    s = read_lines(f1)
    try:
        str1 = zlib.decompress(s, gzdict_sz)
        with tempfile.TemporaryFile() as temp_file:
            temp_file.write(str1)
            temp_file.seek(0)
            with tarfile.TarFile(fileobj=temp_file) as tar_file:  # Expects a file object
                meta = install_tar(tar_file, install_path)
    finally:
        f1.close()
    return meta 
开发者ID:peterhinch,项目名称:micropython-samples,代码行数:22,代码来源:micropip.py

示例15: _create_test_corpus

# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import TarFile [as 别名]
def _create_test_corpus(self, data):
        """
        Create a small tar in a similar format to the
        Ubuntu corpus file in memory for testing.
        """
        file_path = os.path.join(self.trainer.data_directory, 'ubuntu_dialogs.tgz')
        tar = tarfile.TarFile(file_path, 'w')

        tsv1 = BytesIO(data[0])
        tsv2 = BytesIO(data[1])

        tarinfo = tarfile.TarInfo('dialogs/3/1.tsv')
        tarinfo.size = len(data[0])
        tar.addfile(tarinfo, fileobj=tsv1)

        tarinfo = tarfile.TarInfo('dialogs/3/2.tsv')
        tarinfo.size = len(data[1])
        tar.addfile(tarinfo, fileobj=tsv2)

        tsv1.close()
        tsv2.close()
        tar.close()

        return file_path 
开发者ID:gunthercox,项目名称:ChatterBot,代码行数:26,代码来源:test_ubuntu_corpus_training.py


注:本文中的tarfile.TarFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。