user8211900
user8211900

Reputation:

Check if multi directory exists

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?

Upvotes: 2

Views: 65

Answers (2)

Ashfaque Ali Solangi
Ashfaque Ali Solangi

Reputation: 1891

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)

Upvotes: 2

Pedro Lobito
Pedro Lobito

Reputation: 99041

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)

Upvotes: 2

Related Questions