Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import stat
  5.  
  6.  
  7. def rec_rmdir(root, callback, preserve=True):
  8. for path in (os.path.join(root, p) for p in os.listdir(root)):
  9. st = os.stat(path)
  10. if stat.S_ISREG(st.st_mode):
  11. callback(path)
  12. elif stat.S_ISDIR(st.st_mode):
  13. rec_rmdir(path, callback, preserve=False)
  14. if not preserve:
  15. try:
  16. os.rmdir(root)
  17. except IOError:
  18. pass
  19.  
  20.  
  21. def process_file_and_remove(path):
  22. # process the file
  23. # ...
  24. os.remove(path)
  25.  
  26.  
  27. rec_rmdir("/path/to/root", process_file_and_remove)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement