How can I delete folders with specific names in Python?

Fellows, I haven't worked too much with Python Files I / O and now I want to ask for your help.

I want to delete all folders with specific names, eg. '1', '2', '3', ... I created them with code:

zoom_min = 1
path_to_folders = 'D:/ms_project/'
def folders_creator(zoom):
     for name in range (zoom_min, zoom + 1):
        path_to_folders = '{0}'.format(name)
         if not os.path.exists(path_to_folders):
             os.makedirs(path_to_folders)

      

I want my Python code to have a condition that I don't know how to write that checks if these folders exist ('1', '2', '3', ...):

If so, I want to remove them with all their content and then execute the above code. if not then just execute the code.

thank

PS Is there any difference between "directory" and "folder" based on programming syntax?

+3


source to share


3 answers


After some time of practice, I ended up with the code that was in my mind:

def create_folders(zoom):
    zoom_min = 1
    path_to_folders = 'D:/ms_project/'
    if os.path.isdir(path_to_folders):
        if not os.listdir(path_to_folders) == []:
            for subfolder in os.listdir(path_to_folders):
                subfolder_path = os.path.join(path_to_folders, subfolder)
                try:
                    if os.path.isdir(subfolder_path):
                        shutil.rmtree(subfolder_path)
                    elif os.path.isfile(subfolder_path):
                        os.unlink(subfolder_path)
                except Exception as e:
                    print(e)
        elif os.listdir(path_to_folders) == []:
           print("A folder existed before and was empty.")
    elif not os.path.isdir(path_to_folders):
        os.mkdir("ms_project")
    os.chdir(path_to_folders)
    for name in range(zoom_min, zoom + 1):
        path_to_folders = '{0}'.format(name)
        if not os.path.exists(path_to_folders):
            os.makedirs(path_to_folders)

      



Thanks to everyone who inspired me, especially who answered my original question.

0


source


Hope this code helps you figure it out.

You can use the os.walk function to get a list of all directories to check if the subfolder is (1 or 2 or 3). Then you can use os.system which essentially lets you run cmd commands and use the delete command. This is a rough solution, but hope it helps.



import os

# purt r"directorypath" within os.walk parameter.

genobj = os.walk(r"C:\Users\Sam\Desktop\lel") #gives you a generator function with all directorys
dirlist = genobj.next()[1] #firt index has list of all subdirectorys
print dirlist 

if "1" in dirlist: #checking if a folder called 1 exsists
    print "True"


#os.system(r"rmdir /S /Q your_directory_here ")

      

+2


source


First of all directory

and folder

are synonyms, so the validation you are looking for is the same as you already used, i.e. e os.path.exists

.

Probably the easiest way to delete a directory (and all of its contents) is to use the function rmtree

provided by the standard module shutil

.

Below is your code with my suggestions included.

import shutil
zoom_min = 1
path_to_folders = 'D:/ms_project/'

def folders_creator(zoom):
    for name in range (zoom_min, zoom + 1):
        path_to_folders = '{0}'.format(name)
        if os.path.exists(path_to_folders):
            shutil.rmtree(path_to_folders) 
        os.makedirs(path_to_folders)

      

+1


source







All Articles