Python tarfile 模块在提取过程中覆盖现有文件 - 如何禁用它?
有没有办法防止 tarfile.extractall
(API )避免覆盖现有文件?我所说的“防止”是指理想情况下在即将发生覆盖时引发异常。当前的行为是静默覆盖文件。
Is there a way prevent tarfile.extractall
(API) from overwriting existing files? By "prevent" I mean ideally raising an exception when an overwrite is about to happen. The current behavior is to silently overwrite the files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查
tarfile.getnames
针对现有文件并提出错误。You could check result of
tarfile.getnames
against the existing files and raise your error.您是否尝试过将 tarfile.errorlevel 设置为 2?这将导致出现非致命错误。我假设覆盖属于该类别。
Have you tried setting
tarfile.errorlevel
to 2? That will cause non-fatal errors to be raised. I'm assuming an overwrite falls in that category.我有类似的情况,我只想在所有文件尚未提取时提取。我使用以下函数来检查
archive
是否已提取到extract_dir
:I have a similar situation, where I only want to extract if all the files have not yet already been extracted. I use the following function to check if
archive
has already been extracted toextract_dir
: