Apr-01-2018, 01:42 PM
Hello all,
I'm relatively new to Python, and I'm enjoying the learning process and the info I find here!
I am writing a little program that will delete all contents of a particular folder. I am using a test folder of "c:\py" (yes, windows) and the program works, but afterward when I go to my file explorer window and try to manually delete the now-empty folder a pop-up says "Folder in use - The action can't be completed because the folder or a file in it is open in another program."
I am using IDLE, and if I restart the shell I can then remove the folder just fine. This tells me that something hasn't been gracefully closed even after the program completes. I tried to see if there is an os.close() function, and there is, but that needs a file descriptor that I am unsure about. There are no open file handles that I know of.
(And if you wonder why I don't just use rmtree() on the folder itself the point of my program is to delete the contents of the folder but leave the folder itself now that it's been cleaned. I am just trying to remove it afterwards because I want to copy over a backup I made of it previously.)
Here is my code. What am I doing wrong?
!!!Heed the warning in the comments if you decide to try to run this. It WILL delete everything if you have a a c:\py folder!!!
I'm relatively new to Python, and I'm enjoying the learning process and the info I find here!
I am writing a little program that will delete all contents of a particular folder. I am using a test folder of "c:\py" (yes, windows) and the program works, but afterward when I go to my file explorer window and try to manually delete the now-empty folder a pop-up says "Folder in use - The action can't be completed because the folder or a file in it is open in another program."
I am using IDLE, and if I restart the shell I can then remove the folder just fine. This tells me that something hasn't been gracefully closed even after the program completes. I tried to see if there is an os.close() function, and there is, but that needs a file descriptor that I am unsure about. There are no open file handles that I know of.
(And if you wonder why I don't just use rmtree() on the folder itself the point of my program is to delete the contents of the folder but leave the folder itself now that it's been cleaned. I am just trying to remove it afterwards because I want to copy over a backup I made of it previously.)
Here is my code. What am I doing wrong?
!!!Heed the warning in the comments if you decide to try to run this. It WILL delete everything if you have a a c:\py folder!!!
#This will remove files and directories in "path". ***Be careful!!*** import os, shutil path = "c:\\Py" #Root path of dir to clean - caution! os.chdir(path) #Switch to that directory to operate on directories = [] #List to hold the names of the directories to remove for x in os.scandir(path): #Read the contents of the directory if x.is_file(): #Check for files os.remove(x.name) #Clean out all files in root path elif x.is_dir(): #Check for directories directories.append(x.name) # Build a list of dirs to be removed next for n in directories: #Go through the complete list of dirs to be removed shutil.rmtree(os.path.join(path, n)) #This will append the dir name to the path and remove it