I am using shutil.rmtree
to remove a directory, but other processes (that I don't control) that create files in that tree are failing to create the files because the directories don't exist. Is there something as easy as shutil.rmtree
that only deletes files but preserves directory structure?
I don't think there's a built-in function to do this, but you could easily do it yourself using os.walk()
:
for dirpath, dirnames, filenames in os.walk(my_directory):
# Remove regular files, ignore directories
for filename in filenames:
os.unlink(os.path.join(dirpath, filename))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With