Skip to content

Instantly share code, notes, and snippets.

@metinsaylan
Forked from anonymous/rmblanks.py
Created February 24, 2018 00:13
Show Gist options
  • Save metinsaylan/e34576a457b2ad26896507c51bde7174 to your computer and use it in GitHub Desktop.
Save metinsaylan/e34576a457b2ad26896507c51bde7174 to your computer and use it in GitHub Desktop.
This Python script deletes all blank folders under a given path. Minimum python version: 3.5.
#-------------------------------------------------------------
# rmblanks.py
# Deletes all empty folders under a given path.
# http://metinsaylan.com
#-------------------------------------------------------------
# Usage: rmblanks.py "E:/Test"
import sys, os
if len(sys.argv) == 1:
# Print usage
print("Usage: rmblanks.py \"E:/TestFolder\"")
else:
for root, dirs, files in os.walk(sys.argv[1], topdown=False):
for name in dirs:
try:
if len(os.listdir( os.path.join(root, name) )) == 0: #check whether the directory is empty
print( "Deleting", os.path.join(root, name) )
try:
os.rmdir( os.path.join(root, name) )
except:
print( "FAILED :", os.path.join(root, name) )
pass
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment