Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Last active June 4, 2017 02:45
Show Gist options
  • Save apoorvalal/b0aed6c4347550678004509350413afc to your computer and use it in GitHub Desktop.
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
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)
@jesscyzhao
Copy link

Noice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment