Reputation: 276
I did not work too much with Python Files I/O and now I want kindly ask your help.
I want to delete all folders that have specific names, e.g. '1', '2', '3', ... I created them with a 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 which I do not know how to write, that checks if these folders ('1', '2', '3', ...) already exist:
if yes, I want to delete them with all their content and then execute the code above. if not, then just execute the code.
P.S. Does any difference exist between 'directory' and 'folder' based on programming syntax?
Upvotes: 3
Views: 4359
Reputation: 276
After some time of practising, I ended up with a 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 replied me to my initial question.
Upvotes: 0
Reputation: 876
First of all directory
and folder
are synonyms, so the check you're looking for is the same you already used, i. e. os.path.exists
.
Probably the simplest way to remove 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)
Upvotes: 1
Reputation: 57
Hope this code helps you figure this out.
You can use the os.walk function to get list of all directory to check if the subfolder (1 or 2 or 3) exsists. Then you can use os.system which essentially allows you to launch cmd commands and delete command is used. This is a crude solution but hope this 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 ")
Upvotes: 3