Skip to content

Instantly share code, notes, and snippets.

@DavidHickman
Last active August 24, 2016 14:41
Show Gist options
  • Save DavidHickman/4fa5025bad340ebadad1af6842d50846 to your computer and use it in GitHub Desktop.
Save DavidHickman/4fa5025bad340ebadad1af6842d50846 to your computer and use it in GitHub Desktop.
import os
def clean_up_folder(dir_path, ext_keep=(".zip", ".kmz", ".GeoJSON", ".csv")):
"""
Loop through a directory looking for files with extensions
that match an extension in an array. If the file's extension
does not match, delete the file.
:param dir_path: (str) path to directory to search
:param ext_keep: (list) file extensions of files to keep
:return: None
"""
ext_keep = [ext.lower() for ext in ext_keep]
for f in os.listdir(dir_path):
if os.path.splitext(f)[1].lower() not in ext_keep:
os.remove(os.path.join(dir_path, f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment