Last active
June 4, 2017 02:45
-
-
Save apoorvalal/b0aed6c4347550678004509350413afc to your computer and use it in GitHub Desktop.
Clears files with extension in exts matching pattern fn in directory (and subdirectories if subdir is set to true) in root
This file contains 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 os, glob | |
def clean_folder(exts, | |
root, | |
subdir=True, | |
fn='*.'): | |
""" | |
Clears files with extension in exts matching pattern fn in directory | |
(and subdirectories if subdir is set to true) in root | |
""" | |
if subdir == True: | |
pattern = root+'\\**\\'+fn | |
l = [glob.glob(pattern+e,recursive=True) for e in exts] | |
else: | |
pattern = root+fn | |
l = [glob.glob(pattern+e) for e in exts] | |
files = [y for x in l for y in x] | |
for f in files: | |
os.remove(f) | |
if __name__ == '__main__': | |
cleanup_exts = [ | |
'tmp', | |
'csv', | |
'dta', | |
'html', | |
'log', | |
'txt' | |
] | |
root_list = [ | |
'C:/Users/alal/Desktop/projects/temp', | |
"C:/Users/alal/Desktop/projects/housekeeping", | |
] | |
for r in root_list: | |
clean_folder( | |
cleanup_exts, | |
r, | |
subdir=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Noice.