是否可以从python中的tar包中提取单个文件 [英] Is it possible to extract single file from tar bundle in python

查看:66
本文介绍了是否可以从python中的tar包中提取单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个庞大的svn存储库中获取几个文件.整个回购大约需要一个小时才能提取.我要查找的文件是tar捆绑包的一部分.

I need to fetch a couple of files from a huge svn repo. Whole repo takes almost an hour to be fetched. Files I am looking for are part of tar bundle.

是否可以从tar捆绑包中仅提取这两个文件,而无需通过Python代码提取整个捆绑包?

Is it possible to fetch only those two files from tar bundle without extracting the whole bundle through Python Code?

如果是这样,有人可以让我知道我应该怎么做吗?

If so, can anybody let me know how should I go about it?

推荐答案

这是从svn获取tar文件并从中提取一个文件的一种方法:

Here is one way to get a tar file from svn and extract one file from it all:

import tarfile
from subprocess import check_output
# Capture the tar file from subversion
tmp='/home/me/tempfile.tar'
open(tmp, 'wb').write(check_output(["svn", "cat", "svn://url/some.tar"]))
# Extract the file we want, saving to current directory
tarfile.open(tmp).extract('dir1/fname.ext', path='dir2')

其中"dir1/fname.ext"是tar归档文件中所需文件的完整路径.它将保存在"dir2/dir1/fname.ext"中.如果省略path参数,它将保存在当前目录下的"dir1/fname.ext"中.

where 'dir1/fname.ext' is the full path to the file that you want within the tar archive. It will be saved in 'dir2/dir1/fname.ext'. If you omit the path argument, it will be saved in 'dir1/fname.ext' under the current directory.

以上内容可以理解如下.在普通的shell命令行上,svn cat url告诉Subversion将url定义的文件发送到stdout(有关更多信息,请参见svn help cat). url可以是svn可以理解的任何类型的url,例如svn://...svn+ssh://...file://....我们使用subprocess模块​​在python控制下运行此命令.为此,将svn cat url命令分解为一个列表:["svn", "cat", "url"].此svn命令的输出将保存到tmp变量定义的本地文件中.然后,我们使用tarfile模块提取所需的文件.

The above can be understood as follows. On a normal shell command line, svn cat url tells subversion to send the file defined by url to stdout (see svn help cat for more info). url can be any type of url that svn understands such as svn://..., svn+ssh://..., or file://.... We run this command under python control using the subprocess module. To do this the svn cat url command is broken up into a list: ["svn", "cat", "url"]. The output from this svn command is saved to a local file defined by the tmp variable. We then use the tarfile module to extract the file you want.

或者,您可以使用extractfile方法将文件数据捕获到python变量中:

Alternatively, you could use the extractfile method to capture the file data to a python variable:

handle = t.extractfile('dir1/fname.ext')
print handle.readlines() # show file contents

根据文档,tarfile应该接受子进程的stdout作为文件句柄.这将简化代码并消除在本地保存tar文件的需要.但是,由于问题10436 的错误,将无法正常工作.

According to the documentation, tarfile should accept a subprocess's stdout as a file handle. This would simplify the code and eliminate the need to save the tar file locally. However, due to a bug, Issue 10436, that will not work.

这篇关于是否可以从python中的tar包中提取单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆