使用tarfile.extractall()解压缩的tar.gz文件夹的其他名称 [英] Different name of unpacked tar.gz folder using tarfile.extractall()

查看:1578
本文介绍了使用tarfile.extractall()解压缩的tar.gz文件夹的其他名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pythons tarfile.extractall()解压缩foo.tar.gz文件.我想访问提取的文件夹,但有时提取的文件夹的名称与打包文件的名称不同.我需要一种方法来控制提取的文件夹的名称,或者是一个告诉我提取的文件夹的名称的返回值.

I am using pythons tarfile.extractall() to unpack a foo.tar.gz file. I want to access the extracted folder, but sometimes the extracted folder has a different name than the packed file. I need a way to control the name of the extracted folder or, a return value that tells me the name of the extracted folder.

示例

打包文件:foo-rc-2.0.0.tar.gz

解压后的文件夹:foo-2.0.0-rc

推荐答案

tarfile.extractall()只需将压缩包中的 all 文件提取到当前目录或您选择的目录中.

tarfile.extractall() simply extracts all the files in the tarball to the current directory, or a directory of your choice.

如果压缩包包含具有嵌套目录结构的文件,则将解压缩该文件.您看到的是那个目录结构.这些目录的名称(可以超过1个)不必与tarball的名称相对应.

If the tarball contains files with a nested directory structure, then that's what'll be extracted. It is that directory structure that you see. The names of these directories (there can be more than 1) do not have to correspond with the name of the tarball.

如果需要移动这些提取的内容,请在新的空目录中进行操作,这样您就可以在该目录中提取所有:

If you need to move these extracted contents, do so in a new and empty directory, so that you can just pick up everything in that directory:

os.mkdir('extraction')
tarball.extractall('extraction')
for name in os.listdir('extraction'):
     # move that name.

这篇关于使用tarfile.extractall()解压缩的tar.gz文件夹的其他名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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