【问题标题】:How do I extract a tar file using python 2.4?如何使用 python 2.4 提取 tar 文件?
【发布时间】:2015-07-01 14:06:28
【问题描述】:

我正在尝试使用 python 2.4.2 完全提取 .tar 文件,因此并非 tarfile 模块的所有方面都可用。我浏览了 python 纪录片,但我发现它没有帮助,因为我继续犯语法错误。以下是我尝试过的命令(没有成功):

tarfile.Tarfile.getnames(tarfile.tar)
tarfile.Tarfile.extract(tarfile.tar)

有没有一种简单的方法可以完全提取我的焦油?如果是这样,使用的格式是什么?另外,我想指出 tarfile.TarFile.extractall() 在我的 python 版本中不可用。

【问题讨论】:

    标签: python tar


    【解决方案1】:

    此示例来自tarfile 文档。

    import tarfile
    tar = tarfile.open("sample.tar.gz")
    tar.extractall()
    tar.close()
    

    首先使用tarfile.open()创建一个TarFile对象,然后使用extractall()提取所有文件,最后关闭该对象。

    如果要解压到其他目录,请使用extractall's path parameter

    tar.extractall(path='/home/connor/')
    

    编辑:我现在看到您使用的是没有 TarFile.extractall() 方法的旧 Python 版本。 documentation for older versions of tarfile 证实了这一点。您可以改为执行以下操作:

    for member in tar.getmembers():
        print "Extracting %s" % member.name
        tar.extract(member, path='/home/connor/')
    

    如果你的 tar 文件中有目录,这可能会失败(我还没有测试过)。如需更完整的解决方案,请参阅Python 2.7 implementation of extractall

    编辑 2:对于使用旧 Python 版本的简单解决方案,请使用 subprocess.call 调用 tar command

    import subprocess
    tarfile = '/path/to/myfile.tar'
    path = '/home/connor'
    retcode = subprocess.call(['tar', '-xvf', tarfile, '-C', path])
    if retcode == 0:
        print "Extracted successfully"
    else:
        raise IOError('tar exited with code %d' % retcode)
    

    【讨论】:

      【解决方案2】:

      这是来自torchvision library 的更通用的代码:

      import os
      import hashlib
      import gzip
      import tarfile
      import zipfile
      
      def _is_tarxz(filename):
          return filename.endswith(".tar.xz")
      
      
      def _is_tar(filename):
          return filename.endswith(".tar")
      
      
      def _is_targz(filename):
          return filename.endswith(".tar.gz")
      
      
      def _is_tgz(filename):
          return filename.endswith(".tgz")
      
      
      def _is_gzip(filename):
          return filename.endswith(".gz") and not filename.endswith(".tar.gz")
      
      
      def _is_zip(filename):
          return filename.endswith(".zip")
      
      
      def extract_archive(from_path, to_path=None, remove_finished=False):
          if to_path is None:
              to_path = os.path.dirname(from_path)
      
          if _is_tar(from_path):
              with tarfile.open(from_path, 'r') as tar:
                  tar.extractall(path=to_path)
          elif _is_targz(from_path) or _is_tgz(from_path):
              with tarfile.open(from_path, 'r:gz') as tar:
                  tar.extractall(path=to_path)
          elif _is_tarxz(from_path):
              with tarfile.open(from_path, 'r:xz') as tar:
                  tar.extractall(path=to_path)
          elif _is_gzip(from_path):
              to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0])
              with open(to_path, "wb") as out_f, gzip.GzipFile(from_path) as zip_f:
                  out_f.write(zip_f.read())
          elif _is_zip(from_path):
              with zipfile.ZipFile(from_path, 'r') as z:
                  z.extractall(to_path)
          else:
              raise ValueError("Extraction of {} not supported".format(from_path))
      
          if remove_finished:
              os.remove(from_path)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多