Last active
December 6, 2024 12:21
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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