import os import shutil import subprocess import re dirPath = ["C:\\Users\\user1\\demo\\text document.txt","C:\\users\\user1\\documents"] # Provide the files or folder path to be deleted x=0 for i in dirPath: if os.path.isfile(i): os.remove(i) print("Cleared successfully : "+i) else: fileList = os.listdir(i) try: os.chmod(dirPath,0644) except: pass for f in fileList: try: if os.path.isfile(os.path.join(i, f)): os.remove(os.path.join(i, f)) elif os.path.isdir(os.path.join(i, f)): shutil.rmtree(os.path.join(i, f)) except: x+=1 if x==0: print "Cleared successfully : "+i else: print "Some error removing Files or Folders : "+i