I’m trying to remove all files found in a directory. The accepted answer to Delete Folder Contents in Python suggests getting a list of all files and calling “unlink” on them in a loop.
Suppose I have thousands of files on a network share, and want to tie up the directory for as short a time as possible.
Is it more efficient to delete them all using a shell command like rm -f /path/* or by using shutils.rmtree or some such?
If you actually want to delete the whole directory tree,
shutils.rmtreeshould be faster thanos.remove(which is the same asos.unlink). It also allows you to specify a callback function to handle errors.The suggestion in the comment by @nmichaels is also good, you can
os.renamethe directory then make a new one in its place and useshutils.rmtreeon the original, renamed directory.