From 6386f25cd4f5a7705e67e9eebc6ec9995f669789 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 20 Jan 2020 17:33:58 -0800 Subject: [PATCH] Rearrange the lines in delete_file. If anything is going to raise an exception here I expect it to be the item missing from the manifest. In which case let's get that out of the way early and only delete from disk at the very end. --- epubfile.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/epubfile.py b/epubfile.py index d31d207..2ebc13e 100644 --- a/epubfile.py +++ b/epubfile.py @@ -479,12 +479,12 @@ class Epub: ) def delete_file(self, id): - os.remove(self.get_filepath(id).absolute_path) + manifest_item = self.opf.manifest.find('item', {'id': id}) + manifest_item.extract() spine_item = self.opf.spine.find('itemref', {'idref': id}) if spine_item: spine_item.extract() - manifest_item = self.opf.manifest.find('item', {'id': id}) - manifest_item.extract() + os.remove(self.get_filepath(id).absolute_path) def get_filepath(self, id): href = self.opf.manifest.find('item', {'id': id})['href']