Reputation: 15
I am trying to search for the existence of multiple folders, and if they exist, to delete them. At the moment, the following code works to find and delete a single folder titled "runtime" with myfile_path defined earlier.
if os.path.exists(myfile_path + "/runtime"):
shutil.rmtree(myfile_path + "/runtime")
Rather than repeat these two lines of code for each folder I want to search and delete, is there a cleaner concise way of achieving that? For simplicity, lets say I want to search and delete three folders with all their contents:
Upvotes: 0
Views: 56
Reputation: 3611
You can use a list of all folders you want to remove and then loop through them, doing the same operation with different values.
junk_folders = ['FolderA', 'JunkB', 'DirectoryC']
for folder in junk_folders:
if os.path.exists(myfile_path + "/" + folder):
shutil.rmtree(myfile_path + "/" + folder)
Upvotes: 2