def checkandremoveboth(): dirpath = "C:\Adialapps\CRMV3', 'C:\Adialapps\CRMV2" if os.path.exists(dirpath) and os.pathisdir(dirpath): shutil.rmtree(dirpath) This formatting looks correct but not working?
import os, shutil def remove_dirs(dirs): for dir in dirs: shutil.rmtree(dir, ignore_errors=True) # https://docs.python.org/3/library/shutil.html#shutil.rmtree dirs = ["C:\Adialapps\CRMV3', 'C:\Adialapps\CRMV2"] remove_dirs(dirs) A better-way and clean way with loop rather than checking each path manually .
import os def check_and_remove(pathsList): for path in pathsList: if os.path.exists(path) and os.path.isdir(path): shutil.rmtree(dir,ignore_errors=True) print("Deleted") else: print(path, " directory not found") dirs_to_delete = [ 'C:\Adialapps\CRMV3', 'C:\Adialapps\CRMV2' ] check_and_remove(dirs_to_delete)
shutil.rmtree. It would be better to use atry/exceptblock to just delete the directories and catch any errors. You can also call it withignore_errors=trueto suppress exceptions if the path doesn't exists, although you do risk hiding other errors, like incorrect permissions.