Skip to content

Instantly share code, notes, and snippets.

@pokulo
Last active December 6, 2024 12:21
Show Gist options
  • Save pokulo/4a8056f58018a252d8e115dbb2995078 to your computer and use it in GitHub Desktop.
Save pokulo/4a8056f58018a252d8e115dbb2995078 to your computer and use it in GitHub Desktop.
shutil.rmtree may fail deleting folder inside NFS mount with: OSError: [Errno 39] Directory not empty:
import logging
import os
import psutil
import shutil
# inspired by https://stackoverflow.com/a/58943527/2894081 and https://github.com/iterative/dvc/issues/5641
directory = "/nfs-mount/some-random-working-directory/"
os.mkdir(directory)
logging.basicConfig(level=logging.DEBUG, filename=os.path.join(directory, 'scratch_141.log'))
logging.debug("This will open the file and write some this message into it.")
p = psutil.Process(os.getpid())
assert len(p.open_files()) > 0
try:
shutil.rmtree(directory)
except OSError as exception:
logging.error(exception)
logging.shutdown()
assert len(p.open_files()) == 0
assert os.path.exists(directory)
shutil.rmtree(directory)
else:
print(f"{directory} is not in nfs!?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment