banner ad

python file directory delete

| January 23, 2013
0 Flares 0 Flares ×

Delete svn code directory:

#!/Usr/bin/env python
import sys, os, stat 

def walk (path): 
    for item in os.listdir (path): 
        subpath = os.path.join (path, item) 
        mode = os.stat (subpath) [stat.ST_MODE] 
        if stat.S_ISDIR (mode): 
            if item == ". svn": 
                print "Cleaning% s"% subpath 
                print "% d deleted"% purge (subpath) 
            else: 
                walk (subpath) 

def purge (path): 
    count = 0 
    for item in os.listdir (path): 
        subpath = os.path.join (path, item) 
        mode = os.stat (subpath) [stat.ST_MODE] 
        if stat.S_ISDIR (mode): 
            count + = purge (subpath) 
        else: 
            os.chmod (subpath, stat.S_IREAD | stat.S_IWRITE) 
            os.unlink (subpath) 
            count + = 1 
    os.rmdir (path) 
    count + = 1 
    return count 
if len (sys.argv)! = 2: 
    print "Usage: python cleansvn.py path" 
    sys.exit (1) 
walk (sys.argv [1]) 

Delete all files and folders in a directory:

#!/Usr/bin/env python
import os 

def delete_all_file (path): 
    "Delete all folers and files" 
    if os.path.isfile (path): 
        try: 
            os.remove (path) 
        except: 
            pass 
    elif os.path.isdir (path): 
        for item in os.listdir (path): 
            itemsrc = os.path.join (path, item) 
            delete_all_file (itemsrc) 
        try: 
            os.rmdir (path) 
        except: 
            pass 

if __ name__ == "__main__": 
    dirname = r'F: \ trunk ' 
    print delete_all_file (dirname)
Download PDF
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×

Tags: , ,

Category: Function

About the Author ()

My name is John Link.I am 26 years old. My major is Computer science and technology. I am a junior programmer with Python.

Comments are closed.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×