Reputation: 85352
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.
Upvotes: 2
Views: 1726
Reputation: 7994
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 to extract_dir
:
from pathlib import Path
import tarfile
def is_extracted(archive, extract_dir):
tar = tarfile.open(archive)
filenames = tar.getnames()
return all([(Path(extract_dir) / f).exists() for f in filenames])
Upvotes: 0
Reputation: 49038
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.
Upvotes: 0
Reputation: 319601
You could check result of tarfile.getnames
against the existing files and raise your error.
Upvotes: 4