deltree function

Bjorn Pettersen bjorn at roguewave.com
Fri Sep 8 17:39:42 EDT 2000


or perhaps just...

  import shutil
  topdir = r'c:\windows'
  shutil.rmtree(topdir, 1)

-- bjorn

gangli at msn.com wrote:
> 
> In article <39b7ed8b.36840425 at news.bright.net>,
>   jonadab at bright.net (Jonadab the Unsightly One) wrote:
> > "Warren Postma" <embed at NOSPAM.geocities.com> wrote:
> >
> > > On unix I'd just shell out and execute "rm -rf".  On windows, the
> same might
> > > work, since you could always go get the rm.exe program from
> Microsoft's NT
> > > Resource Toolkit.
> >
> > Or, rather than downloading something, you could just shell out
> > to deltree.  You can get around the "Are you sure?" prompt by
> > redirecting or piping its input.
> >
> > - jonadab
> >
> 
> why not use a python function that works everywhere
> see following one( though, it is a littl slower than NT rmdir/s
> because isdir takes half time):
> 
> from sys import stderr
> from os import listdir, remove, rmdir
> from os.path import isdir, islink
> 
> DIR_EXCLUDES = ('.', '..')
> 
> def delete_dir_tree(topdir, verbose=0):
>     """
>     force to detele whole directory tree.
>     uses with caution
>     """
>     if verbose:
>         print "deleting directory:", topdir
>     try:
>         dirs = listdir(topdir)
>     except OSError, why:
>         stderr.write('%s, When delete directory: %s\n'%(why, topdir))
>         return
>     for dir in dirs:
>         if dir in DIR_EXCLUDES:
>             continue
>         fpath = topdir+'/'+dir
>         if islink(fpath) or not isdir(fpath):
>             try:
>                 if verbose > 1:
>                     print "deleting file:", fpath
>                 remove(fpath)
>             except OSError, why:
>                 stderr.write('%s, When delete file: %s\n'%(why, fpath))
>         else:
>             delete_dir_tree(fpath, verbose)
>     try:
>         rmdir(topdir)
>     except OSError, why:
>         stderr.write('%s, When delete directory: %s\n'%(why, topdir))
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list