0

I have a long running operation in Python, e.g. shutil.rmtree. Removing a large file system tree can take minutes. I want to ensure that the directory does not end up in a half-deleted state due to the user becoming impatient and interrupting the with CTRL-C.

So far, I have this, but I don't like it, as it feels brittle and weird:

try:
   shutil.rmtree(path)
except BaseException:
   try:
      shutil.rmtree(path)
   except Exception
      pass
   raise

Is there a better way to achieve this?

gexicide
  • 37,012
  • 19
  • 88
  • 143
  • Also, this might be naive, but can't you just rename/move the tree to a "trash" location and then delete it in the background? (I'm wondering why deleting a whole tree should be slow anyway) – mkrieger1 Mar 28 '22 at 19:17

0 Answers0