Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import stat
- def rec_rmdir(root, callback, preserve=True):
- for path in (os.path.join(root, p) for p in os.listdir(root)):
- st = os.stat(path)
- if stat.S_ISREG(st.st_mode):
- callback(path)
- elif stat.S_ISDIR(st.st_mode):
- rec_rmdir(path, callback, preserve=False)
- if not preserve:
- try:
- os.rmdir(root)
- except IOError:
- pass
- def process_file_and_remove(path):
- # process the file
- # ...
- os.remove(path)
- rec_rmdir("/path/to/root", process_file_and_remove)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement