Tuesday, March 9, 2010

Delete the hell out of a folder

This is my breakdown of a python function I'm using to delete a folder.
What I want of course is a no muss, no fuss deletion of a folder, all it's contents including all it's sub-folders and the same so on and on.
That's called 'recursion'. When I first heard about it, it took a bit to get my head around. Basically it's a function which calls itself.
WIERD! I know.

so in plain form we want:
function run on filepath =>
See what's in that filepath =>
if it has a folder, run this same function on that folders filepath (go back to step one until you don't have any sub-folders, in which case that call of the function ends, and you slingshot out of that and back up until you're out of the matrix completely) =>
if you find a file, delete it. =>
once the folder has everything deleted out of it, delete that folder, then end the function!

In my working script, I have some extra catches and conditions to avoid errors. I'll over comment this
comments are whatever is after a '#' in a line. Python is all about indenting, as I'm jumping ahead quite a bit and haven't covered any of that basic stuff yet. But you could play with that yourself or find that info elsewhere or later if you want.

import os #add a module we will need to use. Find what ones you need in the help file.
def delete_folder(path): #Define the function, what the variable it's recieving will be called in brackets, then colon.
   if( os.path.exists(path) ): #indent then run an 'if' condition (similar syntax to the function def. colons!) Make sure the path exists.
       files = os.listdir(path) #find everything in the folder
       for fil in files: #this is a loop. This will iterate over a list making 'fil' the variable I chose in this case, one item in the list at a time.
           pth = os.path.join(path, fil) #Handy function for joining paths and filenames.
           if( os.path.isfile(pth) ): #check if it's a file, if it is, then delete it.
               os.remove( pth )
           elif ( os.path.isdir(pth) ): #elif runs if the preceding condition is False only. If not a file, a folder?
               delete_folder(pth) # here we're having this function run itself. Crazy, no? but it works!
       files = os.listdir(path) #this isn't strictly needed, but lets double check.
       if(len(files)>0): #check for one or more entries?
           pass #pass doesn't do anything, but since python needs something here, this is what is used. indentation and all that.
       else: #if the first case isn't true, then do what's here
           os.rmdir( path ) # delete the directory fed into the function at the start! nice,eh?

So, in a nutshell, that's what I'm using right now. now I can call this later in code and clean up a directory etc. But be careful, as with great power comes great responsibility, and don't blame me if you delete all the files on your computer with this! That would be a neat safety to have in this I guess. homework? Likely need to know a bunch about finding system directories etc. Try looking in the help file os. os.getenv("HOME") maybe? something like that....

No comments:

Post a Comment