From f637014ad196e1f5634d0bc56f7c163f367f3075 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 24 Feb 2025 19:40:27 +0530 Subject: [PATCH] Kobo driver: Delete empty directories in root These accumulate over time if user deletes books on device rather than via calibre. --- src/calibre/devices/kobo/driver.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index 3aba2d049c..ee4909f1bd 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -17,7 +17,7 @@ import os import re import shutil import time -from contextlib import closing +from contextlib import closing, suppress from datetime import datetime from calibre import fsync, prints, strftime @@ -1631,6 +1631,17 @@ class KOBOTOUCH(KOBO): self.set_device_name() return super().get_device_information(end_session) + def post_open_callback(self): + # delete empty directories in root they get left behind when deleting + # books on device. + for prefix in (self._main_prefix, self._card_a_prefix, self._card_b_prefix): + if prefix: + with suppress(OSError): + for de in os.scandir(prefix): + if not de.name.startswith('.') and de.is_dir(): + with suppress(OSError): + os.rmdir(de.path) + def open_linux(self): super().open_linux()