Changes between Version 5 and Version 6 of PythonSandbox

Show
Ignore:
Author:
kmilner (IP: 128.125.23.182)
Timestamp:
08/19/08 14:29:08 (1 year ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PythonSandbox

    v5 v6  
    3636}}} 
    3737 
    38 = Delete a directory in python (helpful for cleanup) = 
    39  
    40 Note: this will not delete subdirs, if you want to do that then you should recursively call the function if f is a dir (there's an os method to find that out) 
     38= Delete a directory recursively in python (helpful for cleanup) = 
    4139 
    4240{{{ 
    43 items = os.listdir(tmpdir) 
    44 for f in items: 
    45         if f == '.' or f == '..': continue 
    46         file = tmpdir + os.sep + f 
    47         print "Deleting " + file 
    48         os.remove(file) 
    49 os.rmdir(tmpdir) 
     41# Delete a directory, recursively 
     42def delete_dir(dir): 
     43        items = os.listdir(dir) 
     44        for item in items: 
     45                if item == '.' or item == '..': continue 
     46                file = dir + os.sep + item 
     47                if os.path.isdir(file): 
     48                        # if this file is actually a dir, delete the dir 
     49                        delete_dir(file) 
     50                else: # it's just a file 
     51                        print "Deleting " + file   
     52                        os.remove(file) 
     53        os.rmdir(dir) 
    5054}}}