使用Python的tarfile时覆盖现有的只读文件 [英] Overwrite existing read-only files when using Python's tarfile

查看:313
本文介绍了使用Python的tarfile时覆盖现有的只读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python的tarfile模块提取tar.gz归档文件.

我希望提取文件覆盖目标文件已经存在的所有文件-这是tarfile的正常行为.

但是,由于某些文件具有写保护功能(例如chmod 550),我遇到了麻烦.

tarfile.extractall()操作实际上失败:

IOError: [Errno 13] Permission denied '/foo/bar/file'

如果我尝试从普通命令行删除文件,我可以做到,我只需要回答一个提示:

$ rm <filename>
rm: <filename>: override protection 550 (yes/no)? yes

普通的GNU tar实用程序也可以轻松处理这些文件-提取时只会覆盖它们.

我的用户是文件的所有者,因此在运行tarfile.extractall之前递归更改目标文件并不困难.或者,我可以使用shutil.rmtree预先删除目标,这是我现在正在使用的解决方法.

是否存在使用异常或类似方法处理tarfile中的只读文件的更多Python方式?

解决方案

您可以遍历tarball的成员并提取/处理每个文件上的错误:

在现代版本的Python中,我将使用with语句:

import os, tarfile

with tarfile.TarFile('myfile.tar', 'r', errorlevel=1) as tar:
    for file_ in tar:
        try:
            tar.extract(file_)
        except IOError as e:
            os.remove(file_.name)
            tar.extract(file_)
        finally:
            os.chmod(file_.name, file_.mode)

如果您不能使用with,只需将with语句块替换为:

tarball = tarfile.open('myfile.tar', 'r', errorlevel=1)
for file_ in tar:

如果压缩了您的tar球,有一个快速的快捷方式可以解决这个问题:

tarfile.open('myfile.tar.gz', 'r:gz')

如果tarfile.extractall具有覆盖选项,那就更好了.

I'm attempting to use Python's tarfile module to extract a tar.gz archive.

I'd like the extraction to overwrite any target files it they already exist - this is tarfile's normal behaviour.

However, I'm hitting a snitch in that some of the files have write-protection on (e.g. chmod 550).

The tarfile.extractall() operation actually fails:

IOError: [Errno 13] Permission denied '/foo/bar/file'

If I try to delete the files from the normal command-line, I can do it, I just need to answer a prompt:

$ rm <filename>
rm: <filename>: override protection 550 (yes/no)? yes

The normal GNU tar utility also handles these files effortlessly - it just overwrites them when you extract.

My user is the owner of the files, so it wouldn't be hard to recursively chmod the target files before running tarfile.extractall. Or I can use shutil.rmtree to blow away the target beforehand, which is the workaround I'm using now.. However, that feels a little hackish.

Is there a more Pythonic way of handle overwriting read-only files within tarfile, using exceptions, or something similar?

解决方案

You could loop over the members of the tarball and extract / handle errors on each file:

In a modern version of Python I'd use the with statement:

import os, tarfile

with tarfile.TarFile('myfile.tar', 'r', errorlevel=1) as tar:
    for file_ in tar:
        try:
            tar.extract(file_)
        except IOError as e:
            os.remove(file_.name)
            tar.extract(file_)
        finally:
            os.chmod(file_.name, file_.mode)

If you can't use with just replace the with statement block with:

tarball = tarfile.open('myfile.tar', 'r', errorlevel=1)
for file_ in tar:

If your tar ball is gzipped there's a quick shortcut to handle that with just:

tarfile.open('myfile.tar.gz', 'r:gz')

It would be nicer if tarfile.extractall had an overwrite option.

这篇关于使用Python的tarfile时覆盖现有的只读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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