Skip to content

Instantly share code, notes, and snippets.

@nikos-glikis
Created August 9, 2015 13:49
Show Gist options
  • Save nikos-glikis/16d4fead73b6e3a074c1 to your computer and use it in GitHub Desktop.
Save nikos-glikis/16d4fead73b6e3a074c1 to your computer and use it in GitHub Desktop.
Recursive delete directory contents with python
def delete_directory_contents(folder):
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception, e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment