Possibly along these lines but this must be tested on Windows and on Linux:
def handle_drive_removal(drive):
normalized_drive = os.path.normpath(drive)
removed_volume_name = QtCore.QStorageInfo(normalized_drive).displayName()
windows_to_close = []
for window in open_windows.values():
path = window.folder_path
if os.path.normpath(path).startswith(normalized_drive) or window.volume_name == removed_volume_name:
windows_to_close.append(window)
for window in windows_to_close:
window.close()
desktop_window.refresh_view()
Possibly along these lines but this must be tested on Windows and on Linux: