views:

36

answers:

2

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.

+2  A: 

You could check result of tarfile.getnames against the existing files and raise your error.

SilentGhost
A: 

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.

Karl Bielefeldt
docs mention only `extract`, does it also work for `extractall`?
SilentGhost
just tested it, it doesn't work for `extractall`
SilentGhost
Good question. After testing, it looks like it either doesn't support `extractall`, or an overwrite isn't considered an error. Looks like SilentGhost's solution is your best bet.
Karl Bielefeldt