如何使用 python 2.4 提取 tar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31163668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract a tar file using python 2.4?
提问by Connor Tepfer
I'm trying to extract a .tar file completely using python 2.4.2 and because of this not all of the aspects of the tarfile module are usable. I've looked through the python documentary and I have not found it to be helpful as I continue to make syntax errors. The following are commands I have tried(to no success):
我正在尝试使用 python 2.4.2 完全提取 .tar 文件,因此并非 tarfile 模块的所有方面都可用。我浏览了 python 纪录片,但我没有发现它对我有帮助,因为我继续犯语法错误。以下是我尝试过的命令(没有成功):
tarfile.Tarfile.getnames(tarfile.tar)
tarfile.Tarfile.extract(tarfile.tar)
Is there a simple way to extract my tar completely? If so what is the formatting used? Also, I'd like to note that tarfile.TarFile.extractall() is not available in my python version.
有没有一种简单的方法可以完全提取我的焦油?如果是这样,使用的格式是什么?另外,我想指出 tarfile.TarFile.extractall() 在我的 python 版本中不可用。
回答by André Laszlo
This example is from the tarfile
docs.
这个例子来自tarfile
文档。
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
First, a TarFile object is created using tarfile.open()
, then all files are extracted using extractall()
and finally the object is closed.
首先,使用 创建一个 TarFile 对象tarfile.open()
,然后使用 提取所有文件extractall()
,最后关闭该对象。
If you want to extract to a different directory, use extractall
's path
parameter:
如果要提取到不同的目录,请使用extractall
的 path
参数:
tar.extractall(path='/home/connor/')
Edit:I see now that you are using an old Python version which doesn't have the TarFile.extractall()
method. The documentation for older versions of tarfileconfirms this. You can instead do something like this:
编辑:我现在看到您使用的是没有该TarFile.extractall()
方法的旧 Python 版本。tarfile 旧版本的文档证实了这一点。您可以改为执行以下操作:
for member in tar.getmembers():
print "Extracting %s" % member.name
tar.extract(member, path='/home/connor/')
If your tar file has directories in it, this probably fails (I haven't tested it). For a more complete solution, see the Python 2.7 implementation of extractall
如果您的 tar 文件中有目录,这可能会失败(我还没有测试过)。有关更完整的解决方案,请参阅Python 2.7 实现extractall
Edit 2:For a simple solution using your old Python version, call the tar commandusing subprocess.call
编辑2:对于使用旧版本的Python一个简单的解决方案,调用tar命令使用subprocess.call
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)
回答by Shital Shah
Here's the more general code from torchvision library:
这是来自torchvision 库的更通用的代码:
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)